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
Button2Click(Sender: TObject);
021
Button3Click(Sender: TObject);
022
FormMouseMove(Sender: TObject; Shift: TShiftState; X,
023
Y:
Integer
);
024
Button4Click(Sender: TObject);
025
Timer1Timer(Sender: TObject);
026
Button6Click(Sender: TObject);
027
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
TForm1
.
038
pt: TPoint;
039
begin
040
//模擬左鍵單擊
041
If
RadioButton1
Checked
Then
042
Begin
043
pt:= Edit1
ClIEntToScreen( Point(
4
,
));
//鼠標定位
044
SetCursorPos( pt
x, pt
y );
045
mouse_event( MOUSEEVENTF_LEFTDOWN,
0
046
mouse_event( MOUSEEVENTF_LEFTUP,
047
048
//模擬右鍵單擊
049
RadioButton2
050
051
ClIEntToScreen(Point(
052
053
mouse_event( MOUSEEVENTF_RightDOWN,
054
mouse_event( MOUSEEVENTF_RightUP,
055
056
057
058
btn3Rect: TRect;
059
060
btn3Rect := Button3
BoundsRect;
061
MapWindowPoints(handle,
, btn3Rect,
2
// 坐標換算
062
ClipCursor(@btn3Rect);
// 限制鼠標移動區域
063
064
065
btScreen: TRect;
066
067
btScreen := Rect(
, Screen
Width, Screen
Height);
068
ClipCursor(@btScreen);
//解瑣,使鼠標在整個屏幕有效
069
070
071
072
073
cursor:=crNone;
//2秒鐘沒有移動鼠標就在當前窗體隱藏鼠標
074
075
076
077
078
079
if
Timer1
Enabled=
True
then
//判斷定時器是否打開
080
//如打開,則重新開始計時
081
Enabled:=
False
082
083
084
cursor:=crDefault;
//鼠標移動時,改變光標類型為缺省類型
085
086
087
088
mouseinfo:
string
089
090
//檢測是否安裝了鼠標
091
(GetSystemMetrics(SM_MOUSEPRESENT)<>
)
092
mouseinfo:=
'1.系統檢測到鼠標;'
093
Else
'1.系統沒有鼠標;'
094
//檢測鼠標是否支持滑動
095
(GetSystemMetrics(SM_MOUSEWHEELPRESENT)<>
096
mouseinfo:=mouseinfo+
'2.系統支持滑動鼠標。'
097
'2.系統不支持滑動鼠標。'
098
Label1
Caption:=mouseinfo;
099
100
101
102
SwapMouseButton(
//交換左右鍵
103
104
105
106
//復原左右鍵
107
108
SetBlendTriangularShape
//聲明: PolyBezIErTo( DC: HDC;
相關函數: GetWindowOrgEx、SetView
本例效果圖: 代碼文件:unit Unit1;inte
本例效果圖: 代碼文件:un