Delphi锁定鼠标的方法
2个回答
2017-11-16 · 知道合伙人互联网行家
关注
展开全部
Delphi设置鼠标信息,当鼠标静止无操作时会自动隐藏鼠标,锁定鼠标,切换左键和右键,交换鼠标,模拟鼠标左右键单击效果,以下是主要的代码。
001 unit Unit1;
002 interface
003 uses
004 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
005 Dialogs, StdCtrls, ImgList, ComCtrls, ExtCtrls;
006 type
007 TForm1 = class(TForm)
008 Button1: TButton;
009 Edit1: TEdit;
010 Button2: TButton;
011 Button3: TButton;
012 Button4: TButton;
013 Timer1: TTimer;
014 Button6: TButton;
015 Label1: TLabel;
016 RadioButton1: TRadioButton;
017 RadioButton2: TRadioButton;
018 Button5: TButton;
019 procedure Button1Click(Sender: TObject);
020 procedure Button2Click(Sender: TObject);
021 procedure Button3Click(Sender: TObject);
022 procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
023 Y: Integer);
024 procedure Button4Click(Sender: TObject);
025 procedure Timer1Timer(Sender: TObject);
026 procedure Button6Click(Sender: TObject);
027 procedure Button5Click(Sender: TObject);
028 private
029 { Private declarations }
030 public
031 { Public declarations }
032 end;
033 var
034 Form1: TForm1;
035 implementation
036 {$R *.dfm}
037 procedure TForm1.Button1Click(Sender: TObject);
038 var pt: TPoint;
039 begin
040 //模拟左键单击
041 If RadioButton1.Checked Then
042 Begin
043 pt:= Edit1.ClientToScreen( Point(4,4)); //鼠标定位
044 SetCursorPos( pt.x, pt.y );
045 mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
046 mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
047 end;
048 //模拟右键单击
049 If RadioButton2.Checked Then
050 Begin
051 pt:= Edit1.ClientToScreen(Point(4,4)); //鼠标定位
052 SetCursorPos( pt.x, pt.y );
053 mouse_event( MOUSEEVENTF_RightDOWN, 0, 0, 0, 0 );
054 mouse_event( MOUSEEVENTF_RightUP, 0, 0, 0, 0 );
055 end;
056 end;
057 procedure TForm1.Button2Click(Sender: TObject);
058 var btn3Rect: TRect;
059 begin
060 btn3Rect := Button3.BoundsRect;
061 MapWindowPoints(handle,0, btn3Rect, 2); // 坐标换算
062 ClipCursor(@btn3Rect); // 限制鼠标移动区域
063 end;
064 procedure TForm1.Button3Click(Sender: TObject);
065 var btScreen: TRect;
066 begin
067 btScreen := Rect(0, 0, Screen.Width, Screen.Height);
068 ClipCursor(@btScreen); //解琐,使鼠标在整个屏幕有效
069 end;
070
071 procedure TForm1.Timer1Timer(Sender: TObject);
072 begin
073 cursor:=crNone; //2秒钟没有移动鼠标就在当前窗体隐藏鼠标
074 end;
075
076 procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
077 Y: Integer);
078 begin
079 if Timer1.Enabled=True then //判断定时器是否打开
080 begin //如打开,则重新开始计时
081 Timer1.Enabled:=False;
082 Timer1.Enabled:=True;
083 end;
084 cursor:=crDefault; //鼠标移动时,改变光标类型为缺省类型
085 end;
086
087 procedure TForm1.Button6Click(Sender: TObject);
088 var mouseinfo:string;
089 begin
090 //检测是否安装了鼠标
091 If (GetSystemMetrics(SM_MOUSEPRESENT)<>0) Then
092 mouseinfo:='1.系统检测到鼠标;'
093 Else mouseinfo:='1.系统没有鼠标;';
094 //检测鼠标是否支持滑动
095 If (GetSystemMetrics(SM_MOUSEWHEELPRESENT)<> 0) Then
096 mouseinfo:=mouseinfo+'2.系统支持滑动鼠标。'
097 Else mouseinfo:=mouseinfo+'2.系统不支持滑动鼠标。';
098 Label1.Caption:=mouseinfo;
099 end;
100 procedure TForm1.Button4Click(Sender: TObject);
101 begin
102 SwapMouseButton(True); //交换左右键
103 end;
104 procedure TForm1.Button5Click(Sender: TObject);
105 begin
106 SwapMouseButton(False); //复原左右键
107 end;
108 end.
001 unit Unit1;
002 interface
003 uses
004 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
005 Dialogs, StdCtrls, ImgList, ComCtrls, ExtCtrls;
006 type
007 TForm1 = class(TForm)
008 Button1: TButton;
009 Edit1: TEdit;
010 Button2: TButton;
011 Button3: TButton;
012 Button4: TButton;
013 Timer1: TTimer;
014 Button6: TButton;
015 Label1: TLabel;
016 RadioButton1: TRadioButton;
017 RadioButton2: TRadioButton;
018 Button5: TButton;
019 procedure Button1Click(Sender: TObject);
020 procedure Button2Click(Sender: TObject);
021 procedure Button3Click(Sender: TObject);
022 procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
023 Y: Integer);
024 procedure Button4Click(Sender: TObject);
025 procedure Timer1Timer(Sender: TObject);
026 procedure Button6Click(Sender: TObject);
027 procedure Button5Click(Sender: TObject);
028 private
029 { Private declarations }
030 public
031 { Public declarations }
032 end;
033 var
034 Form1: TForm1;
035 implementation
036 {$R *.dfm}
037 procedure TForm1.Button1Click(Sender: TObject);
038 var pt: TPoint;
039 begin
040 //模拟左键单击
041 If RadioButton1.Checked Then
042 Begin
043 pt:= Edit1.ClientToScreen( Point(4,4)); //鼠标定位
044 SetCursorPos( pt.x, pt.y );
045 mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
046 mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
047 end;
048 //模拟右键单击
049 If RadioButton2.Checked Then
050 Begin
051 pt:= Edit1.ClientToScreen(Point(4,4)); //鼠标定位
052 SetCursorPos( pt.x, pt.y );
053 mouse_event( MOUSEEVENTF_RightDOWN, 0, 0, 0, 0 );
054 mouse_event( MOUSEEVENTF_RightUP, 0, 0, 0, 0 );
055 end;
056 end;
057 procedure TForm1.Button2Click(Sender: TObject);
058 var btn3Rect: TRect;
059 begin
060 btn3Rect := Button3.BoundsRect;
061 MapWindowPoints(handle,0, btn3Rect, 2); // 坐标换算
062 ClipCursor(@btn3Rect); // 限制鼠标移动区域
063 end;
064 procedure TForm1.Button3Click(Sender: TObject);
065 var btScreen: TRect;
066 begin
067 btScreen := Rect(0, 0, Screen.Width, Screen.Height);
068 ClipCursor(@btScreen); //解琐,使鼠标在整个屏幕有效
069 end;
070
071 procedure TForm1.Timer1Timer(Sender: TObject);
072 begin
073 cursor:=crNone; //2秒钟没有移动鼠标就在当前窗体隐藏鼠标
074 end;
075
076 procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
077 Y: Integer);
078 begin
079 if Timer1.Enabled=True then //判断定时器是否打开
080 begin //如打开,则重新开始计时
081 Timer1.Enabled:=False;
082 Timer1.Enabled:=True;
083 end;
084 cursor:=crDefault; //鼠标移动时,改变光标类型为缺省类型
085 end;
086
087 procedure TForm1.Button6Click(Sender: TObject);
088 var mouseinfo:string;
089 begin
090 //检测是否安装了鼠标
091 If (GetSystemMetrics(SM_MOUSEPRESENT)<>0) Then
092 mouseinfo:='1.系统检测到鼠标;'
093 Else mouseinfo:='1.系统没有鼠标;';
094 //检测鼠标是否支持滑动
095 If (GetSystemMetrics(SM_MOUSEWHEELPRESENT)<> 0) Then
096 mouseinfo:=mouseinfo+'2.系统支持滑动鼠标。'
097 Else mouseinfo:=mouseinfo+'2.系统不支持滑动鼠标。';
098 Label1.Caption:=mouseinfo;
099 end;
100 procedure TForm1.Button4Click(Sender: TObject);
101 begin
102 SwapMouseButton(True); //交换左右键
103 end;
104 procedure TForm1.Button5Click(Sender: TObject);
105 begin
106 SwapMouseButton(False); //复原左右键
107 end;
108 end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询