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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