⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chxa.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Appendix A</TITLE>
<LINK REL="Next" HREF="chxb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxb.htm">
<LINK REL="Previous" HREF="ch28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch28.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">

<H2><B>Appendix A </B></H2>
<H2><B>Windows Programming 
Review and a Look Inside CWnd</B></H2>
<hr>
<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 &quot;window 
classes&quot; in the old Windows sense of the word class. Many of the methods correspond closely to API (Application Programming Interface) functions that are already familiar to Windows programmers.</P>
<ul>
<li> <B>Windows programming review</B></P>
<P>  
A brief review of Windows programming in C for those who haven't done it recently.</P>
<li> <B>API functions: what they are, why you should care</B></P>
<P>  The useful functions provided to Windows C programmers are still used by C++ programmers, but they 
are wrapped up inside MFC classes that call the API functions for you.</P>
<li> <B>The MFC classes arranged by category</B></P>
<P>  An overview of the chapters that discuss various categories of MFC classes.</P>
</ul>
<H3><B>Programming for 
Windows</B></H3>
<P>If you've programmed for Windows in C, you know that the word <I>class</I> was used to describe the definition of 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 a number of standard window classes are provided by the operating system. A programmer usually builds a new window class for each program and registers it by calling an API function, <font 
color="#008000">RegisterClass().</font> Windows that appear on the screen can then be created, based on that class, by calling another API function, <font color="#008000">CreateWindow()</font>.</P>
<P><B>A C-style windows class</B></P>
<P>The <font 
color="#008000">WNDCLASS</font> structure, which describes the window class, is equivalent to the WNDCLASSA structure, which looks like this:</P>
<P><I>Listing A.1 WNDCLASSA structure from WINUSER.H</I></P>
<pre><font color="#008000">typedef struct 
tagWNDCLASSA {</font></pre>
<pre><font color="#008000">    UINT        style;</font></pre>
<pre><font color="#008000">    WNDPROC     lpfnWndProc;</font></pre>
<pre><font color="#008000">    int         cbClsExtra;</font></pre>
<pre><font color="#008000">    
int         cbWndExtra;</font></pre>
<pre><font color="#008000">    HINSTANCE   hInstance;</font></pre>
<pre><font color="#008000">    HICON       hIcon;</font></pre>
<pre><font color="#008000">    HCURSOR     hCursor;</font></pre>
<pre><font 
color="#008000">    HBRUSH      hbrBackground;</font></pre>
<pre><font color="#008000">    LPCSTR      lpszMenuName;</font></pre>
<pre><font color="#008000">    LPCSTR      lpszClassName;</font></pre>
<pre><font color="#008000">} WNDCLASSA, *PWNDCLASSA, 
NEAR *NPWNDCLASSA, FAR *LPWNDCLASSA;</font></pre>
<P>WINUSER.H sets up two very similar window class structures, WNDCLASSA for programs that use normal strings, and WNDCLASSW for Unicode programs. Unicode programs are covered in <A HREF="index28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index28.htm" 
target="text">Chapter 28</A>, &quot;Future Explorations,&quot; in the &quot;Unicode&quot; section.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>WINUSER.H is code supplied with Developer Studio. It's typically in the folder \Program 
Files\DevStudio\VC\include.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>If you were creating a Windows program in C, you would need to fill a WNDCLASS structure. The members of the <font color="#008000">WNDCLASS</font> structure are as follows:</P>
<ul>

