1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| /**
* LowLevel mouse Event
*/
int MouseEvents(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0)
return CallNextHookEx(_hook, code, wParam, lParam);
if (code == HookCodes.HC_ACTION)
{
// Marshal the MSLLHOOKSTRUCT data from the callback lParam
MSLLHOOKSTRUCT mouseHookStruct = (MSLLHOOKSTRUCT)
Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
// Get the mouse WM from the wParam parameter
MouseMessage wmMouse = (MouseMessage)wParam;
if (wmMouse == MouseMessage.WM_LBUTTONDOWN )
{
IntPtr win = WindowFromPoint(mouseHookStruct.pt);
if (EventHandlerList != null)
{
EventHandlerList(this, new SysEventInfo { Hwnd = win, mouse = mouseHookStruct, Lparam = lParam, Wparam = wParam});
}
}
}
return CallNextHookEx(_hook, code, wParam, lParam);
}
//.........
public class SysEventInfo : EventArgs
{
public IntPtr Hwnd { get; set; }
public IntPtr Lparam { get; set; }
public IntPtr Wparam { get; set; }
public MSLLHOOKSTRUCT mouse { get; set; }
}
//.......
// Methode 1 :
/*
* SendMessage WM_GETTEXT to the HWND
*/
GetText(IntPtr hwnd){
string result;
StringBuilder sb = new StringBuilder(65535);
SendMessage(hwnd, (int)WM_GETTEXT, (IntPtr)sb.Capacity, sb);
return sb.ToString();
}
// Methode 2 :
public static AutomationElement GetCurrentElement(SysEventInfo info)
{
IntPtr hwnd = info.Hwnd;
System.Drawing.Point mouse = new System.Drawing.Point(info.mouse.pt.X, info.mouse.pt.Y); AutomationElement element;
try
{
element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));
}
catch (Exception e)
{
element = AutomationElement.FromHandle(hwnd);
}
return element;
} |
Partager