利用熱鍵控制滑鼠移動的一個程式 (轉)

worldblog發表於2007-12-10
利用熱鍵控制滑鼠移動的一個程式 (轉)[@more@]

本可以使用熱鍵來將滑鼠移動到某一個指定的座標。是一個定義熱鍵的示例程式。

本程式的熱鍵為小鍵盤的5,在編輯框內可以指定座標。

 

unit MainUnit;

interface

uses
  , 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: T);
  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 : = 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 = ialog
  BorderWidth = 8
  Caption = '模擬滑鼠拖動'
  ClientHeight = 265
  ClientWidth = 211
  Color = clBtnFace
  Font.Charset = GB2312_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = '新宋體'
  Font.Style = []
  OldCreateOrder = False
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 12
  object btnSetHK: TButton
  Left = 136
  Top = 8
  Width = 75
  Height = 25
  Caption = '設定熱鍵(&H)'
  TabOrder = 0
  OnClick = btnSetHKClick
  end
  object btnExit: TButton
  Left = 136
  Top = 72
  Width = 75
  Height = 25
  Caption = '退出(&X)'
  TabOrder = 2
  OnClick = btnExitClick
  end
  object GroupBox2: TGroupBox
  Left = 0
  Top = 0
  Width = 129
  Height = 97
  Caption = '目的座標'
  TabOrder = 3
  object Label3: TLabel
  Left = 16
  Top = 29
  Width = 6
  Height = 12
  Caption = 'X'
  end
  object Label4: TLabel
  Left = 16
  Top = 61
  Width = 6
  Height = 12
  Caption = 'Y'
  end
  object edXPos: TMaskEdit
  Left = 32
  Top = 24
  Width = 73
  Height = 20
  EditMask = '0000;1;_'
  MaxLength = 4
  TabOrder = 0
  Text = '0000'
  end
  object edYPos: TMaskEdit
  Left = 32
  Top = 56
  Width = 73
  Height = 20
  EditMask = '0000;1;_'
  MaxLength = 4
  TabOrder = 1
  Text = '0000'
  end
  end
  object btnUnsetHK: TButton
  Left = 136
  Top = 40
  Width = 75
  Height = 25
  Caption = '取消熱鍵(&U)'
  TabOrder = 1
  OnClick = btnUnsetHKClick
  end
  object Memo: TMemo
  Left = 0
  Top = 104
  Width = 209
  Height = 161
  TabOrder = 4
  end
end

 

 

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-990860/,如需轉載,請註明出處,否則將追究法律責任。

相關文章