<li> <font color="#008000">style</font>&#151;A number made by combining standard styles, represented with <font color="#008000">constants</font> like <font color="#008000">CS_GLOBALCLASS</font> or <font color="#008000">CS_OWNDC</font>, 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> <font color="#008000">lpfnWndProc</font>&#151;A pointer to a function that is the Windows Procedure (generally 
called the WindProc) for the class. This function is discussed in <A HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &quot;Messages and Commands.&quot;</P>
<li> <font color="#008000">cbClsExtra</font>&#151;How many extra bytes to add to the window class. 
Usually 0, but C programmers would sometimes build a window class with extra data in it.</P>
<li> <font color="#008000">cbWndExtra</font>&#151;How many extra bytes to add to each instance of the window. Usually 0.</P>
<li> <font 
color="#008000">hInstance</font>&#151;A handle to an instance of an application, the running program that is registering this window class. For now, just think of this as a way that the window class can reach the application that uses it.</P>
<li> <font 
color="#008000">hIcon</font>&#151;An icon to be drawn when the window is minimized. Typically this is set with a call to another API function, <font color="#008000">LoadIcon()</font>.</P>
<li> <font color="#008000">hCursor</font>&#151;The cursor to be 
displayed when the mouse is over the screen window associated with this window class. Typically this is set with a call to the API function <font color="#008000">LoadCursor()</font>.</P>
<li> <font color="#008000">hbrBackground</font>&#151;The brush to be 
used for painting the background of the window. The API call <font color="#008000">GetStockObject()</font> is the usual way to set this variable.</P>
<li> <font color="#008000">lpszMenuName</font>&#151;A long pointer to a string that is zero terminated and 
contains the name of the menu for the window class.</P>
<li> <font color="#008000">lpszClassName</font>&#151;The name for this window class, to be used by <font color="#008000">CreateWindow()</font>, when a window (an instance of the window class) is 
created. You would make a name up.</P>
</ul>
<P><B> Window Creation</B></P>
<P>If you've never written a Windows program before, you might be quite intimidated by having to fill out a WNDCLASS structure. But this is the first step in Windows programming in 
C. However, you can always find simple sample programs to copy, like this one:</P>
<pre><font color="#008000">WNDCLASS wcInit;</font></pre>
<pre><font color="#008000"> wcInit.style = 0;</font></pre>
<pre><font color="#008000"> wcInit.lpfnWndProc = 
(WNDPROC)MainWndProc;</font></pre>
<pre><font color="#008000"> wcInit.cbClsExtra = 0;</font></pre>
<pre><font color="#008000"> wcInit.cbWndExtra = 0;</font></pre>
<pre><font color="#008000"> wcInit.hInstance = hInstance;</font></pre>
<pre><font 
color="#008000"> wcInit.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(ID_ICON));</font></pre>
<pre><font color="#008000"> wcInit.hCursor = LoadCursor (NULL, IDC_ARROW);</font></pre>
<pre><font color="#008000"> wcInit.hbrBackground = GetStockObject 
(WHITE_BRUSH);</font></pre>
<pre><font color="#008000"> wcInit.lpszMenuName = &quot;DEMO&quot;;</font></pre>
<pre><font color="#008000"> wcInit.lpszClassName =&quot;NewWClass&quot;;</font></pre>
<pre><font color="#008000"> return (RegisterClass 
(&amp;wcInit));</font></pre>
<blockquote><p><img src="sidebar.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/sidebar.gif">
<P ALIGN="CENTER">
<CENTER><I>Hungarian Notation</I></CENTER></P>
<P>You might wonder what kind of variable name <font color="#008000">lpszClassName</font> is. You also might wonder why it 
is <font color="#008000">wcInit</font> and not just <font color="#008000">Init. </font> Because Microsoft programmers use a variable naming convention called <I>Hungarian Notation</I>. It is so named because it was popularized at Microsoft by a Hungarian 
programmer named Charles Simonyi, and probably because at first glance, the variable names seem to be written in another language.</P>
<P>In Hungarian Notation, the variable is given a descriptive name, like <font color="#008000">Count</font> or <font 
color="#008000">ClassName,</font> that starts with a capital letter. If it is a multi-word name, each word is capitalized. Then, before the descriptive name, letters are added to indicate the type of the variable&#151; for example, <font 
color="#008000">nCount</font> for an integer, or <font color="#008000">bFlag</font> for a Boolean (True or False) variable. In this way the programmer should never forget a variable type, or do something foolish like passing a signed variable to a function 
that is expecting an unsigned value.</P>
<P>The style has gained widespread popularity, though some people hate it. If you long for the good old days of arguing where to put the brace brackets, or better still whether to call them brace, face, or squiggle 
brackets, but can't find anyone to rehash those old wars any more, you can probably find somebody to argue about Hungarian Notation instead. The arguments in favor boil down to &quot;you catch yourself making stupid mistakes&quot; and the arguments against 
to &quot;it's ugly and hard to read.&quot; But 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:</P>
<TABLE BORDER>
<TR> 
<TD>
<P><B>Prefix</B></P>
<TD>
<P><B>Variable Type</B></P>
<TD>
<P><B>Comment</B></P>
<TR>
<TD>
<P>a</P>
<TD>
<P>Array</P>
<TD><br>
<TR>
<TD>
<P>b</P>
<TD>
<P>Boolean</P>
<TD><br>
<TR>

<TD>
<P>d</P>
<TD>
<P>Double</P>
<TD><br>
<TR>
<TD>
<P>h</P>
<TD>
<P>Handle</P>
<TD><br>
<TR>
<TD>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -