API - SetCapture(),ReleaseCapture与GetCapture - 鼠标移动的捕获与释放

演示效果:

添加控件:Form1,Button1,Label1,Label2,Timer1

GetCapture:返回当前捕获鼠标输入的窗口的句柄
SetCapture():通过句柄设置指定窗口来捕获鼠标的输入
ReleaseCapture:释放当前捕获鼠标的窗口的捕获功能
同一时间只能有一个前景窗口可以捕获鼠标。当在控件上按下鼠标时,会自动执行SetCapture(),这时该控件的鼠标移动事件在全屏都会触发,松开鼠标按键时自动执行ReleaseCapture释放捕获

代码文件:

 


unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; Label2: TLabel; Timer1: TTimer; procedure Timer1Timer(Sender: TObject); procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Timer1Timer(Sender: TObject); begin if GetCapture=Button1.Handle then Label2.Caption:='Get' else Label2.Caption:='Release' end; procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Label1.Caption:=Format('%d,%d',[x,y]); end; end.

窗体文件:


object Form1: TForm1 Left = 412 Top = 202 Width = 262 Height = 164 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 16 Top = 8 Width = 24 Height = 13 Caption = '坐标' end object Label2: TLabel Left = 16 Top = 32 Width = 32 Height = 13 Caption = 'Label2' end object Button1: TButton Left = 16 Top = 56 Width = 75 Height = 57 Caption = 'Button1' TabOrder = 0 OnMouseMove = Button1MouseMove end object Timer1: TTimer Interval = 100 OnTimer = Timer1Timer Left = 208 Top = 8 end end
posted @ 2009-03-17 22:19  Record drip  阅读(3404)  评论(0编辑  收藏  举报