tags:

views:

1338

answers:

3

I am creating an ATL 8.0 based ActiveX control in C++ using Visual Studio 2008. I need to create a sub-window and attach it to the ActiveX control.

How do I get access to the HWND that is owned by the ActiveX control?

Which ATL function can I override in order to use the HWND after the control's window has been created?

A: 

[Full Disclosure]: I'm not that familiar with ActiveX or ATL, but I hope this is at least somewhat helpful.

If ActiveX allows you to define arbitrary methods on your object, try to expose a function that you can call that will simply return the value of the HWND to you (the control almost certainly knows its own HWND). That way you can call GetActiveXHwnd() to get the necessary handle, which you would then use for further manipulation.

Brian
Yeah sure it knows it's own HWND, it just never seems to get initialised. I can add a function to return the m_hWnd member but it will just return NULL. I want to know how and when the HWND is supposed to be initialised?
Ashley Davis
+1  A: 

ActiveX would allow you to define your own methods on your own interface (to address Brians assumption), but that likely won't help here. The ActiveX control might very well be created by another component. ATL too is irrelevant - it's a C++ template library that wraps COM interfaces.

The function you need here is IOleWindow::GetWindow. I'm not sure what you mean by "override an ATL function to use the HWMD". Once you have retrieved the HWND, you can pass it to any function that uses an HWND. For instance, as the parent in SetParent(child, parent)

MSalters
The wizard generates an ATL ActiveX control that has (thru a base class) a m_hWnd member. However this always appears to be NULL, which is my problem. How and when is this supposed to be initialised?
Ashley Davis
+2  A: 

After some trial and error and I found the answer I was after.

In the constructor of your ATL ActiveX control you to add the following line of code:

m_bWindowOnly = true;

This causes the window for the control to be created (rather than just reusing the HWND of the parent window). After this the m_hWnd member of the control class can be used to access the HWND for the control's window.

Ashley Davis
Just been bashing my head against a wall - and this solved it - do you remember where you read about this?
freefallr
I can't remember sorry, I may have just figured it out through experimentation. I had a lot of trouble finding relevant ActiveX information.
Ashley Davis