📄 apb.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Appendix B -- Windows Programming Review and a Look Inside CWnd</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../apa/apa.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apc/apc.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- B -</H1>
<H1>Windows Programming Review and a Look Inside CWnd</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Programming for Windows</A>
<UL>
<LI><A HREF="#Heading2">A C-Style Window Class</A>
<LI><A HREF="#Heading3">Window Creation</A>
</UL>
<LI><A HREF="#Heading4">Encapsulating the Windows API</A>
<LI><A HREF="#Heading5">Inside CWnd</A>
<LI><A HREF="#Heading6">Getting a Handle on All These MFC Classes</A>
<UL>
<LI><A HREF="#Heading7">CObject</A>
<LI><A HREF="#Heading8">CCmdTarget</A>
<LI><A HREF="#Heading9">CWnd</A>
<LI><A HREF="#Heading10">All Those Other Classes</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>The Microsoft Foundation Classes were written for one single purpose: to make
Windows programming easier by providing classes with methods and data that handle
tasks common to all Windows programs. The classes that are in MFC are designed to
be useful to a Windows programmer specifically. The methods within each class perform
tasks that Windows programmers often need to perform. Many of the classes have a
close correspondence to structures and <I>window</I> <I>classes</I>, in the old Windows
sense of the word <I>class</I>. Many of the methods correspond closely to API (Application
Programming Interface) functions already familiar to Windows programmers, who often
refer to them as the Windows SDK or as SDK functions.</P>
<P>
<H2><A NAME="Heading1"></A>Programming for Windows</H2>
<P>If you've programmed for Windows in C, you know that the word <I>class</I> was
used to describe a window long before C++ programming came to Windows. A window class
is vital to any Windows C program. A standard structure holds the data that describes
this window class, and the operating system provides a number of standard window
classes. A programmer usually builds a new window class for each program and registers
it by calling an API function, RegisterClass(). Windows that appear onscreen can
then be created, based on that class, by calling another API function, CreateWindow().</P>
<P>
<H3><A NAME="Heading2"></A>A C-Style Window Class</H3>
<P>The WNDCLASS structure, which describes the window class, is equivalent to the
WNDCLASSA structure, which looks like Listing B.1.</P>
<P>
<H4>Listing B.1  WNDCLASSA Structure from WINUSER.H</H4>
<PRE>typedef struct tagWNDCLASSA {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCSTR lpszMenuName;
LPCSTR lpszClassName;
</PRE>
<PRE>} WNDCLASSA, *PWNDCLASSA, NEAR *NPWNDCLASSA, FAR *LPWNDCLASSA;
</PRE>
<P>WINUSER.H sets up two very similar window class structures: WNDCLASSA for programs
that use normal strings and WNDCLASSW for Unicode programs. Chapter 28, "Future
Explorations," covers Unicode programs in the "Unicode" section.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> WINUSER.H is code supplied with Developer Studio. It's typically in
the folder \Program Files\ Files\Microsoft Visual Studio\VC98\include.
<HR>
</BLOCKQUOTE>
<P>If you were creating a Windows program in C, you would need to fill a WNDCLASS
structure. The members of the WNDCLASS structure are as follows:</P>
<P>
<UL>
<LI>style--A number made by combining standard styles, represented with constants
like CS_GLOBALCLASS or CS_OWNDC, with the bitwise or operator (|). A perfectly good
class can be registered with a style value of 0; the other styles are for exceptions
to normal procedure.
<P>
<LI>lpfnWndProc--A pointer to a function that is the Windows Procedure (generally
called the WindProc) for the class. Refer to Chapter 3, "Messages and Commands,"
for a discussion of this function.
<P>
<LI>cbClsExtra--The number of extra bytes to add to the window class. It's usually
0, but C programmers would sometimes build a window class with extra data in it.
<P>
<LI>cbWndExtra--The number of extra bytes to add to each instance of the window,
usually 0.
<P>
<LI>hInstance--A handle to an instance of an application, the running program that
is registering this window class. For now, think of this as a way for the window
class to reach the application that uses it.
<P>
<LI>hIcon--An icon to be drawn when the window is minimized. Typically, this is set
with a call to another API function, LoadIcon().
<P>
<LI>hCursor--The cursor that displays when the mouse is over the screen window associated
with this window class. Typically, this is set with a call to the API function LoadCursor().
<P>
<LI>hbrBackground--The brush to be used for painting the window background. The API
call to GetStockObject() is the usual way to set this variable.
<P>
<LI>lpszMenuName--A long pointer to a string that is zero terminated and contains
the name of the menu for the window class.
<P>
<LI>lpszClassName--The name for this window class, to be used by CreateWindow(),
when a window (an instance of the window class) is created. You make up a name.
</UL>
<H3><A NAME="Heading3"></A>Window Creation</H3>
<P>If you've never written a Windows program before, having to fill out a WNDCLASS
structure might intimidate you. This is the first step, though, in Windows programming
in C. However, you can always find simple sample programs to copy, like this one:</P>
<P>
<PRE>WNDCLASS wcInit;
wcInit.style = 0;
wcInit.lpfnWndProc = (WNDPROC)MainWndProc;
wcInit.cbClsExtra = 0;
wcInit.cbWndExtra = 0;
wcInit.hInstance = hInstance;
wcInit.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(ID_ICON));
wcInit.hCursor = LoadCursor (NULL, IDC_ARROW);
wcInit.hbrBackground = GetStockObject (WHITE_BRUSH);
wcInit.lpszMenuName = "DEMO";
wcInit.lpszClassName ="NewWClass";
return (RegisterClass (&wcInit));
</PRE>
<BLOCKQUOTE>
<P>
<HR>
<B>Hungarian Notation</B><BR>
</P>
<P>You might wonder what kind of variable name lpszClassName is or why it's wcInit
and not just Init. The reason for this is Microsoft programmers use a variable naming
convention called <I>Hungarian Notation</I>. It is so named because a Hungarian programmer
named Charles Simonyi popularized it at Microsoft (and probably because at first
glance, the variable names seem to be written in another language).<BR>
</P>
<P>In Hungarian Notation, the variable is given a descriptive name, like Count or
ClassName, that starts with a capital letter. If it is a multiword name, each word
is capitalized. Then, before the descriptive name, letters are added to indicate
the variable type--for example, nCount for an integer or bFlag for a Boolean (TRUE
or FALSE) variable. In this way, the programmer should never forget a variable type
or do something foolish such as pass a signed variable to a function that is expecting
an unsigned value.<BR>
</P>
<P>The style has gained widespread popularity, although some people hate it. If you
long for the good old days of arguing where to put the braces, or better still whether
to call them brace, face, or squiggle brackets, but can't find anyone to rehash those
old wars anymore, you can probably find somebody to argue about Hungarian Notation
instead. The arguments in favor boil down to "you catch yourself making stupid
mistakes," and the arguments against it to "it's ugly and hard to read."
The practical truth is that the structures used by the API and the classes defined
in MFC all use Hungarian Notation, so you might as well get used to it. You'll probably
find yourself doing it for your own variables, too. The prefixes are as follows:
<BR>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<B>Prefix </B> </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<B>Variable Type </B> </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<B>Comment </B> </BLOCKQUOTE>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
a </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Array </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
b </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Boolean </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
d </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Double </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
h </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Handle </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
i </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Integer </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
"index into" </BLOCKQUOTE>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
l </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Long </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
lp </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Long pointer to </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
lpfn </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Long pointer to function </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
m_ </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Member variable </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT"> <P>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
n </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
Integer </BLOCKQUOTE>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
"number of" </BLOCKQUOTE>
</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
p </BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -