使用c#跨进程操作SysTreeView32

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/arebu/article/details/78549339

项目需要模拟鼠标点击其他应用程序的SysTreeView32控件指定Item。

//define
   
public const int TVM_ENSUREVISIBLE = 0x1100 + 20;
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;
public const int WM_MOUSEMOVE = 0x0200;

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//BOOL WINAPI PostMessage(_In_opt_ HWND   hWnd,_In_     UINT   Msg,_In_     WPARAM wParam,_In_     LPARAM lParam);
public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//LRESULT WINAPI SendMessage( _In_ HWND hWnd, _In_ UINT Msg, _In_ WPARAM wParam, _In_ LPARAM lParam);
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

//implement
this.RunApi(() => Win32Api.SendMessage(treeWindow.HWnd, Win32Api.TVM_ENSUREVISIBLE, 0, remoteNodePtr));

this.RunApi(() => Win32Api.SendMessage(treeWindow.HWnd, Win32Api.TVM_GETITEMRECT, 1, remoteTviRectPtr));
this.RunApi(() => Win32Api.ReadProcessMemory(hProcess, remoteTviRectPtr, localTviRectPtr, new UIntPtr(sizeOfTviRect), IntPtr.Zero));
getedTviRect = (Win32Api.Rect)Marshal.PtrToStructure(localTviRectPtr, typeof(Win32Api.Rect));
Help.DebugWrite(this, getedTviRect.ToString());

//SendInputTest(getedTviRect);
this.RunApi(() => Win32Api.PostMessage(treeWindow.HWnd,
    Win32Api.WM_MOUSEMOVE, 0, (getedTviRect.CenterY * 65536 + getedTviRect.CenterX)));
this.RunApi(() => Win32Api.SendMessage(treeWindow.HWnd,
    Win32Api.WM_LBUTTONDOWN, 0, (getedTviRect.CenterY * 65536 + getedTviRect.CenterX)));
this.RunApi(() => Win32Api.SendMessage(treeWindow.HWnd,
    Win32Api.WM_LBUTTONUP, 0, (getedTviRect.CenterY * 65536 + getedTviRect.CenterX)));


展开阅读全文

没有更多推荐了,返回首页