问题
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?
回答1:
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)
回答2:
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.
回答3:
[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.
来源:https://stackoverflow.com/questions/413425/how-do-i-get-the-hwnd-for-an-activex-control-after-the-control-has-been-initiali