?? wtl for mfc programmers, part i.mht
字號:
inheritance=20
list, each with a <CODE>CHAIN_MSG_MAP</CODE> macro so that =
messages are=20
passed to it. This is different from MFC, where each=20
<CODE>CWnd</CODE>-derived class can have only one base class, and =
MFC=20
automatically passes unhandled messages to the base class.</P>
<H2><A name=3Dexestructure></A>Structure of an ATL EXE</H2>
<P>So now that we have a complete (if not entirely useful) main =
window,=20
let's see how to use it in a program. An ATL executable contains a =
global=20
<CODE>CComModule</CODE> variable called <CODE>_Module</CODE>. This =
is=20
analogous to the global <CODE>CWinApp</CODE> variable called=20
<CODE>theApp</CODE> in an MFC program; the only difference is that =
in ATL,=20
the variable <I>must</I> be called <CODE>_Module</CODE>.</P>
<P>Here's how we start our stdafx.h:</P><PRE><SPAN =
class=3Dcpp-comment>// stdafx.h:</SPAN>
<SPAN class=3Dcpp-preprocessor>#define STRICT</SPAN>
<SPAN class=3Dcpp-preprocessor>#define VC_EXTRALEAN</SPAN>
=20
<SPAN class=3Dcpp-preprocessor>#include <atlbase.h> // Base =
ATL classes</SPAN>
<SPAN class=3Dcpp-keyword>extern</SPAN> CComModule _Module; <SPAN =
class=3Dcpp-comment>// Global _Module</SPAN>
<SPAN class=3Dcpp-preprocessor>#include <atlwin.h> // ATL =
windowing classes</SPAN></PRE>
<P>atlbase.h will include the basic Windows headers, so there's no =
need to=20
include windows.h, tchar.h, etc. In our CPP file, we declare the=20
<CODE>_Module</CODE> variable:</P><PRE><SPAN =
class=3Dcpp-comment>// main.cpp:</SPAN>
CComModule _Module;</PRE>
<P><CODE>CComModule</CODE> had explicit initialization and =
shutdown=20
functions that we need to call in <CODE>WinMain()</CODE>, so let's =
start=20
with those:</P><PRE><SPAN class=3Dcpp-comment>// main.cpp:</SPAN>
CComModule _Module;
=20
<SPAN class=3Dcpp-keyword>int</SPAN> WINAPI WinMain(HINSTANCE hInst, =
HINSTANCE hInstPrev,
nCmdShow)
{
_Module.Init(NULL, hInst);
_Module.Term();
}</PRE>
<P>The first parameter to <CODE>Init()</CODE> is only used in COM =
servers.=20
Since our EXE contains no COM objects, we can just pass =
<CODE>NULL</CODE>.=20
ATL doesn't provide its own <CODE>WinMain()</CODE> or message pump =
like=20
MFC, so to get our program running, we create a =
<CODE>CMyWindow</CODE>=20
object and add a message pump.</P><PRE><SPAN =
class=3Dcpp-comment>// main.cpp:</SPAN>
<SPAN class=3Dcpp-preprocessor>#include "MyWindow.h"</SPAN>
CComModule _Module;
=20
<SPAN class=3Dcpp-keyword>int</SPAN> WINAPI WinMain(HINSTANCE hInst, =
HINSTANCE hInstPrev,
LPSTR szCmdLine, <SPAN class=3Dcpp-keyword>int</SPAN> =
nCmdShow)
{
_Module.Init(NULL, hInst);
=20
CMyWindow wndMain;
MSG msg;
=20
<SPAN class=3Dcpp-comment>// Create & show our main =
window</SPAN>
<SPAN class=3Dcpp-keyword>if</SPAN> ( NULL =3D=3D wndMain.Create ( =
NULL, CWindow::rcDefault,=20
_T(<SPAN class=3Dcpp-string>"My First =
ATL Window"</SPAN>) ))
{
<SPAN class=3Dcpp-comment>// Bad news, window creation =
failed</SPAN>
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>1</SPAN>;
}
=20
wndMain.ShowWindow(nCmdShow);
wndMain.UpdateWindow();
=20
<SPAN class=3Dcpp-comment>// Run the message loop</SPAN>
<SPAN class=3Dcpp-keyword>while</SPAN> ( GetMessage(&msg, NULL, =
<SPAN class=3Dcpp-literal>0</SPAN>, <SPAN class=3Dcpp-literal>0</SPAN>) =
> <SPAN class=3Dcpp-literal>0</SPAN> )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
=20
_Module.Term();
<SPAN class=3Dcpp-keyword>return</SPAN> msg.wParam;
}</PRE>
<P>The only unusual thing in the above code is=20
<CODE>CWindow::rcDefault</CODE>, which is a <CODE>RECT</CODE> =
member of=20
<CODE>CWindow</CODE>. Using that as the window's initial =
<CODE>RECT</CODE>=20
is like using <CODE>CW_USEDEFAULT</CODE> for the width and height =
with the=20
<CODE>CreateWindow()</CODE> API.</P>
<P>Under the hood, ATL uses some assembly-language black magic to =
connect=20
the main window's handle to its corresponding =
<CODE>CMyWindow</CODE>=20
object. The upside of this is that there is no problem passing=20
<CODE>CWindow</CODE> objects between threads, something that fails =
miserably with <CODE>CWnd</CODE>s in MFC.</P>
<P>Here's what our window looks like:</P>
<P><IMG height=3D249 alt=3D" [First ATL window - 4K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC1/firstwin.png" =
width=3D370=20
align=3Dbottom border=3D0></P>
<P>Nothing particularly exciting, I'll admit. To spice it up, =
we'll add an=20
<I>About</I> menu item that shows a dialog.</P>
<H2><A name=3Ddialogs></A>Dialogs in ATL</H2>
<P>As mentioned earlier, ATL has two dialog classes. We'll use=20
<CODE>CDialogImpl</CODE> for our about dialog. Making a new dialog =
class=20
is almost like making a new frame window class; there are just two =
differences:</P>
<OL>
<LI>The base class is <CODE>CDialogImpl</CODE> instead of=20
<CODE>CWindowImpl</CODE>.=20
<LI>You need to define a public member called <CODE>IDD</CODE> =
that=20
holds the resource ID of the dialog. </LI></OL>
<P>Here is the start of a new class definition for an about =
dialog:</P><PRE><SPAN class=3Dcpp-keyword>class</SPAN> CAboutDlg : <SPAN =
class=3Dcpp-keyword>public</SPAN> CDialogImpl<CAboutDlg>
{
<SPAN class=3Dcpp-keyword>public</SPAN>:
<SPAN class=3Dcpp-keyword>enum</SPAN> { IDD =3D IDD_ABOUT };
=20
BEGIN_MSG_MAP(CAboutDlg)
END_MSG_MAP()
};</PRE>
<P>ATL has no built-in handlers for the <I>OK</I> and =
<I>Cancel</I>=20
buttons, so we need to code them ourselves, along with a=20
<CODE>WM_CLOSE</CODE> handler that is called if the user clicks =
the close=20
button in the title bar. We also need to handle =
<CODE>WM_INITDIALOG</CODE>=20
so that the keyboard focus is set properly when the dialog =
appears. Here=20
is the complete class definition with message =
handlers.</P><PRE><SPAN class=3Dcpp-keyword>class</SPAN> CAboutDlg : =
<SPAN class=3Dcpp-keyword>public</SPAN> CDialogImpl<CAboutDlg>
{
<SPAN class=3Dcpp-keyword>public</SPAN>:
<SPAN class=3Dcpp-keyword>enum</SPAN> { IDD =3D IDD_ABOUT };
=20
BEGIN_MSG_MAP(CAboutDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_CLOSE, OnClose)
COMMAND_ID_HANDLER(IDOK, OnOKCancel)
COMMAND_ID_HANDLER(IDCANCEL, OnOKCancel)
END_MSG_MAP()
=20
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, =
BOOL& bHandled)
{
CenterWindow();
<SPAN class=3Dcpp-keyword>return</SPAN> TRUE; <SPAN =
class=3Dcpp-comment>// let the system set the focus</SPAN>
}
=20
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& =
bHandled)
{
EndDialog(IDCANCEL);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
=20
LRESULT OnOKCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, =
BOOL& bHandled)
{
EndDialog(wID);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
};</PRE>
<P>I used one handler for both <I>OK</I> and <I>Cancel</I> to =
demonstrate=20
the <CODE>wID</CODE> parameter, which is set to either =
<CODE>IDOK</CODE>=20
or <CODE>IDCANCEL</CODE>, depending on which button is =
clicked.</P>
<P>Showing the dialog is similar to MFC, you create an object of =
the new=20
class and call <CODE>DoModal()</CODE>. Let's go back to our main =
window=20
and add a menu with an <I>About</I> item that shows our new about =
dialog.=20
We'll need to add two message handlers, one for =
<CODE>WM_CREATE</CODE> and=20
one for the new menu item <CODE>IDC_ABOUT</CODE>.</P><PRE><SPAN =
class=3Dcpp-keyword>class</SPAN> CMyWindow : <SPAN =
class=3Dcpp-keyword>public</SPAN> CWindowImpl<CMyWindow, CWindow, =
CFrameWinTraits>,
<SPAN class=3Dcpp-keyword>public</SPAN> =
CPaintBkgnd<CMyWindow,RGB(<SPAN class=3Dcpp-literal>0</SPAN>,<SPAN =
class=3Dcpp-literal>0</SPAN>,<SPAN class=3Dcpp-literal>255</SPAN>)>
{
<SPAN class=3Dcpp-keyword>public</SPAN>:
BEGIN_MSG_MAP(CMyWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
COMMAND_ID_HANDLER(IDC_ABOUT, OnAbout)
<SPAN class=3Dcpp-comment>// ...</SPAN>
CHAIN_MSG_MAP(CPaintBkgndBase)
END_MSG_MAP()
=20
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& =
bHandled)
{
HMENU hmenu =3D LoadMenu ( _Module.GetResourceInstance(),
MAKEINTRESOURCE(IDR_MENU1) );
=20
SetMenu ( hmenu );
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
=20
LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& =
bHandled)
{
CAboutDlg dlg;
=20
dlg.DoModal();
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
<SPAN class=3Dcpp-comment>// ...</SPAN>
};</PRE>
<P>One small difference in modal dialogs is where you specify the =
dialog's=20
parent window. In MFC you pass the parent to the =
<CODE>CDialog</CODE>=20
constructor, but in ATL you pass it as the first parameter to=20
<CODE>DoModal()</CODE>. If you don't specify a window, as in the =
code=20
above, ATL uses the result of <CODE>GetActiveWindow()</CODE> =
(which will=20
be our frame window) as the parent.</P>
<P>The <CODE>LoadMenu()</CODE> call also demonstrates one of the=20
<CODE>CComModule</CODE> methods, =
<CODE>GetResourceInstance()</CODE>. This=20
returns the <CODE>HINSTANCE</CODE> of our EXE, similar to=20
<CODE>AfxGetResourceHandle()</CODE>. (There is also=20
<CODE>CComModule::GetModuleInstance()</CODE>, which functions like =
<CODE>AfxGetInstanceHandle()</CODE>.)</P>
<P>Here is our revised main window and the about dialog:</P>
<P><IMG height=3D248 alt=3D" [About box - 5K] "=20
src=3D"http://www.codeproject.com/wtl/WTL4MFC1/firstabout.png" =
width=3D369=20
align=3Dbottom border=3D0></P>
<H2><A name=3Dwhereswtlman></A>I'll Get to WTL, I Promise!</H2>
<P>But it will be in Part 2. Since I'm writing these articles for =
MFC=20
developers, I felt that it was best to do an intro to ATL first, =
before=20
diving into WTL. If this has been your first exposure to ATL, now =
might be=20
a good time to write some simple apps on your own, so you get the =
hang of=20
message maps and using mix-in classes. You can also experiment =
with=20
ClassView's support for ATL message maps, as it can add message =
handlers=20
for you. To get started, right-click on the <I>CMyWindow</I> item =
and pick=20
<I>Add Windows Message Handler</I> on the context menu.</P>
<P>In Part II, I will cover the base WTL windowing classes, the =
WTL=20
AppWizard, and the much nicer message map macros.</P>
<H2><A name=3Drevisionhistory></A>Revision History</H2>
<P>March 22, 2003: Article first published.=20
</P><!-- Article Ends --></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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -