Article View: borland.public.cppbuilder.nativeapi
Article #14817Re: Trapping button message?
From: Remy Lebeau (TeamB)
Date: Wed, 30 Jul 2003 16:43
Date: Wed, 30 Jul 2003 16:43
79 lines
3892 bytes
3892 bytes
"Mattias Carleson" <mattias.carleson@commentor.se> wrote in message
news:3f27eaec$1@newsgroups.borland.com...
> The trapping seems to work, but the ControlAtPos()
> doesnt return anything but NULL although the coordinates
> in 'TPoint click' is within the TSpeedButtons limits. Any
> idea why? My speedbutton is placed upon a TCoolBar.
Calling ControlAtPos() within the context of the form object will find the
TCoolBar itself, not the TSpeedButton. However, you are specifying false
for the 'AllowWinControls' parameter, so actually, ControlAtPos() will not
return the TCoolBar either at all.
The TSpeedButton is a child of the TCoolBar, so you will have to obtain the
pointer to the TCoolBar first and then call ControlAtPos() a second time
within the context of the TCoolBar object. Make sure you are specifying
'true', not 'false', for the AllowWinControls parameter of ControlAtPos().
Because of the possible parent/child hierarchy invoked with finding
controls, I would suggest you wrap the searching into a helper function, ie:
TControl* __fastcall FindControlAtPos(TWinControl *WinControl, TPoint
&pt)
{
TPoint click = WinControl->ScreenToClient(pt);
TControl* ctrl = WinControl->ControlAtPos(click, false, true);
if( ctrl )
{
TWinControl* wc = dynamic_cast<TWinControl*>(ctrl);
if( wc )
return FindControlAtPos(wc, pt);
return ctrl;
}
return NULL;
}
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if(Msg.message == WM_LBUTTONDOWN)
{
if( dynamic_cast<TSpeedButton*>(FindControlAtPos(this,
Msg.pt)) )
ShowMessage("Button event catched!");
}
}
If you do not want the search function to be generic, but rather to find
specific types only, then you could wrap it in a template, ie:
template<typename T>
T* __fastcall FindControlAtPos(TWinControl *WinControl, TPoint &pt)
{
TPoint click = WinControl->ScreenToClient(pt);
TControl* ctrl = WinControl->ControlAtPos(click, false, true);
if( ctrl )
{
T* pT = dynamic_cast<T*>(ctrl);
if( pT )
return pT;
TWinControl* wc = dynamic_cast<TWinControl*>(ctrl);
if( wc )
return FindControlAtPos<T>(wc, pt);
}
return NULL;
}
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if(Msg.message == WM_LBUTTONDOWN)
{
TSpeedButton* btn = FindControlAtPos<TSpeedButton>(this,
Msg.pt);
if( btn )
ShowMessage("Button event catched!");
}
}
Gambit
Technical Details
Message-ID:
<3f27f465@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-09!supernews.com!wn14feed!wn13feed!worldnet.att.net!216.166.71.14!border3.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f26ea7c$1@newsgroups.borland.com> <3f27c07b$1@newsgroups.borland.com> <3f27eaec$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,77c37ce9ffa755d1 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-07-30 09:40:04 PST From: "Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f26ea7c$1@newsgroups.borland.com> <3f27c07b$1@newsgroups.borland.com> <3f27eaec$1@newsgroups.borland.com> Subject: Re: Trapping button message? Date: Wed, 30 Jul 2003 09:43:46 -0700 Lines: 81 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 207.175.226.26 Message-ID: <3f27f465@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1059583077 207.175.226.26 (30 Jul 2003 09:37:57 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-06!sn-xit-09!supernews.com!wn14feed!wn13feed!worldnet.att.net!216.166.71.14!border3.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail