1. 程式人生 > 其它 >獲取點選的控制元件名稱

獲取點選的控制元件名稱

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;
procedureFormCreate(Sender:TObject);
procedurebtn1Click(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
procedureAppMsg(varMsg:TMsg;varHandled:Boolean);
end;

var
Form1:TForm1;

implementation

{$R*.dfm}

{TForm1}
procedureTForm1.AppMsg(varMsg:TMsg;varHandled:Boolean);
var
i:integer;
begin
caseMsg.messageof
WM_LBUTTONDOWN,
WM_LBUTTONDBLCLK:
begin
//攔截PageControl控制元件的Tab標籤切換事件
ifMsg.hwnd=PageControl1.Handlethen
begin
fori:=0toPageControl1.PageCount-1do
begin
ifPtInRect(PageControl1.TabRect(i),PageControl1.ScreenToClient(Msg.pt))then
begin
Handled:=true;
ShowMessage(IntToStr(i));
end;
end;
end;

//攔截Button按鈕點選事件
ifMsg.hwnd=btn1.Handlethen
begin
Handled:=true;
ShowMessage('bbbb');
end;
end;
end;
end;

procedureTForm1.FormCreate(Sender:TObject);
begin
Application.OnMessage:=AppMsg;
end;

procedureTForm1.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;
ifnotPtInRect(MyRect,Pt)thenPanel14.Visible:=False;
end;
end;
需要注意的是:視窗銷燬時,如果應用程式需要繼續執行,則要在視窗銷燬時解除訊息截獲,即Application.OnMessage:=nil;

一花獨放不是春,百花齊放春滿園! 讓機器視覺應用滿天下,解放人類的眼睛和大腦!