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

时间:2014-07-11 15:20:36

标签: c++ winapi click mousemove simulate

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

我尝试使用PostMessageSendMessage模拟鼠标移动但到目前为止没有运气。

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

这甚至可能吗?

1 个答案:

答案 0 :(得分:5)

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

#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;
}

效果很好。