Understanding the ToolBarSrv.cs File

Following is the full source code for the ToolBarSrv.cs class, used to implement UFT record and run support for the TD.SandBar.Toolbar control:

using System;
using Mercury.QTP.CustomServer;
//using TD.SandBar;

namespace ToolBar
{
	[ReplayInterface]
	public interface IToolBarSrvReplay
	{
		void  ClickButton(string  text);
	}
	/// <summary>
	/// Summary description for ToolBarSrv.
	/// </summary>
	public class ToolBarSrv:
		CustomServerBase,
		IToolBarSrvReplay
	{
		// You shouldn't call Base class methods/properties at the constructor
		// since its services are not initialized yet.
		public ToolBarSrv()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IRecord override Methods
		#region Wizard generated sample code (commented)
		/// <summary>
		/// To change Window messages filter, implement this method.
		/// The default implementation is to get only the control's 
		/// Windows messages.
		/// </summary>
		public override WND_MsgFilter GetWndMessageFilter()
		{
			return(WND_MsgFilter.WND_MSGS);
		}

		/*
		/// <summary>
		/// To catch Windows messages, you should implement this method.
		/// Note that this method is called only if the CustomServer is running
		/// under UFT process.
		/// </summary>
		public override RecordStatus OnMessage(ref Message tMsg)
		{
			// TODO:  Add OnMessage implementation.
			return RecordStatus.RECORD_HANDLED;
		}
		*/
		#endregion

		/// <summary>
		/// If you are extending the Record process, you should add your event 
		/// handlers to listen to the control's events.
		/// </summary>
		public override void InitEventListener()
		{
			   TD.SandBar.ToolBar  oControl = (TD.SandBar.ToolBar)SourceControl;
			   oControl.ButtonClick += new
			   TD.SandBar.ToolBar.ButtonClickEventHandler(oControl_ButtonClick);
			   //AddHandler("ButtonClick", new
			   //TD.SandBar.ToolBar.ButtonClickEventHandler(oControl_ButtonClick));
		}

		/// <summary>
		/// At the end of the Record process, this method is called by UFT to 
		/// release all the handlers the user added in the InitEventListener method.
		/// Note that handlers added via UFT methods are released by
		/// the UFT infrastructure.
		/// </summary>
		public override void ReleaseEventListener()
		{
			TD.SandBar.ToolBar  oControl = (TD.SandBar.ToolBar)SourceControl;
			oControl.ButtonClick -= new 
			TD.SandBar.ToolBar.ButtonClickEventHandler(oControl_ButtonClick);
		}
		#endregion

		#region Record events handlers
		private void oControl_ButtonClick(object sender, 
							TD.SandBar.ToolBarItemEventArgs e)
		{
			TD.SandBar.ToolBar  oControl = (TD.SandBar.ToolBar)SourceControl;
			// Add a step in the test for the test object with the ClickButton method
			// and the tooltip text as an argument
			base.RecordFunction("ClickButton", 
					RecordingMode.RECORD_SEND_LINE, e.Item.ToolTipText);
		}
		#endregion
		#region Replay interface implementation
		public void  ClickButton(string text)
		{
		TD.SandBar.ToolBar  oControl = (TD.SandBar.ToolBar)SourceControl;
		//Find the correct item in the toolbar according to its tooltip text.
		for(int i=0; i<oControl.Items.Count; i++)
		{
			//Found the correct ButtonItem
			if(oControl.Items[i].ToolTipText == text)
			{
			// Retrieve the rectangle of the button's boundaries and 
			// locate its center
			System.Drawing.Rectangle oRect=oControl.Items[i].ButtonBounds;
			int x = oRect.X + oRect.Width/2;
			int y = oRect.Y + oRect.Height/2;
			//Click the middle of the button item
			base.MouseClick(x, y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);
			break;
			}
		}
		//Add the step to the report
		base.ReplayReportStep("ClickButton", 
						EventStatus.EVENTSTATUS_GENERAL, text);
		}
		#endregion
	}
}