Can't get HWnd of real parent form since usercontrol container changes during load 
Author Message
 Can't get HWnd of real parent form since usercontrol container changes during load

Hi All,

I have a usercontrol whose usercontrol_terminate event is not being
fired until app exit, even though the terminate event of its parents
is being fired. It's a private control so no references are being
passed out of the control. This is a situation with usercontrols
containing usercontrols etc. Anyway, I have determined that the
failure of this event to fire is causing a gpf on app exit, hence my
problem.

 Form unload fires
   ParentControl terminate fires
      MyUserControl terminate doesn't fire

My simple test case can't reproduce it so it's not a simple bug, it's
only happening in my app. Anyway, I have moved on from trying to
understand why my usercontrol is left floating in space until the app
exists instead of being terminated when the form unloads. Basically, I
need to get the parent form by recursively calling GetParent. Once I
hook the parent form's unload event, I can clear a reference which is
causing the GPF.

However, the parent HWnd that I receive when I call
UserControl.ContainerHwnd in the ReadProperties is not the same as
when the form is finally displayed. I am not doing anything tricky,
it's just a form containing controls which themselves contain
controls.

I checked this out in Spy++ and, during the ReadProperties event, the
control is being created in another temporary window with the name of
the application. Once the form is displayed, the control is reparented
to the form. It's like it assembles the control and then "pastes" it
on the form.

From the UserControl perspective, however, there is no event which
says "now you've been pasted on", nor any message I can subclass. I
can't see an option short of creating a timer and periodically
checking it to see if the control's top-level hwnd now points to
something resembling a real window. And if I close it before the timer
event fires... GPF. This is crap!

Any way to get around this?

Any help would be greatly appreciated.

Cheers,
Matt



Wed, 13 Apr 2005 12:01:55 GMT  
 Can't get HWnd of real parent form since usercontrol container changes during load
Hi All,

For people's future reference, I found out how to do this.

This code will return the incorrect parent form if called in the read
properties event:

' Returns the parent form's hwnd
Private Function GetFormHWnd() As Long
    Dim iHwnd As Long
    iHwnd = UserControl.hWnd
    Do Until GetParent(iHwnd) = 0
        iHwnd = GetParent(iHwnd)
    Loop
    GetFormHWnd = iHwnd
End Function

However, by placing a hook on the &H100E message for the usercontrol while
in the ReadProperties
event, and then calling the same function, it will then work correctly, and
the real parent form can be obtained.

(Now I just need to hook that parent's event and find out when it destroys
itself. Argh! All this to get
around the UserControl_Terminate not being fired!)

Hope this saves someone some time in the future.

cheers,
matt


Quote:
> Hi All,

> I have a usercontrol whose usercontrol_terminate event is not being
> fired until app exit, even though the terminate event of its parents
> is being fired. It's a private control so no references are being
> passed out of the control. This is a situation with usercontrols
> containing usercontrols etc. Anyway, I have determined that the
> failure of this event to fire is causing a gpf on app exit, hence my
> problem.

>  Form unload fires
>    ParentControl terminate fires
>       MyUserControl terminate doesn't fire

> My simple test case can't reproduce it so it's not a simple bug, it's
> only happening in my app. Anyway, I have moved on from trying to
> understand why my usercontrol is left floating in space until the app
> exists instead of being terminated when the form unloads. Basically, I
> need to get the parent form by recursively calling GetParent. Once I
> hook the parent form's unload event, I can clear a reference which is
> causing the GPF.

> However, the parent HWnd that I receive when I call
> UserControl.ContainerHwnd in the ReadProperties is not the same as
> when the form is finally displayed. I am not doing anything tricky,
> it's just a form containing controls which themselves contain
> controls.

> I checked this out in Spy++ and, during the ReadProperties event, the
> control is being created in another temporary window with the name of
> the application. Once the form is displayed, the control is reparented
> to the form. It's like it assembles the control and then "pastes" it
> on the form.

> From the UserControl perspective, however, there is no event which
> says "now you've been pasted on", nor any message I can subclass. I
> can't see an option short of creating a timer and periodically
> checking it to see if the control's top-level hwnd now points to
> something resembling a real window. And if I close it before the timer
> event fires... GPF. This is crap!

> Any way to get around this?

> Any help would be greatly appreciated.

> Cheers,
> Matt



Wed, 13 Apr 2005 12:23:17 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Parent UserControl's Properties in a UserControl (placed on another UserControl)

2. UserControl's Parent's BackColor Change?

3. UserControl's Parent's BackColor Change?

4. App.Path of the Usercontrol's Parent form

5. App.Path of the Usercontrol's Parent form

6. Trapping KeyStrokes inside a UserControl for it's parent form

7. Getting a reference to a form's objects from UserControl (OCX)

8. Getting Parent Container Size

9. Help: Change a MDI child's parent MDI form at run-time

10. How can I find the Hwnd number of a parent's child window

11. How can I find the Hwnd number of a parent's child window

12. Crystal report isn't updated in VB parent container

 

 
Powered by phpBB® Forum Software