c – 是否可以在隐藏窗口模拟鼠标移动和鼠标点击?

那么有没有办法让我的Win32应用程序“思考”鼠标在窗口上移动并在隐藏实际窗口时进行一些点击(我的意思是ShowWindow(hWnd,SW_HIDE);)?

我尝试用PostMessage和SendMessage模拟鼠标移动但到目前为止没有运气.

int x = 0;
int y = 0;
while (true)
{
    SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
    x += 10;
    y += 10;
    Sleep(100);
}

这甚至可能吗?

解决方法:

是的,这是可能的.这是一个测试隐藏窗口:

#define UNICODE
#include <Windows.h>
#include <Strsafe.h>
#include <Windowsx.h>

LRESULT CALLBACK WndProc(HWND Hwnd, UINT Msg, WPARAM WParam, LPARAM LParam);

INT CALLBACK
WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        INT nCmdShow)
{
  WNDCLASSEX WndClass;

  ZeroMemory(&WndClass, sizeof(WNDCLASSEX));

  WndClass.cbSize           = sizeof(WNDCLASSEX);
  WndClass.lpfnWndProc      = WndProc;
  WndClass.hInstance        = hInstance;
  WndClass.lpszClassName    = L"HiddenWinClass";

  if(RegisterClassEx(&WndClass))
  {
    HWND Hwnd;
    MSG Msg;

    Hwnd = CreateWindowEx(0, L"HiddenWinClass", L"Nan",
      0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);

    if(Hwnd)
    {
      UpdateWindow(Hwnd);
      ShowWindow(Hwnd, SW_HIDE);
      while(GetMessage(&Msg, 0, 0, 0) > 0)
      {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
      }
    }
  }

  return 0;
}


LRESULT CALLBACK
WndProc(HWND Hwnd,
        UINT Msg,
        WPARAM WParam,
        LPARAM LParam)
{
  TCHAR Message[255];

  switch(Msg)
  {
    case WM_MOUSEMOVE:
      StringCbPrintf(Message, sizeof(Message), L"(%d, %d)",
        GET_X_LPARAM(LParam), GET_Y_LPARAM(LParam));
      MessageBox(NULL, Message, L"WM_MOUSEMOVE", MB_OK);
      break;
    default:
      return DefWindowProc(Hwnd, Msg, WParam, LParam);
  }

  return 0;
}

这是你的代码:

#define UNICODE
#include <Windows.h>

int
main(int argc, char **argv)
{
  HWND Hwnd;

  if((Hwnd = FindWindow(L"HiddenWinClass", L"Nan")))
  {
    int x, y;

    for(x = y = 0 ; ; x += 10, y += 10)
    {
      SendMessage(Hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
      Sleep(100);
    }
  }

  return 0;
}

它工作得很好.

上一篇:模拟鼠标光标在c#中两个坐标之间的移动


下一篇:javascript-当画布在屏幕外滚动时,无法在画布中拖动对象