频道分类

Delphi锁定鼠标 模拟左右键 静止一会自动隐藏鼠标

作者:admin 来源: 日期:2019/10/18 12:19:18 人气: 标签:

 
Delphi锁定鼠标 模拟左右键 静止一会自动隐藏鼠标
unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ImgList, ComCtrls, ExtCtrls;

type

  TForm1 = class(TForm)

    Button1: TButton;

    Edit1: TEdit;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    Timer1: TTimer;

    Button6: TButton;

    Label1: TLabel;

    RadioButton1: TRadioButton;

    RadioButton2: TRadioButton;

    Button5: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,

      Y: Integer);

    procedure Button4Click(Sender: TObject);

    procedure Timer1Timer(Sender: TObject);

    procedure Button6Click(Sender: TObject);

    procedure Button5Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var  pt: TPoint;

begin  

   //模拟左键单击

  If RadioButton1.Checked Then

   Begin

    pt:= Edit1.ClientToScreen( Point(4,4));   //鼠标定位

    SetCursorPos( pt.x, pt.y );

    mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );

    mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );

   end;

   //模拟右键单击

  If RadioButton2.Checked Then

   Begin

    pt:= Edit1.ClientToScreen(Point(4,4));       //鼠标定位

    SetCursorPos( pt.x, pt.y );

    mouse_event( MOUSEEVENTF_RightDOWN, 0, 0, 0, 0 );

    mouse_event( MOUSEEVENTF_RightUP, 0, 0, 0, 0 );

   end;

 end;

procedure TForm1.Button2Click(Sender: TObject);

var btn3Rect: TRect;

begin

 btn3Rect := Button3.BoundsRect;

 MapWindowPoints(handle,0, btn3Rect, 2);  // 坐标换算

 ClipCursor(@btn3Rect);                   // 限制鼠标移动区域

end;

procedure TForm1.Button3Click(Sender: TObject);

var btScreen: TRect;

begin

 btScreen := Rect(0, 0, Screen.Width, Screen.Height);

 ClipCursor(@btScreen);       //解琐,使鼠标在整个屏幕有效

end;

 

procedure TForm1.Timer1Timer(Sender: TObject);

begin

  cursor:=crNone;   //2秒钟没有移动鼠标就在当前窗体隐藏鼠标

end;

 

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

  Y: Integer);

begin

if Timer1.Enabled=True  then //判断定时器是否打开

begin                        //如打开,则重新开始计时

 Timer1.Enabled:=False;

 Timer1.Enabled:=True;

 end;

 cursor:=crDefault;          //鼠标移动时,改变光标类型为缺省类型

end;

 

procedure TForm1.Button6Click(Sender: TObject);

var mouseinfo:string;

begin

    //检测是否安装了鼠标

 If (GetSystemMetrics(SM_MOUSEPRESENT)<>0) Then

     mouseinfo:='1.系统检测到鼠标;'

  Else  mouseinfo:='1.系统没有鼠标;';

   //检测鼠标是否支持滑动

 If (GetSystemMetrics(SM_MOUSEWHEELPRESENT)<> 0) Then

     mouseinfo:=mouseinfo+'2.系统支持滑动鼠标。'

  Else  mouseinfo:=mouseinfo+'2.系统不支持滑动鼠标。';

Label1.Caption:=mouseinfo;

end;

procedure TForm1.Button4Click(Sender: TObject);

begin

  SwapMouseButton(True);    //交换左右键

end;

procedure TForm1.Button5Click(Sender: TObject);

begin

   SwapMouseButton(False); //复原左右键

end;

end.