Windows下程序模拟鼠标单击和拖放操作

原文地址:http://blog.csdn.net/ariesjzj/article/details/8526103

用程序模拟鼠标输入可以用以下几种方法:

1. SendMessage和PostMessage,通过发鼠标消息和模拟鼠标事件,优点是窗口最小化后仍然有效。

2. mouse_event,模拟鼠标操作。窗口必须在前面。

3. SendInput, 可以用作模拟鼠标和键盘。

 

这里用第一种方法实现单击和拖放

单击:

void click_point(unsigned long x, unsigned long y)
{
	SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(x, y));
	SendMessage(hwnd, WM_LBUTTONUP, NULL, MAKELPARAM(x, y));
}


拖放:

void drag_drop(unsigned long x1, unsigned long y1, 
			   unsigned long x2, unsigned long y2)
{
#define MOVE_STEP	50
	assert(hwnd);
	POINT point1, point2;
	point1.x = x1; point1.y = y1;
	point2.x = x2; point2.y = y2;
	SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(point1.x, point1.y));
	if (x2 >= x1) {
		for (unsigned long i = x1; i < x2; i += MOVE_STEP) {
			Sleep(10);
			SendMessage(hwnd, WM_MOUSEMOVE, NULL, MAKELPARAM(i, point1.y));
		}
	} else {
		for (unsigned long i = x1; i > x2; i -= MOVE_STEP) {
			Sleep(10);
			SendMessage(hwnd, WM_MOUSEMOVE, NULL, MAKELPARAM(i, point1.y));
		}
	}
	SendMessage(hwnd, WM_LBUTTONUP, NULL, MAKELPARAM(point2.x, point2.y));
}


 

你可能感兴趣的:(Windows)