PC版
搜索导航
论文网 > 计算机论文 > 计算机应用论文

利用热键控制鼠标移动的一个程序

- 本程序可以使用热键来将鼠标移动到某一个指定的坐标。是一个定义热键的示例程序。
本程序的热键为小键盘的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
  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

相关论文

鼠标程序
PBL模式下高职院校“C语言程序设计”
高职《C语言程序设计》课程教学中微课
基于混合教学模式的程序设计课程教学
《Java语言程序设计》教学改革探索
基于CDIO的主动式项目驱动教学法在Ja
程序设计类课程教学改革研究与实践
试论构建档案开放程序规制的必要性及
可视化实验平台在高校Java程序设计教
浅析我国死刑复核程序的完善
浅谈设置死刑复核程序的原因