Delphi 鼠标控制函数GetCursorPos、SetCursorPos
GetCursorPos函数 获取鼠标的位置
var
P: TPoint;
begin
GetCursorPos(P); //获取鼠标位置
end;
SetCursorPos函数 设置鼠标的位置
var
x,y:integer; //
begin
x:=614; y:=282; //
SetCursorPos(x,y); //
Mouse_Event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //开始点击
Mouse_Event(MOUSEEVENTF_LEFTUP,0,0,0,0); //弹出
end;
mouse_event,用于模拟鼠标动作。无返回值。参数1类型:整数型(Integer),鼠标动作标识;参数2类型:整数型(Integer),鼠标动作的水平坐标X;参数3类型:整数型(Integer),鼠标动作的垂直坐标Y;参数4类型:整数型(Integer),鼠标滚轮转动方向,若当前鼠标无滚轮动作,则取值为0;参数5在Windows SDK中的函数原型:
VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants 鼠标动作标识
DWORD dx, // horizontal mouse position or position change 鼠标动作的水平坐标
DWORD dy, // vertical mouse position or position change 鼠标动作垂直坐标
DWORD dwData, // amount of wheel movement 当鼠标动作标识为MOUSEEVENTF_WHEEL时,此参数为鼠标滚轮转动方向,正值表示滚轮向前旋转,远离用户;负值表示滚轮向后旋转,向用户。
DWORD dwExtraInfo // 32 bits of application-defined information 设定一个32位值用于取扩展信息
);
鼠标动作标识:
MOUSEEVENTF_MOVE = $0001; { mouse move 鼠标移动}
MOUSEEVENTF_LEFTDOWN = $0002; { left button down 左键按下}
MOUSEEVENTF_LEFTUP = $0004; { left button up 左键弹起}
MOUSEEVENTF_RIGHTDOWN = $0008; { right button down 右键按下}
MOUSEEVENTF_RIGHTUP = $0010; { right button up 右键弹起}
MOUSEEVENTF_MIDDLEDOWN = $0020; { middle button down 中间按下}
MOUSEEVENTF_MIDDLEUP = $0040; { middle button up 中键弹起}
MOUSEEVENTF_WHEEL = $0800; { wheel button rolled 滚轮移动}
MOUSEEVENTF_ABSOLUTE = $8000; { absolute move 绝对移动}
其他操作:
var
SDC: HDC;
SColor: COLORREF;
begin
//鼠标移动到坐标X:20 Y:30初
Mouse.CursorPos := Point(20, 30);
//获取屏幕坐标X:20 Y:30初的颜色
SDC := GetDC(0);
SColor := GetPixel(SDC, 20, 30);
ReleaseDC(0, SDC);
//把取到的颜色画在屏幕上
Canvas.Brush.Color := SColor;
Canvas.FillRect(Rect(0, 0, 50, 50));