鼠標操作window窗體合集...更新中
1.根據句柄查找窗體
引自http://www.2cto.com/kf/201410/343342.html
使用SPY++工具獲取窗體
首先打開spy++工具,同時點擊"查找窗口"按鈕(望遠鏡),再點擊"查找程序工具"中按鈕拖拽至要查看的窗體中,點擊"確定"按鈕.
IntPtr awin = MouseHookHelper.FindWindow("WeChatMainWndForPC", "微信");
if (awin == IntPtr.Zero)
{
MessageBox.Show("沒有找到窗體");
return;
}
2.獲取窗體坐標信息
MouseHookHelper.RECT rect = new MouseHookHelper.RECT();
MouseHookHelper.GetWindowRect(awin, ref rect);
int width = rect.Right - rect.Left; //窗口的寬度
int height = rect.Bottom - rect.Top; //窗口的高度
int x = rect.Left;
int y = rect.Top;
3.設置為當前窗體
MouseHookHelper.SetForegroundWindow(awin);
MouseHookHelper.ShowWindow(awin,MouseHookHelper.SW_SHOWNOACTIVATE);//4、5
4.點擊某個坐標
LeftMouseClick(new MouseHookHelper.POINT()
{
X = ppp.MsgX,
Y = ppp.MsgY
});
private static void LeftMouseClick(MouseHookHelper.POINT pointInfo)
{
//先移動鼠標到指定位置
MouseHookHelper.SetCursorPos(pointInfo.X, pointInfo.Y);
//按下鼠標左鍵
MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTDOWN,
pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,
pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);
//松開鼠標左鍵
MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTUP,
pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,
pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);
}
5.復制粘貼操作
//復制到剪貼板
Clipboard.SetText("test");
//從剪貼板獲取數據
Clipboard.GetText();
//粘貼
SendKeys.SendWait("^V");
//回車鍵
SendKeys.Send("{Enter}");
6.鈎子的使用
private void button1_Click(object sender, EventArgs e)
{
if (hHook == 0)
{
MyProcedure = new MouseHookHelper.HookProc(this.MouseHookProc);
//這里掛節鈎子
hHook = MouseHookHelper.SetWindowsHookEx(WH_MOUSE_LL, MyProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (hHook == 0)
{
MessageBox.Show("請以管理員方式打開");
return;
}
button1.Text = "卸載鈎子";
}
else
{
bool ret = MouseHookHelper.UnhookWindowsHookEx(hHook);
if (ret == false)
{
MessageBox.Show("請以管理員方式打開");
return;
}
hHook = 0;
button1.Text = "安裝鈎子";
}
}
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MouseHookHelper.MouseHookStruct MyMouseHookStruct = (MouseHookHelper.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookHelper.MouseHookStruct));
if (nCode < 0)
{
return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
String strCaption = "x = " + MyMouseHookStruct.pt.X.ToString("d") + " y = " + MyMouseHookStruct.pt.Y.ToString("d");
this.Text = strCaption;
return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
7.MouseHookHelper代碼
public class MouseHookHelper
{
#region 根據句柄尋找窗體並發送消息
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
//參數1:指的是類名。參數2,指的是窗口的標題名。兩者至少要知道1個
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
#endregion
#region 獲取窗體位置
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; //最左坐標
public int Top; //最上坐標
public int Right; //最右坐標
public int Bottom; //最下坐標
}
#endregion
#region 設置窗體顯示形式
public enum nCmdShow : uint
{
SW_NONE,//初始值
SW_FORCEMINIMIZE,//:在WindowNT5.0中最小化窗口,即使擁有窗口的線程被掛起也會最小化。在從其他線程最小化窗口時才使用這個參數。
SW_MIOE,//:隱藏窗口並激活其他窗口。
SW_MAXIMIZE,//:最大化指定的窗口。
SW_MINIMIZE,//:最小化指定的窗口並且激活在Z序中的下一個頂層窗口。
SW_RESTORE,//:激活並顯示窗口。如果窗口最小化或最大化,則系統將窗口恢復到原來的尺寸和位置。在恢復最小化窗口時,應用程序應該指定這個標志。
SW_SHOW,//:在窗口原來的位置以原來的尺寸激活和顯示窗口。
SW_SHOWDEFAULT,//:依據在STARTUPINFO結構中指定的SW_FLAG標志設定顯示狀態,STARTUPINFO 結構是由啟動應用程序的程序傳遞給CreateProcess函數的。
SW_SHOWMAXIMIZED,//:激活窗口並將其最大化。
SW_SHOWMINIMIZED,//:激活窗口並將其最小化。
SW_SHOWMINNOACTIVATE,//:窗口最小化,激活窗口仍然維持激活狀態。
SW_SHOWNA,//:以窗口原來的狀態顯示窗口。激活窗口仍然維持激活狀態。
SW_SHOWNOACTIVATE,//:以窗口最近一次的大小和狀態顯示窗口。激活窗口仍然維持激活狀態。
SW_SHOWNOMAL,//:激活並顯示一個窗口。如果窗口被最小化或最大化,系統將其恢復到原來的尺寸和大小。應用程序在第一次顯示窗口的時候應該指定此標志。
}
public const int SW_HIDE = 0;
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_MAXIMIZE = 3;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_SHOW = 5;
public const int SW_MINIMIZE = 6;
public const int SW_SHOWMINNOACTIVE = 7;
public const int SW_SHOWNA = 8;
public const int SW_RESTORE = 9;
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
#endregion
#region 控制鼠標移動
//移動鼠標
public const int MOUSEEVENTF_MOVE = 0x0001;
//模擬鼠標左鍵按下
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
//模擬鼠標左鍵抬起
public const int MOUSEEVENTF_LEFTUP = 0x0004;
//模擬鼠標右鍵按下
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
//模擬鼠標右鍵抬起
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
//模擬鼠標中鍵按下
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
//模擬鼠標中鍵抬起
public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
//標示是否采用絕對坐標
public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
[Flags]
public enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
#endregion
#region 獲取坐標鈎子
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//安裝鈎子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸載鈎子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//調用下一個鈎子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
}
C# 系統應用之鼠標模擬技術及自動操作鼠標
c# 獲取當前活動窗口句柄,獲取窗口大小及位置
用Mouse_event()來控制鼠標操作