PHP自定义dll,项目搭建(三):自定义DLL

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}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值