How to resize an ActiveX window or control.
June 24th, 2008Resizing an ActiveX window is hard. That’s all there is to it. If you try using the nifty SetWindowPos function, you will indeed see the ActiveX window resize, but as soon as you try using it, it will pop back to it’s original size. To successfully resize it, involves using COM, smart pointers and an obscure object known as IUnknownPtr. Heh, IUnknownPtr, brilliant name! One things for sure, it’s unknown to me!
Building this function, and getting it to work and compile was one of the worst programming experiences I have ever had. COM and smart pointers are some of the most infuriating subjects in the world of computer programming. All I needed was a little information to get started, and there simply was little to no information available online (about defining the IUnknownPtr object) . I thankfully, after hours of searching, found an obscure forum post, showing how to define the IUnknownPtr object, or whatever it is. Something so simple, yet so insanely difficult to find. I don’t mind telling you that this experience, plus a few others, have left a very bad taste in my mouth, when it comes to COM. There is simply no reason, in this day and age, for something like COM, to be so convoluted and archaic. Avoid COM, it’s bad news.
Unfortunately for us however, using COM is the only way to resize an ActiveX window, so we must use the wretched thing. Well, here is what I finally came up with, enjoy.
// This is the magical line that defines IUnknownPtr, simply unbelievable how hard this was to find. typedef _com_ptr_t<_com_IIID> IUnknownPtr; void SizeActiveXWindow(CWnd &ctrl, CRect newRect) { IUnknownPtr unk(ctrl.GetControlUnknown(), true); IOleObjectPtr oleo=unk; IOleClientSitePtr olecs; if (oleo!=0) oleo->GetClientSite(&olecs); IOleInPlaceSitePtr olips=olecs; ctrl.MoveWindow(newRect); if (olips!=0) olips->OnPosRectChange(&newRect); }