簡體   English   中英

你如何虛擬地執行鼠標移動和點擊 [user32.dll]

[英]How do you perform mouse movements & clicks virtually [user32.dll]

你如何虛擬地執行鼠標移動和點擊?

我的意思是,主鼠標不受影響,所以基本上“創建”第二個鼠標,通過設置 x/y position (使用 user32.dll 的首選)來控制。

我在這里讀過一些類似的問題,但答案通常是用你的主鼠標,例如:

DllImport("user32")]
public static extern int SetCursorPos(int x, int y);

^ 這會將您的主鼠標移動到屏幕上的 x&y position,我想要的是使用“虛擬鼠標”執行此操作,因此我的主鼠標不會移動,所以基本上我可以在這個“虛擬鼠標”執行時繼續使用我的電腦例如,另一個 window 中的其他內容。

然后使用這個“虛擬鼠標”來執行點擊[ 虛擬鍵碼]

我試過這個:

IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));
            
var pointPtr = MakeLParam(900, 1000);//x y cords 
           
IntPtr hWnd = ScreenCapture.FindWindow(null, ScreenCapture.GetWindowName()); //Finds Window 

PostMessage(hWnd, WM_MOUSEMOVE | WM_LBUTTONDOWN | WM_LBUTTONUP, IntPtr.Zero, pointPtr);

[DllImport("User32.dll")]
        public static extern Int32 PostMessage(
            IntPtr hWnd,               // handle to destination window
            int Msg,                // message
            IntPtr wParam,             // first message parameter
            IntPtr lParam);            // second message parameter

但是什么都沒有發生,我的猜測是 WM_MOUSEMOVE 無法正常工作

public const int WM_KEYDOWN = 0x100;
        public const int WM_KEYUP = 0x101;
        public const int WM_COMMAND = 0x111;
        public const int WM_LBUTTONDOWN = 0x201;
        public const int WM_LBUTTONUP = 0x202;
        public const int WM_LBUTTONDBLCLK = 0x203;
        public const int WM_RBUTTONDOWN = 0x204;
        public const int WM_RBUTTONUP = 0x205;
        public const int WM_RBUTTONDBLCLK = 0x206;
        public const int WM_MOUSEMOVE = 0x0200;

更新,我想我解決了一些問題!

在閱讀了有關 PostMessage /SendInput 的更多信息后,我發現“應用程序窗口”可以有“應用程序窗口”的層,所以我使用 ispy++ 來檢查它,是的,該程序還有另一個我想點擊的層。

因此,為了解決這個問題,我使用了我想要單擊的 window 的 class 名稱以及 window 名稱,這是一個示例代碼:

 public static bool ClickTest()
        {
            IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));//Just converts x&y to InPtr lParam

            IntPtr WindowhWid = ScreenCapture.FindWindow(null, ScreenCapture.GetWindowName()); //Gets the window hWid
            var ClasshWid = FindWindowEx(WindowhWid, IntPtr.Zero, "Classname", null); //Gets the Class hWid using WindowhWid AND the Class name (Need to find a way to get the classname using process list)

            PostMessage(ClasshWid, WM_LBUTTONDOWN | WM_LBUTTONUP, 0, MakeLParam(938, 1011));//Finally sends it to the classhWid

            return true;
        }
        

使用這兩個功能

[DllImport("User32.dll")]
        public static extern int FindWindowEx(
            IntPtr hwndParent, 
            IntPtr hwndChildAfter,
            string strClassName,
            string strWindowName);

[DllImport("User32.dll")]
        public static extern Int32 PostMessage(
            int hWnd,               // handle to destination window
            int Msg,                // message
            int wParam,             // first message parameter
            IntPtr lParam);            // second message parameter

請注意,如果應用程序 window 被最小化,它將不會單擊,盡管如果 window 在另一個應用程序 window 后面,它將單擊!

X & Y 也是屏幕 X & Y 而不是應用程序 X & Y !

現在問題仍然存在,為什么在應用程序 window 最小化時它不起作用?

除了應用程序之外,點擊在任何地方都沒有 go,所以在應用程序最小化時它不應該工作嗎?

它可能不是您所關心的和您所關心的,但AutoIT Dll以最簡單的方式提供了大量隨時可用的自動化。 既然您依賴 windows DLL 那么它就是完美的。

如果您仍然在自己的路上 user32.dll 然后檢查Low level hook ,這就是我使用的。

不過,我強烈推薦 AutoIT dll,它會為您節省大量時間來猜測無法正確響應的 windows 消息發生了什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM