获取点击的控件名称
作者:互联网
1.对于有句柄的控件,可以用一下代码
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
PageControl1: TPageControl;
ts1: TTabSheet;
ts2: TTabSheet;
procedure FormCreate(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure AppMsg(var Msg: TMsg; var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.AppMsg(var Msg: TMsg; var Handled: Boolean);
var
i:integer;
begin
case Msg.message of
WM_LBUTTONDOWN,
WM_LBUTTONDBLCLK:
begin
//拦截PageControl控件的Tab标签切换事件
if Msg.hwnd=PageControl1.Handle then
begin
for i:=0 to PageControl1.PageCount-1 do
begin
if PtInRect(PageControl1.TabRect(i),PageControl1.ScreenToClient(Msg.pt)) then
begin
Handled:=true;
ShowMessage(IntToStr(i));
end;
end;
end;
//拦截Button按钮点击事件
if Msg.hwnd=btn1.Handle then
begin
Handled:=true;
ShowMessage('bbbb');
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage:=AppMsg;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
ShowMessage('aaaa');
end;
end.
2.对于没有句柄的控件,可以通过矩形区域判断
var
Pt: TPoint;
MyRect: TRect;
begin
if (Msg.message = WM_LBUTTONUP) or (Msg.message = WM_RBUTTONUP) then
begin
GetCursorPos(Pt);
MyRect.TopLeft.X := OwnButton5.ClientOrigin.x;
MyRect.TopLeft.y := OwnButton5.ClientOrigin.y;
MyRect.BottomRight.X := MyRect.TopLeft.X +OwnButton5.Width;
MyRect.BottomRight.y := MyRect.TopLeft.y +OwnButton5.Height;
if not PtInRect(MyRect,Pt) then Panel14.Visible := False;
end;
end;
需要注意的是:窗口销毁时,如果应用程序需要继续运行,则要在窗口销毁时解除消息截获,即Application.OnMessage:=nil;
标签:控件,begin,end,TForm1,MyRect,var,获取,点击,Msg 来源: https://www.cnblogs.com/QuincyYi/p/15970357.html