Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find Form handle from any handle

1,723 views
Skip to first unread message

Venu

unread,
Dec 26, 2006, 9:50:50 AM12/26/06
to
Hi,

Im overriding Application.OnMessage.

In that, I want to find the form handle for any message handle which goes
through.

How to find the form handle from any handle

By handle, I mean Windows message handle for a control or any window object.

GetParentForm(FindControl(Msg.Hwnd)) - doesnt seems to work for
a combobox control

regards


Venu


Russ

unread,
Dec 26, 2006, 10:50:24 AM12/26/06
to
Here is a little snippet that might answer some of your questions. I do
this in my mainform's Application.OnMessage handler. the global screen
variable also has a forms list that is very useful for iterating through all
the open forms.


if (screen.activeform<>nil) then
begin
getclassname(screen.activeform.handle,@tmpcstr[0],cClassNameLength);
if strpas(@tmpcstr[0])='Tnewappointmentform' then
begin
if
msg.hwnd=Tnewappointmentform(screen.activeform).timeproclistbox.Handle then
begin
case msg.message of
wm_mousemove,wm_lbuttondown,wm_lbuttonup,wm_rbuttondown,wm_rbuttonup:
begin
handled:=true;
end;
end;
// debugmsg('Listbox message - '+textwm_message(msg.message));
end;
end;
end;

Russ


"Venu" <ve...@hotmail.com> wrote in message
news:4591...@newsgroups.borland.com...

Rob Kennedy

unread,
Dec 26, 2006, 1:46:27 PM12/26/06
to
Venu wrote:
> Im overriding Application.OnMessage.
>
> In that, I want to find the form handle for any message handle which goes
> through.
>
> How to find the form handle from any handle

Try calling GetAncestor(Msg.HWnd, ga_Root). That will get you the root
parent window, which should be the handle of the form. Call FindControl
on that handle to get a reference to the form object.

> By handle, I mean Windows message handle for a control or any window object.
>
> GetParentForm(FindControl(Msg.Hwnd)) - doesnt seems to work for
> a combobox control

Does it work for everything else? Combo boxes are composed of at least
two windows -- an edit box and a list box. I think Delphi only
instruments the main container window handle, not the subwindows, which
could explain why FindControl doesn't work.

--
Rob

Rob Kennedy

unread,
Dec 26, 2006, 1:39:51 PM12/26/06
to
Russ wrote:
> Here is a little snippet that might answer some of your questions. I do
> this in my mainform's Application.OnMessage handler. the global screen
> variable also has a forms list that is very useful for iterating through all
> the open forms.
>
>
> if (screen.activeform<>nil) then
> begin
> getclassname(screen.activeform.handle,@tmpcstr[0],cClassNameLength);
> if strpas(@tmpcstr[0])='Tnewappointmentform' then
> begin

An easier way of writing all of the above lines is this:

if Screen.ActiveForm is TNewAppointmentForm then begin

Your method forces Delphi to construct and destroy an AnsiString for
every message that comes through your program. You can at least avoid
the string allocation by calling StrComp to compare the character array
instead of calling StrPas.

> if
> msg.hwnd=Tnewappointmentform(screen.activeform).timeproclistbox.Handle then
> begin
> case msg.message of
> wm_mousemove,wm_lbuttondown,wm_lbuttonup,wm_rbuttondown,wm_rbuttonup:
> begin
> handled:=true;
> end;
> end;

For such a focussed use, it may be more straightforward to just set the
WndProc property of the list box. That way, you're only running custom
code for one control, instead of checking the active form's ancestry for
*every* message that ever arrives for your program.

--
Rob

Remy Lebeau (TeamB)

unread,
Dec 26, 2006, 4:24:20 PM12/26/06
to

"Venu" <ve...@hotmail.com> wrote in message
news:4591...@newsgroups.borland.com...

> In that, I want to find the form handle for any message handle
> which goes through.

The MSG structure contains an HWND as a member. Pass that HWND to
FindControl(), which will return a TWinControl pointer. You can then use
the 'is' operator to see if that is a TForm, or pass it to GetParentForm()
if needed.

> GetParentForm(FindControl(Msg.Hwnd)) - doesnt seems to work
> for a combobox control

A ComboBox is actually made up of two inner controls - an Edit and a
ListBox. Most of the messages you receive for a ComboBox are actually meant
for those inner controls, which have no ties to the VCL at all. In that
situation, try using the Win32 API GetParent() function to get the HWND of
the ComboBox container itself. Then you should be able to use FindControl()
and GetParentForm() normally.


Gambit


Russ

unread,
Dec 26, 2006, 4:40:40 PM12/26/06
to
That was a snippet and not the whole handler, sorry for the confusion. It
is bracketed with a condition so that it only handles mouse and keyboard
messages which come glacially in computer terms.

I am an old win api programmer and old habits die hard. That code has been
in there a long time I'll look it over.

Russ


"Rob Kennedy" <m...@privacy.net> wrote in message
news:45916c4f$1...@newsgroups.borland.com...

0 new messages