Created
January 17, 2024 01:55
-
-
Save he426100/7389281de65c4c1cae02fbd8bdcfb1d5 to your computer and use it in GitHub Desktop.
基于php ffi实现模拟鼠标操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // 定义 FFI 和必要的常量 | |
| define('USER32', 'user32.dll'); | |
| define('GDI32', 'gdi32.dll'); | |
| // @link https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-point | |
| // @link https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect | |
| // @link https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdesktopwindow | |
| // @link https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos | |
| // @link https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect | |
| $ffi = FFI::cdef( | |
| " | |
| typedef int BOOL; | |
| typedef unsigned int UINT; | |
| typedef unsigned long DWORD; | |
| typedef long LONG; | |
| typedef void* HWND; | |
| typedef struct tagRECT { | |
| LONG left; | |
| LONG top; | |
| LONG right; | |
| LONG bottom; | |
| } RECT, *PRECT, *NPRECT, *LPRECT; | |
| typedef struct tagPOINT { | |
| LONG x; | |
| LONG y; | |
| } POINT, *PPOINT, *NPPOINT, *LPPOINT; | |
| HWND GetDesktopWindow(); | |
| BOOL GetCursorPos(POINT *lpPoint); | |
| BOOL SetCursorPos(int X, int Y); | |
| BOOL GetWindowRect(HWND hWnd, RECT *lpRect); | |
| ", | |
| USER32 | |
| ); | |
| // 获取屏幕分辨率 | |
| function screenSize() | |
| { | |
| global $ffi; | |
| $hWnd = $ffi->GetDesktopWindow(); | |
| $rect = $ffi->new("RECT"); // 创建 RECT 结构的实例 | |
| $ffi->GetWindowRect($hWnd, FFI::addr($rect)); // 传递 RECT 结构的指针 | |
| return ["width" => $rect->right - $rect->left, "height" => $rect->bottom - $rect->top]; | |
| } | |
| // 获取鼠标位置 | |
| function mousePosition() | |
| { | |
| global $ffi; | |
| $point = $ffi->new("POINT"); | |
| $ffi->GetCursorPos(FFI::addr($point)); | |
| return ["x" => $point->x, "y" => $point->y]; | |
| } | |
| // 定义移动鼠标的函数 | |
| function moveTo($x, $y) | |
| { | |
| global $ffi; | |
| // 设置鼠标位置 | |
| $ffi->SetCursorPos($x, $y); | |
| } | |
| // 使用示例:获取屏幕分辨率 | |
| echo "Screen size: " . json_encode(screenSize()) . "\n"; | |
| // 使用示例:获取鼠标位置 | |
| echo "Mouse position: " . json_encode(mousePosition()) . "\n"; | |
| // 使用示例:移动鼠标到坐标 (100, 100) | |
| moveTo(100, 100); | |
| echo "Mouse position: " . json_encode(mousePosition()) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment