本程序可以使用热键来将鼠标移动到某一个指定的坐标。是一个定义热键的示例
程序。
本
程序的热键为小键盘的5,在编辑框内可以指定坐标。
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;
type
TForm1 = class(TForm)
btnSetHK: TButton;
btnExit: TButton;
GroupBox2: TGroupBox;
Label3: TLabel;
Label4: TLabel;
btnUnsetHK: TButton;
edYPos: TMaskEdit;
edXPos: TMaskEdit;
Memo: TMemo;
procedure btnExitClick(Sender: TObject);
procedure btnSetHKClick(Sender: TObject);
procedure btnUnsetHKClick(Sender: TObject);
procedure OnHotKey(var Message: TWMHOTKEY); message WM_HOTKEY;
procedure FormDestroy(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
const
idHotKey : WORD = 0;
implementation
{$R *.DFM}
procedure TForm1.btnExitClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.btnSetHKClick(Sender: TObject);
begin
if idHotKey<>0 then Exit;
idHotKey := GlobalAddAtom(''EmuMouse''); //给热键取得一个唯一的标识
RegisterHotKey(Handle, idHotKey, 0, VK_NUMPAD5); //
注册热键
end;
procedure TForm1.OnHotKey(var Message: TWMHOTKEY);
var
Point: TPoint;
X, Y: Word;
begin
GetCursorPos(Point); //取回当前坐标
try
X := StrToInt(edXPos.Text);
Y := StrToInt(edYPos.Text);
except
ShowMessage(''坐标输入不正确.'');
Exit;
end;
try
Mouse_Event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTDOWN, Point.X, Point.Y, 0, GetMessageExtraInfo);
SetCursorPos(X, Y);
Mouse_Event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTUP, X, Y, 0, GetMessageExtraInfo);
except
ShowMessage(''Error'');
end;
end;
procedure TForm1.btnUnsetHKClick(Sender: TObject);
begin
if idHotKey = 0 then Exit;
UnRegisterHotKey(Handle, idHotKey); //注销热键
DeleteAtom(idHotKey); //注销标识
idHotKey := 0;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
btnUnsetHK.Click;
end;
end.
--------------------------------------------------------------------------------
这是dfm文件
object Form1: TForm1
Left = 296
Top = 238
AutoSize = True
BorderStyle = bsDialog
BorderWidth = 8
Caption = ''模拟鼠标拖动''
ClientHeight = 265
ClientWidth = 211
Color = clBtnFace
&