📄 chxd.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Appendix D</TITLE>
<LINK REL="Next" HREF="chxe.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxe.htm">
<LINK REL="Previous" HREF="chxc.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxc.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Appendix D</B></H2>
<H2><B>MFC Macros and Globals</B></H2>
<hr>
<P>When you're writing programs, there are many types of data and operations that you must use again and again. Sometimes, you have to do something as simple as creating a portable integer data type. Other times, you need to do something a little more
complex, like extracting a word from a long word value or storing the position of the mouse pointer. As you may know, when you compile your program with Visual C++ there are many constants and variables already defined. You can use these in your programs
to save time writing code and to make your programs more portable and more readable by other programmers. In the following tables, you'll get a look at the most important of these globally available constants, macros, and variables.</P>
<H3><B>Ten Categories of Macros and Global Variables</B></H3>
<P>Because there are so many constants, macros, and global variables, it is helpful to divide them into ten categories, listed below. The following sections describe each of these categories and the symbols they define:</P>
<ul>
<li> Application information and management</P>
<li> ClassWizard comment delimiters</P>
<li> Collection class helpers</P>
<li> CString formatting and message-box display</P>
<li> Data types</P>
<li> Diagnostic services</P>
<li> Exception processing</P>
<li> Message maps</P>
<li> Run-time object model services</P>
<li> Standard command and window IDs</P>
</ul>
<H3><B>Application Information and Management Functions</B></H3>
<P>Because a typical Visual C++ application contains only one application object but many other objects created from other MFC classes, you frequently need to get information about the application in different places in a program. Visual C++ defines a set
of global functions that return this information to any class in a program. These functions, which are listed in Table D.1, can be called from anywhere within an MFC program. For example, you frequently need to get a pointer to an application's main
window. The following function call accomplishes that task:</P>
<pre><font color="#008000">CWnd* pWnd = AfxGetMainWnd();</font></pre>
<P><B>Table D.1—Application Information and Management</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<P>AfxBeginThread()</P>
<TD>
<P>Creates a new thread. (See <A HREF="index27.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index27.htm" target="text">Chapter 27</A>, "Multitasking with Windows Threads.")</P>
<TR>
<TD>
<P>AfxEndThread()</P>
<TD>
<P>Terminates a thread.</P>
<TR>
<TD>
<P>AfxGetApp()</P>
<TD>
<P>Gets the application's <font color="#008000">CWinApp</font> pointer.</P>
<TR>
<TD>
<P>AfxGetAppName()</P>
<TD>
<P>Gets the application's name.</P>
<TR>
<TD>
<P>AfxGetInstanceHandle()</P>
<TD>
<P>Gets the application's instance handle.</P>
<TR>
<TD>
<P>AfxGetMainWnd()</P>
<TD>
<P>Gets a pointer to the application's main window.</P>
<TR>
<TD>
<P>AfxGetResourceHandle()</P>
<TD>
<P>Get the application's resource handle.</P>
<TR>
<TD>
<P>AfxGetThread()</P>
<TD>
<P>Gets a pointer to a <font color="#008000">CWinThread</font> object.</P>
<TR>
<TD>
<P>AfxRegisterClass()</P>
<TD>
<P>Registers a window class in an MFC DLL.</P>
<TR>
<TD>
<P>AfxRegisterWndClass()</P>
<TD>
<P>Registers a Windows window class in an MFC application.</P>
<TR>
<TD>
<P>AfxSetResourceHandle()</P>
<TD>
<P>Sets the instance handle that determines where to load the application's default resources.</P>
<TR>
<TD>
<P>AfxSocketInit()</P>
<TD>
<P>Initializes Windows Sockets. (See <A HREF="index18.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index18.htm" target="text">Chapter 18</A>, "Sockets, MAPI, and the Internet.")</P></TABLE>
<H3><B>ClassWizard Comment Delimiters</B></H3>
<P>Visual C++ defines a number of delimiters that ClassWizard uses to keep track of what it's doing, as well as to locate specific areas of source code. Although you'll rarely, if ever, use these macros yourself, you will see them embedded in your
AppWizard applications, so you might like to know exactly what they do. Table D.2 fills you in.</P>
<P><B>Table D.2—ClassWizard Delimiters</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Delimiter</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<P>AFX_DATA</P>
<TD>
<P>Starts and ends member variable declarations in header files that are associated with dialog data exchange.</P>
<TR>
<TD>
<P>AFX_DATA_INIT</P>
<TD>
<P>Starts and ends dialog data exchange variable initialization in a dialog class's constructor.</P>
<TR>
<TD>
<P>AFX_DATA_MAP</P>
<TD>
<P>Starts and ends dialog data exchange function calls in a dialog class's <font color="#008000">DoDataExchange()</font> function.</P>
<TR>
<TD>
<P>AFX_DISP</P>
<TD>
<P>Starts and ends Automation declarations in header files.</P>
<TR>
<TD>
<P>AFX_DISP_MAP</P>
<TD>
<P>Starts and ends Automation mapping in implementation files.</P>
<TR>
<TD>
<P>AFX_EVENT</P>
<TD>
<P>Starts and ends ActiveX event declarations in header files.</P>
<TR>
<TD>
<P>AFX_EVENT_MAP</P>
<TD>
<P>Starts and ends ActiveX events in implementation files.</P>
<TR>
<TD>
<P>AFX_FIELD</P>
<TD>
<P>Starts and ends member variable declarations in header files that are associated with database record field exchange.</P>
<TR>
<TD>
<P>AFX_FIELD_INIT</P>
<TD>
<P>Starts and ends record field exchange member variable initialization in a record set class's constructor.</P>
<TR>
<TD>
<P>AFX_FIELD_MAP</P>
<TD>
<P>Starts and ends record field exchange function calls in a record set class's <font color="#008000">DoFieldExchange()</font> function.</P>
<TR>
<TD>
<P>AFX_MSG</P>
<TD>
<P>Starts and ends ClassWizard entries in header files for classes that use message maps.</P>
<TR>
<TD>
<P>AFX_MSG_MAP</P>
<TD>
<P>Starts and ends message map entries.</P>
<TR>
<TD>
<P>AFX_VIRTUAL</P>
<TD>
<P>Starts and ends virtual function overrides in header files.</P></TABLE>
<H3><B>Collection Class Helpers</B></H3>
<P>Because certain types of data structures are so commonly used in programming, MFC defines collection classes that enable you to get these common data structures initialized quickly and manipulated easily. MFC includes collection classes for arrays,
linked lists, and mapping tables. Each of these types of collections contain elements that represent the individual pieces of data that comprise the collection. To make it easier to access these elements, MFC defines a set of functions created from
templates (see <A HREF="index26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index26.htm" target="text">Chapter 26</A>, "Exceptions, Templates, and the Latest Additions to C++.")The functions are shown in Table D.3, and you provide the implementation for each particular data type.</P>
<P>For example, if you want to keep a sorted list, the functions that insert new items into the list must be able to compare two Truck objects or two Employee objects to decide where to put a new Truck or Employee. You implement <font
color="#008000">CompareElements()</font> for the Truck class or Employee class, and then the collection class code can use this function to decide where to put new additions to the collection.</P>
<P><B>Table D.3—Collection Class Helper Functions</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<P>CompareElements()</P>
<TD>
<P>Checks elements for equality.</P>
<TR>
<TD>
<P>ConstructElements()</P>
<TD>
<P>Constructs new elements (works similar to a class constructor).</P>
<TR>
<TD>
<P>DestructElements()</P>
<TD>
<P>Destroys elements (works similar to a class destructor).</P>
<TR>
<TD>
<P>DumpElements()</P>
<TD>
<P>Provides diagnostic output in text form.</P>
<TR>
<TD>
<P>HashKey()</P>
<TD>
<P>Calculates hashing keys.</P>
<TR>
<TD>
<P>SerializeElements()</P>
<TD>
<P>Saves or loads elements to or from an archive.</P></TABLE>
<H3><B><I>CString</I></B><B> Formatting and Message-Box Display</B></H3>
<P>If you've done much Visual C++ programming, you know that MFC features a special string class, called <font color="#008000">CString</font>, that makes string handling under C++ less cumbersome. <font color="#008000">CString</font> objects are used
extensively throughout MFC programs, and are discussed in <A HREF="indexe.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexe.htm" target="text">Appendix E</A>, “Useful Classes.” There are times when CString is not the right class though, such as when dealing with strings in a resource's string
table. These global functions, which replace format characters in string tables, provide the CString Format() capability for resource strings. There's also a global function for displaying a message box.</P>
<P><B>Table D.4—</B><B><I>CString</I></B><B> Formatting and Message-Box Functions</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<P>AfxFormatString1()</P>
<TD>
<P>Replaces the format characters (such as %1) in a string resource with a given string.</P>
<TR>
<TD>
<P>AfxFormatString2()</P>
<TD>
<P>Replaces the format characters "%1" and "%2" in a string resource with the given strings.</P>
<TR>
<TD>
<P>AfxMessageBox()</P>
<TD>
<P>Displays a message box.</P></TABLE>
<H3><B>Data Types</B></H3>
<P>The most commonly used constants are those that define a portable set of data types. You've seen tons of these constants, which are named using all uppercase letters, used in Windows programs. You'll recognize many of these from the Windows SDK. Others
are included only as part of Visual C++. You use these constants exactly as you would any other data type. For example, to declare a Boolean variable, you'd write something like this:</P>
<pre><font color="#008000">BOOL flag;</font></pre>
<P>Table D.5 lists the most commonly used data types defined by Visual C++ for Windows 95 and NT. Searching in the help index on any one of these types will lead you to a page in the online help that lists all the data types used in MFC and the Windows
SDK.</P>
<P><B>Table D.5—Commonly Used Data Types</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Constant</I></P>
<TD>
<P><I>Data type</I></P>
<TR>
<TD>
<P>BOOL</P>
<TD>
<P>Boolean value.</P>
<TR>
<TD>
<P>BSTR </P>
<TD>
<P>32-bit pointer to character data.</P>
<TR>
<TD>
<P>BYTE</P>
<TD>
<P>8-bit unsigned integer.</P>
<TR>
<TD>
<P>COLORREF</P>
<TD>
<P>32-bit color value.</P>
<TR>
<TD>
<P>DWORD</P>
<TD>
<P>32-bit unsigned integer.</P>
<TR>
<TD>
<P>LONG</P>
<TD>
<P>32-bit signed integer.</P>
<TR>
<TD>
<P>LPARAM</P>
<TD>
<P>32-bit window-procedure parameter.</P>
<TR>
<TD>
<P>LPCRECT</P>
<TD>
<P>32-bit constant RECT structure pointer.</P>
<TR>
<TD>
<P>LPCSTR</P>
<TD>
<P>32-bit string-constant pointer.</P>
<TR>
<TD>
<P>LPSTR</P>
<TD>
<P>32-bit string pointer.</P>
<TR>
<TD>
<P>LPVOID</P>
<TD>
<P>32-bit void pointer.</P>
<TR>
<TD>
<P>LRESULT</P>
<TD>
<P>32-bit window-procedure return value.</P>
<TR>
<TD>
<P>POSITION</P>
<TD>
<P>The position of an element in a collection.</P>
<TR>
<TD>
<P>UINT</P>
<TD>
<P>32-bit unsigned integer.</P>
<TR>
<TD>
<P>WNDPROC</P>
<TD>
<P>32-bit window-procedure pointer.</P>
<TR>
<TD>
<P>WORD</P>
<TD>
<P>16-bit unsigned integer.</P>
<TR>
<TD>
<P>WPARAM</P>
<TD>
<P>32-bit window-procedure parameter.</P></TABLE>
<H3><B>Using Diagnostic Services</B></H3>
<P>Once you have your program written, you're far from done. Then comes the grueling task of testing, which means rolling up your sleeves, cranking up your debugger, and weeding out all the gotchas hiding in your code. Luckily, Visual C++ provides many
macros, functions, and global variables that you can use to incorporate diagnostic abilities into your projects. Using these tools, you can print output to a debugging window, check the integrity of memory blocks, and much more. Table E.6 lists these
valuable diagnostic macros, functions, and global variables.</P>
<P><B>Table D.6—Diagnostic Macros, Functions, and Global Variables</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Symbol</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<P>AfxCheckMemory()</P>
<TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -