簡體   English   中英

是否可以在隱藏窗口模擬鼠標移動和鼠標點擊?

[英]Is it possible to simulate mouse move and mouse clicks at hidden window?

那么有沒有辦法讓我的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);
}

這甚至可能嗎?

是的,這是可能的。 這是一個測試隱藏窗口:

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

它工作得很好。

暫無
暫無

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

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