Demonstrate how to accommodate ActiveX controls in the wtl window (Windows built-in IE (webbrowser) Controls ):
# Pragma once
# Include"Stdafx. h"
# Include <exdisp. h>
# Include <exdispid. h>
// ID of webbrowser Control
ConstUint id_webbrowser = 1;
TypedefCwintraits <ws_overlappedwindow | ws_clipchildren> cmainwintraits;
ClassCmainwindow:
PublicCsf-wimpl <cmainwindow, cwindow, cmainwintraits>,
PublicIdispeventimpl <id_webbrowser, cmainwindow>
{
Public:
Declare_wnd_class (_ T ("Wtl ActiveX host window"))
// Windows message handler
Begin_msg_map (cmainwindow)
Msg_wm_create (oncreate)
Msg_wm_destroy (ondestroy)
Msg_wm_size (onsize)
End_msg_map ()
// Webbrowser control event message handler
Begin_sink_map (cmainwindow)
Sink_entry (id_webbrowser, dispid_downloadbegin, ondownloadbegin)
Sink_entry (id_webbrowser, dispid_downloadcomplete, ondownloadcomplete)
End_sink_map ()
IntOncreate (maid)
{
Crect RC;
Getclientrect (& rc );
/*
// Create the webbrowser control (IE) with progid
Hwnd hwndie = m_ax.create (
M_hwnd,
RC,
_ T ("shell. assumer.2"), // progid of webbrowser Control
Ws_child | ws_visible,
0,
Id_webbrowser );
Atlassert (hwndie );
*/
// Create the webbrowser control (IE) with CLSID
Ccomptr <iunknown> spunkwebbrowser;
Hresult hR = spunkwebbrowser. cocreateinstance (clsid_webbrowser );
Checkhr (HR );
// Create the ActiveX host window
Hwnd hwndwb = m_ax.create (
M_hwnd,
RC,
Null,
Ws_child | ws_visible,
0,
Id_webbrowser );
Atlassert (hwndwb );
// Attach the webbrowser control to host window
Ccomptr <iunknown> spunkcontainer;
M_ax.attachcontrol (spunkwebbrowser, & spunkcontainer );
// Advise sink Map
Atladvisesinkmap (This,True);
// Query interface
HR = m_ax.querycontrol (& m_spiwebbrowser );
Checkhr (HR );
Ccombstr site (_ T ("Http://www.hust.edu.cn"));
Ccomvariant V;
HR = m_spiwebbrowser-> navigate (site, & V, & V );
Checkhr (HR );
Return0;
}
VoidOndestroy ()
{
// Unadvise sink Map
Atladvisesinkmap (This,False);
Postquitmessage (0 );
}
VoidOnsize (uint ntype, csize size)
{
Crect RC;
Getclientrect (& rc );
M_ax.setwindowpos (m_hwnd, & rc, swp_nozorder | swp_noactivate );
}
Void_ Stdcall ondownloadbegin ()
{
Setwindowtext (_ T ("Download begin:"));
}
Void_ Stdcall ondownloadcomplete ()
{
Setwindowtext (_ T ("Download completed."));
}
Private:
Caxwindow m_ax;
Ccomptr <iwebbrowser2> m_spiwebbrowser;
};
Effect: