usingSystem;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Automation;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;namespaceBaseTest
{public classBaseClass
{#region ClickMouse
#region Import DLL
///
///Add mouse move event///
/// Move to specify x coordinate
/// Move to specify y coordinate
///
[DllImport("user32.dll")]extern static bool SetCursorPos(int x, inty);///
///Mouse click event///
/// MouseEventFlag
/// X coordinate
/// Y coordinate
///
///
[DllImport("user32.dll")]extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uintdata, UIntPtr extraInfo);const int MOUSEEVENTF_MOVE = 0x0001;const int MOUSEEVENTF_LEFTDOWN = 0x0002;const int MOUSEEVENTF_LEFTUP = 0x0004;const int MOUSEEVENTF_RIGHTDOWN = 0x0008;const int MOUSEEVENTF_RIGHTUP = 0x0010;const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;const int MOUSEEVENTF_MIDDLEUP = 0x0040;const int MOUSEEVENTF_ABSOLUTE = 0x8000;#endregion
public static void ClickLeftMouse(int processId, stringautomationId)
{
AutomationElement element=FindElementById(processId, automationId);if (element == null)
{throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.",
element.Current.AutomationId, element.Current.Name));
}
Rect rect=element.Current.BoundingRectangle;int IncrementX = (int)(rect.Left + rect.Width / 2);int IncrementY = (int)(rect.Top + rect.Height / 2);//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY,0, UIntPtr.Zero);
}#endregion
public static void ClickLeftMouseByName(int processId, stringname)
{
AutomationElement element=FindElementByName(processId, name);if (element == null)
{throw new NullReferenceException(string.Format("Element with Name '{0}' and Name '{1}' can not be find.",
element.Current.Name, element.Current.Name));
}
Rect rect=element.Current.BoundingRectangle;int IncrementX = (int)(rect.Left + rect.Width / 2);int IncrementY = (int)(rect.Top + rect.Height / 2);//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY,0, UIntPtr.Zero);
}///
///Get the automation elemention of current form.///
/// Process Id
/// Target element
public static AutomationElement FindWindowByProcessId(intprocessId)
{
AutomationElement targetWindow= null;int count = 0;try{
Process p=Process.GetProcessById(processId);
targetWindow=AutomationElement.FromHandle(p.MainWindowHandle);returntargetWindow;
}catch(Exception ex)
{
count++;
StringBuilder sb= newStringBuilder();string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();if (count > 5)
{throw newInvalidProgramException(message, ex);
}else{returnFindWindowByProcessId(processId);
}
}
}//Get the automation element by automation Id.
public static AutomationElement FindElementById(int processId, stringautomationId)
{
AutomationElement aeForm=FindWindowByProcessId(processId);
AutomationElement tarFindElement=aeForm.FindFirst(TreeScope.Descendants,newPropertyCondition(AutomationElement.AutomationIdProperty, automationId));returntarFindElement;
}//Get the automation element by Name.
public static AutomationElement FindElementByName(int processId, stringname)
{
AutomationElement aeForm=FindWindowByProcessId(processId);
AutomationElement tarFindElement=aeForm.FindFirst(TreeScope.Descendants,newPropertyCondition(AutomationElement.NameProperty, name));returntarFindElement;
}//get the button element and click
public static void ClickButtonById(int processId,stringbuttonId)
{
AutomationElement element=FindElementById(processId,buttonId);if (element == null)
{throw new NullReferenceException(string.Format("Element with AutomationId '{0}' can not be find.", element.Current.Name));
}
InvokePattern currentPattern=GetInvokePattern(element);
currentPattern.Invoke();
}#region InvokePattern helper
///
///Get InvokePattern///
/// AutomationElement instance
/// InvokePattern instance
public staticInvokePattern GetInvokePattern(AutomationElement element)
{objectcurrentPattern;if (!element.TryGetCurrentPattern(InvokePattern.Pattern, outcurrentPattern))
{throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the InvokePattern.",
element.Current.AutomationId, element.Current.Name));
}return currentPattern asInvokePattern;
}#endregion}
}