读书人

发送鼠标移动的消息解决思路

发布时间: 2012-04-11 17:42:33 作者: rapoo

发送鼠标移动的消息
已知一个窗口的句柄,hwnd,并且给定一个量化的鼠标的移动速度,如何通过调用sendmessage函数,在该窗口上模拟鼠标从指定的一个点移动到另一个点的过程。谢谢。

[解决办法]
SetTimer然后BitBlt吧~~~~~~
[解决办法]
我写了一个很粗糙的代码,大概就是楼主的意思吧
#include <windows.h>

#define ID_TIMER 10

POINT ptLeft,ptRight,ptnow;

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
HDC hdcwindow;

switch (message)
{
case WM_CREATE:
SetTimer(hwnd,ID_TIMER,100,NULL);
ptnow.x = 0;
ptnow.y = 0;
break;

case WM_TIMER:
if(ptLeft.x < ptRight.x) ptLeft.x++;
else if (ptLeft.x > ptRight.x) ptLeft.x--;
if(ptLeft.y < ptRight.y) ptLeft.y++;
else if (ptLeft.y > ptRight.y) ptLeft.y--;
InvalidateRect(hwnd,NULL,NULL);
break;

case WM_LBUTTONDOWN:
ptLeft.x = LOWORD(lParam);
ptLeft.y = HIWORD(lParam);
break;

case WM_RBUTTONDOWN:
ptRight.x = LOWORD(lParam);
ptRight.y = HIWORD(lParam);
break;

case WM_MBUTTONDOWN:
InvalidateRect(hwnd,NULL,TRUE);
break;

case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
hdcwindow = GetWindowDC(hwnd);
BitBlt(hdc,ptLeft.x,ptLeft.y,10,10,hdcwindow,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
break;

case WM_DESTROY:
PostQuitMessage (0);
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ( "Test ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ( "This program requires Windows NT! "),
szAppName, MB_ICONERROR);
return 0;
}

hwnd = CreateWindow (szAppName, TEXT ( "Test "),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}


读书人网 >C++

热点推荐