OnCreate & OnDestroy callback in juce::Component?



Is there something equivalent to [color=#4040BF]CWnd::OnCreate()[/color] and [color=#4040BF]CWnd::OnDestroy()[/color] in class Component ?

I have a class which derives from Component and which needs to do some stuff with the HWND of the Component.

codegetPeer()->getNativeHandle()[/code] allows to get the HWND, but this method cannot do miracles : as long as windows hasn’t initialized the hwnd, it returns NULL. Windows does however initialise the HWND before OnCreate() is called (and doesn’t destroy it until OnDestroy() is called), hence my question : how can I get a callback of these two methods.

Thank you in advance for your help !



Well, no, there’s nothing that would explicitly receive that win32 callback… Whenever I need to do something with the native handle I generally just wait until the component is visible, and then you know that the hwnd is ready, and can use it…


Thanks for your help.

So, for anyone who reads this, a couple more things that may hopefully help you.

I was actually trying to get the HWND of my component’s parent’s parent’s parent… In such a configuration getPeer() was relying on an unbroken chain of parents up to the top level component. This is not such a hard condition to fulfill, except in constructors. This is actually one more reason why you cannot get the HWND in the constructor’s scope.

What worked for me was accessing the HWND in [color=#0080BF]resized()[/color], storing it in a member, and doing the “release-all-stuff-that-is connected with HWND”-job in destructor.

Making it in [color=#00BF00]visibilityChanged() is, in my opinion, a bad move[/color], as it possible that :
1° the visible flag of your component doesn’t change, but one of its parent’s visibility changes. As a result, your component’s “showing” (in juce’s sense) has changed, but your (child) component’s visibilityChanged isn’t called, and you don’t get a chance to the job you want with the HWND.
2° you simply are set to visible when you don’t have a parent yet, and therefore cannot get the HWND
[color=#505050]I’d like to focus on point 2° for a sec… Of course, one could say, it is possible to call resized() on a component before giving it a parent BUT : my mother always told me to add child components at construction, and call stuff like child->setBounds() NEVER at construction. My mother means it, of course, as a general rule which suffers exceptions, but if you are having the same habit than my mother and me, getting the hWnd in resized() looks like a better choice.

I hope it helps…

Please feel free to correct me if I say anything wrong…



There’s also a class called ComponentMovementWatcher that’s useful for finding out when a component’s peer changes.