📄 wtl for mfc programmers, part vi.mht
字号:
to stop </SPAN>
<SPAN class=3Dcpp-comment>// navigates to evil tracking companies =
like doubleclick.net:</SPAN>
<SPAN class=3Dcpp-keyword>if</SPAN> ( sURL.Find ( _T(<SPAN =
class=3Dcpp-string>"doubleclick.net"</SPAN>) ) > <SPAN =
class=3Dcpp-literal>0</SPAN> )
*Cancel =3D VARIANT_TRUE;
}</PRE>
<P>Here's what the app looks like while viewing the Lounge:</P>
<P><IMG height=3D479 alt=3D" [Sample app - 13K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC6/sampleapp.png" =
width=3D438=20
align=3Dbottom border=3D0></P>
<P>IEHoster demonstrates several other WTL features that have been =
covered=20
in earlier articles: <CODE>CBitmapButton</CODE> (for the browser =
control=20
buttons), <CODE>CListViewCtrl</CODE> (for the event logging), DDX =
(to keep=20
track of the checkbox states), and <CODE>CDialogResize</CODE>.</P>
<H2><A name=3Druntimecreating></A>Creating an ActiveX Control at =
Run=20
Time</H2>
<P>It is also possible to create an ActiveX control at run time, =
instead=20
of in the resource editor. The About box demonstrates this =
technique. The=20
dialog resource contains a placeholder group box that marks where =
the=20
browser control will go:</P>
<P><IMG height=3D290 alt=3D" [About box in editor - 5K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC6/aboutph.png" =
width=3D322=20
align=3Dbottom border=3D0></P>
<P>In <CODE>OnInitDialog()</CODE>, we use <CODE>CAxWindow</CODE> =
to create=20
a new <CODE>AtlAxWin</CODE> in the same <CODE>RECT</CODE> as the=20
placeholder (which is then destroyed):</P><PRE>LRESULT =
CAboutDlg::OnInitDialog(...)
{
CWindow wndPlaceholder =3D GetDlgItem ( IDC_IE_PLACEHOLDER );
CRect rc;
CAxWindow wndIE;
=20
<SPAN class=3Dcpp-comment>// Get the rect of the placeholder group =
box, then destroy </SPAN>
<SPAN class=3Dcpp-comment>// that window because we don't need it =
anymore.</SPAN>
wndPlaceholder.GetWindowRect ( rc );
ScreenToClient ( rc );
wndPlaceholder.DestroyWindow();
=20
<SPAN class=3Dcpp-comment>// Create the AX host window.</SPAN>
wndIE.Create ( *<SPAN class=3Dcpp-keyword>this</SPAN>, rc, _T(<SPAN =
class=3Dcpp-string>""</SPAN>),=20
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN );</PRE>
<P>Next, we use a <CODE>CAxWindow</CODE> method to create the =
ActiveX=20
control. The two methods we can choose from are=20
<CODE>CreateControl()</CODE> and <CODE>CreateControlEx()</CODE>.=20
<CODE>CreateControlEx()</CODE> has additional parameters that can =
return=20
interface pointers, so you don't have to subsequently call=20
<CODE>QueryControl()</CODE>. The two parameters we're interested =
in are=20
the first, which is the string version of the web browser =
control's GUID;=20
and the fourth, which is a pointer to an <CODE>IUnknown*</CODE>. =
This=20
pointer will be filled in with the <CODE>IUnknown</CODE> of the =
ActiveX=20
control. After creating the control, we query for an=20
<CODE>IWebBrowser2</CODE> interface, just as before, and navigate =
the=20
control to a URL.</P><PRE>CComPtr<IUnknown> punkCtrl;
CComQIPtr<IWebBrowser2> pWB2;
CComVariant v;
=20
<SPAN class=3Dcpp-comment>// Create the browser control using its =
GUID.</SPAN>
wndIE.CreateControlEx ( L<SPAN =
class=3Dcpp-string>"{8856F961-340A-11D0-A96B-00C04FD705A2}"</SPAN>,=20
NULL, NULL, &punkCtrl );
=20
<SPAN class=3Dcpp-comment>// Get an IWebBrowser2 interface on the =
control and navigate to a page.</SPAN>
pWB2 =3D punkCtrl;
pWB2->Navigate ( CComBSTR(<SPAN =
class=3Dcpp-string>"about:mozilla"</SPAN>), &v, &v, &v, =
&v );
}</PRE>
<P>For ActiveX controls that have ProgIDs, you can pass the ProgID =
to=20
<CODE>CreateControlEx()</CODE> instead of the GUID. For example, =
we could=20
create the browser control with this call:</P><PRE> <SPAN =
class=3Dcpp-comment>// Use the control's ProgID: Shell.Explorer:</SPAN>
wndIE.CreateControlEx ( L<SPAN =
class=3Dcpp-string>"Shell.Explorer"</SPAN>, NULL,
NULL, &punkCtrl );</PRE>
<P><CODE>CreateControl()</CODE> and <CODE>CreateControlEx()</CODE> =
also=20
have overloads that are used specifically with web browser =
controls. If=20
your app contains a web page as an HTML resource, you can pass its =
resource ID as the first parameter. ATL will create a web browser =
control=20
and navigate it to the resource. IEHoster contains a page whose ID =
is=20
IDR_ABOUTPAGE, so we can show it in the about box with this =
code:</P><PRE> wndIE.CreateControl ( IDR_ABOUTPAGE );</PRE>
<P>Here's the result:</P>
<P><IMG height=3D254 alt=3D" [About box browser ctrl - 6K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC6/aboutbox.png" =
width=3D287=20
align=3Dbottom border=3D0></P>
<P>The sample project contains code for all three of the =
techniques=20
described above. Check out <CODE>CAboutDlg::OnInitDialog()</CODE> =
and=20
comment/uncomment code to see each one in action.</P>
<H2><A name=3Dkeyboard></A>Keyboard Handling</H2>
<P>One final, but very important, detail is keyboard messages. =
Keyboard=20
handling with ActiveX controls is rather complicated, since the =
host and=20
the control have to work together to make sure the control sees =
the=20
messages it's interested in. For example, the browser control lets =
you=20
move through links with the TAB key. MFC handles all this itself, =
so you=20
may never have realized the amount of work required to get =
keyboard=20
support perfectly right.</P>
<P>Unfortunately, the AppWizard doesn't generate keyboard handling =
code=20
for a dialog-based app. However, if you make an SDI app that uses =
a form=20
view as the view window, you'll see the necessary code in=20
<CODE>PreTranslateMessage()</CODE>. When a mouse or keyboard =
message is=20
read from the message queue, the code gets the control with the =
focus and=20
forwards the message to the control using the ATL message=20
<CODE>WM_FORWARDMSG</CODE>. This normally does nothing, but when =
an=20
ActiveX control has the focus, the <CODE>WM_FORWARDMSG</CODE> ends =
up=20
being sent to the <CODE>AtlAxWin</CODE> that hosts the control.=20
<CODE>AtlAxWin</CODE> recognizes <CODE>WM_FORWARDMSG</CODE> and =
takes the=20
necessary steps to see if the control wants to handle the message=20
itself.</P>
<P>If the window with the focus does not recognize=20
<CODE>WM_FORWARDMSG</CODE>, then =
<CODE>PreTranslateMessage()</CODE> calls=20
<CODE>IsDialogMessage()</CODE> so that the standard dialog =
navigation keys=20
like TAB work properly.</P>
<P>The sample project contains the necessary code in=20
<CODE>PreTranslateMessage()</CODE>. Since=20
<CODE>PreTranslateMessage()</CODE> works only in modeless dialogs, =
your=20
dialog-based app <I>must</I> use a modeless dialog if you want =
proper=20
keyboard handling.</P>
<H2><A name=3Dupnext></A>Up Next</H2>
<P>In the next article, we'll return to frame windows and cover =
the topic=20
of using splitter windows.</P>
<H2><A name=3Drevisionhistory></A>Revision History</H2>
<P>May 20, 2003: Article first published. <!-- Article Ends =
--></P></DIV>
<H2>About Michael Dunn</H2>
<TABLE width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/script/profile/images/{2D6F4A37-6FD0-4C=
EF-AC72-EA9D126E611E}.jpg"><BR><BR><IMG=20
=
src=3D"http://www.codeproject.com/script/images/sitebuild_icon.gif">=20
Site Builder</TD>
<TD class=3DsmallText width=3D"100%">Michael lives in sunny =
Los Angeles,=20
California, and is so spoiled by the weather that he will =
probably=20
never be able to live anywhere else. He started programming =
with an=20
Apple //e in 4th grade, graduated from <A=20
href=3D"http://www.ucla.edu/">UCLA</A> with a math degree in =
1995, and=20
immediately landed a job as a QA engineer at Symantec, =
working on=20
the Norton AntiVirus team. He pretty much taught himself =
Windows and=20
MFC programming, and in 1999 he designed and coded a new =
interface=20
for Norton AntiVirus 2000.<BR><BR>Mike now works as a =
developer at=20
<A href=3D"http://www.napster.com/">Napster</A>, an online=20
subscription music service. He also developed <A=20
href=3D"http://www.ultrabar.com/">UltraBar</A>, an IE =
toolbar plugin=20
that makes web searching easy and trounces the Google =
toolbar; the=20
<A href=3D"http://tinyurl.com/i6sk">CodeProject =
SearchBar</A>; and has=20
co-founded <A =
href=3D"http://www.zabersoft.com/">Zabersoft</A>, a=20
development company with offices in Los Angeles and Odense,=20
Denmark.<BR><BR>He also enjoys his hobbies of playing =
pinball, bike=20
riding, and the occasional PlayStation, Dreamcast, or MAME =
game. He=20
is also sad that he's forgotten the languages he's studied: =
French,=20
Mandarin Chinese, and Japanese.
<P class=3DsmallText>Click <A=20
=
href=3D"http://www.codeproject.com/script/profile/whos_who.asp?vt=3Darts&=
amp;id=3D152">here</A>=20
to view Michael Dunn's online =
profile.</P></TD></TR></TBODY></TABLE><BR>
<H2>Other popular WTL articles:</H2>
<UL>
<LI><A href=3D"http://www.codeproject.com/wtl/wtl4mfc2.asp">WTL =
for MFC=20
Programmers, Part II - WTL GUI Base Classes</A><BR><SPAN=20
class=3Dsmalltext>WTL programming for MFC developers - frame=20
windows</SPAN>
<LI><A =
href=3D"http://www.codeproject.com/wtl/wtldockingwindows.asp">WTL=20
Docking windows</A><BR><SPAN class=3Dsmalltext>This is an =
implementation=20
of docking windows for the WTL library</SPAN>
<LI><A href=3D"http://www.codeproject.com/wtl/wtl4mfc5.asp">WTL =
for MFC=20
Programmers, Part V - Advanced Dialog UI Classes</A><BR><SPAN=20
class=3Dsmalltext>Using the new WTL classes that implement =
advanced dialog=20
UI elements</SPAN>
<LI><A href=3D"http://www.codeproject.com/wtl/wtl4mfc1.asp">WTL =
for MFC=20
Programmers, Part I - ATL GUI Classes</A><BR><SPAN =
class=3Dsmalltext>An=20
introduction to WTL programming for MFC =
developers</SPAN></LI></UL>
<FORM action=3D/script/rating/code/app/insert_vote.asp =
method=3Dpost><INPUT=20
type=3Dhidden value=3DWTL4MFC6/wtl5/20/2003 name=3Dvote_name> =
<INPUT type=3Dhidden=20
value=3D/wtl/wtl4mfc6.asp name=3Dgoal>=20
<TABLE cellSpacing=3D0 cellPadding=3D1 width=3D"100%" =
bgColor=3D#ff9900=20
border=3D0><TBODY>
<TR>
<TD width=3D"100%">
<TABLE cellSpacing=3D0 cellPadding=3D4 width=3D"100%" =
bgColor=3D#fbedbb=20
border=3D0>
<TBODY>
<TR>
<TD class=3Dsmalltext vAlign=3Dcenter>[<A=20
=
href=3D"http://www.codeproject.com/wtl/wtl4mfc6.asp#__top">Top</A>]</TD>
<TD vAlign=3Dcenter noWrap align=3Dright><I><B>Rate this =
Article=20
for us!</B></I> =
<I>Poor</I><INPUT=20
type=3Dradio value=3D1 name=3Drate><INPUT type=3Dradio =
value=3D2=20
name=3Drate><INPUT type=3Dradio value=3D3 =
name=3Drate><INPUT=20
type=3Dradio value=3D4 name=3Drate><INPUT type=3Dradio =
value=3D5=20
name=3Drate><I>Excellent</I> <INPUT =
class=3DFormButton type=3Dsubmit value=3DVote>=20
=
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD vAlign=3Dtop>
<CENTER><A=20
=
href=3D"http://www.codeproject.com/script/admentor/admentorredir.asp?id=3D=
156&way=3Dban"=20
target=3D_blank><IMG height=3D60=20
alt=3D"MSDN Magazine - Your guide to Microsoft tools, =
development environments, and technologies for Windows and the Web."=20
=
src=3D"http://www.codeproject.com/script/admentor/images/msdn_button.gif"=
=20
width=3D120 border=3D0></A><BR><FONT size=3D1><A=20
href=3D"http://www.codeproject.com/info/MediaKit/">Premium=20
Sponsor</A></FONT></CENTER><BR></TD>
<TD vAlign=3Dtop noWrap align=3Dmiddle><SPAN id=3DAdBanner5><A =
=
href=3D"http://www.codeproject.com/script/admentor/admentorredir.asp?id=3D=
226&way=3Dban"=20
target=3D_blank><IMG height=3D60=20
alt=3D"Business Components Gallery Pro - Windows GUI =
solutions."=20
=
s
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -