📄 chxa.htm
字号:
<P>i</P>
<TD>
<P>Integer</P>
<TD>
<P>"index into"</P>
<TR>
<TD>
<P>l</P>
<TD>
<P>Long</P>
<TD><br>
<TR>
<TD>
<P>lp</P>
<TD>
<P>Long pointer to</P>
<TD><br>
<TR>
<TD>
<P>lpfn</P>
<TD>
<P>Long pointer to function</P>
<TD><br>
<TR>
<TD>
<P>m_</P>
<TD>
<P>Member variable</P>
<TD><br>
<TR>
<TD>
<P>n</P>
<TD>
<P>Integer</P>
<TD>
<P>"number of"</P>
<TR>
<TD>
<P>p</P>
<TD>
<P>Pointer to</P>
<TD><br>
<TR>
<TD>
<P>s</P>
<TD>
<P>String</P>
<TD><br>
<TR>
<TD>
<P>sz</P>
<TD>
<P>Zero terminated string</P>
<TD><br>
<TR>
<TD>
<P>u</P>
<TD>
<P>Unsigned integer</P>
<TD><br>
<TR>
<TD>
<P>C</P>
<TD>
<P>Class</P>
<TD><br></TABLE>
<P>Many people add their
own type conventions to variable names; the <font color="#008000">wc</font> in <font color="#008000">wcInit</font> stands for window class.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Filling the wcInit structure and calling RegisterClass is fairly
standard stuff, registering a class called NewWClass with a menu called DEMO and a WindProc called MainWndProc. Everything else about it is ordinary to an experienced Windows C programmer. After registering the class, when those old-time Windows
programmers wanted to create a window on the screen, out popped some code like this:</P>
<pre><font color="#008000"> HWND hWnd;</font></pre>
<pre><font color="#008000"> hInst = hInstance;</font></pre>
<pre><font color="#008000"> hWnd = CreateWindow
(</font></pre>
<pre><font color="#008000"> "NewWClass",</font></pre>
<pre><font color="#008000"> "Demo 1",</font></pre>
<pre><font color="#008000"> WS_OVERLAPPEDWINDOW,</font></pre>
<pre><font color="#008000">
CW_USEDEFAULT,</font></pre>
<pre><font color="#008000"> CW_USEDEFAULT,</font></pre>
<pre><font color="#008000"> CW_USEDEFAULT,</font></pre>
<pre><font color="#008000"> CW_USEDEFAULT,</font></pre>
<pre><font color="#008000"> NULL,</font></pre>
<pre><font
color="#008000"> NULL,</font></pre>
<pre><font color="#008000"> hInstance,</font></pre>
<pre><font color="#008000"> NULL);</font></pre>
<pre><font color="#008000"> if (! hWnd)</font></pre>
<pre><font color="#008000"> return (FALSE);</font></pre>
<pre><font
color="#008000"> ShowWindow (hWnd, nCmdShow);</font></pre>
<pre><font color="#008000"> UpdateWindow (hWnd);</font></pre>
<P>This code calls <font color="#008000">CreateWindow()</font>, then <font color="#008000">ShowWindow()</font>, and <font
color="#008000">UpdateWindow()</font>. The parameters to the API function <font color="#008000">CreateWindow()</font> are as follows:</P>
<ul>
<li> <font color="#008000">lpClassName</font>—A pointer to the class name that was used in the <font
color="#008000">RegisterClass()</font> call.</P>
<li> <font color="#008000">lpWindowName</font>—The window name. You make this up.</P>
<li> <font color="#008000">dwStyle</font>—The window style, made by combining <font
color="#008000">#define</font> constants with the | operator. For a primary application window like this one, <font color="#008000">WS_OVERLAPPEDWINDOW</font> is standard.</P>
<li> <font color="#008000">x</font>—The horizontal position of the window.
<font color="#008000">CW_USEDEFAULT</font> lets the operating system calculate sensible defaults based on the user's screen settings.</P>
<li> <font color="#008000">y</font>—The vertical position of the window. <font
color="#008000">CW_USEDEFAULT</font> lets the operating system calculate sensible defaults based on the user's screen settings.</P>
<li> <font color="#008000">nWidth</font>—The width of the window. <font color="#008000">CW_USEDEFAULT</font> lets the
operating system calculate sensible defaults based on the user's screen settings.</P>
<li> <font color="#008000">nHeight</font>—The height of the window. <font color="#008000">CW_USEDEFAULT</font> lets the operating system calculate sensible defaults
based on the user's screen settings.</P>
<li> <font color="#008000">hWndParent</font>—The handle of the parent or owner window. (Some windows are created by other windows, which own them.) <font color="#008000">NULL</font> means there is no parent to
this window.</P>
<li> <font color="#008000">hMenu</font>—The handle to a menu or child-window identifier, in other words a window owned by this window. <font color="#008000">NULL</font> means there are no children.</P>
<li> <font
color="#008000">hInstance</font>—The handle of application instance that is creating this window.</P>
<li> <font color="#008000">lpParam</font>—A pointer to any extra parameters. None are needed in this example.</P>
</ul>
<p><font
color="#008000">CreateWindow()</font> returns a window handle—everybody calls their window handles <font color="#008000">hWnd</font>—and this handle is used in the rest of the standard code. If it's <font color="#008000">NULL</font>, the window
creation failed. If the handle returned has any non-NULL value, the creation succeeded and the handle is passed to <font color="#008000">ShowWindow()</font> and <font color="#008000">UpdateWindow()</font>, which together draw the actual window on the
screen.</P>
<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>Handles</I></CENTER></P>
<P>A <I>handle</I> is more than just a pointer. Windows programs refer to resources like windows, icons, cursors, and so on with a handle. Behind the
scenes there is a handle table that tracks the address of the resource as well as information about the resource type. It's called a handle because a program uses it as a way to "get hold of" a resource. Handles are typically passed around to
functions that need to use resources, and returned from functions that allocate resources.</P>
<P>There are a number of basic types of handles: HWND for a window handle, HICON for an icon handle, and so on. No matter what kind of handle is being used,
remember it's a way to reach a resource so that you can use it.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<H3><B>Encapsulating the Windows API</B></H3>
<P>API functions create and manipulate windows on the screen, handle drawing, connect programs to Help
files, facilitate threading, manage memory, and much more. When these functions are encapsulated into MFC classes, your programs can accomplish these same basic Windows tasks, with less work on your part.</P>
<P>There are literally thousands of API
functions, and it can take six months to a year to get a good handle on the API, so this book does not attempt to present a mini-tutorial on the API. In the "Programming for Windows" section earlier in this chapter you were reminded about two API
functions, <font color="#008000">RegisterClass()</font> and <font color="#008000">CreateWindow().</font> These form a good illustration of what was difficult about C Windows programming with the API, and how the MFC classes make it easier. Documentation on
the API functions is available from inside Visual C++: click the InfoViewer tab in the Workspace pane, and expand the Platform SDK topic. Within that topic, expand Reference, Functions, and finally Win32 Functions to display a list of alphabetical
categories such as ArrangeIconicWindows to CloseClipboard. Functions are arranged alphabetically within these categories. There are also index entries that will lead to specific functions.</P>
<P><B> Inside </B><B><I>CWnd</I></B></P>
<p><font
color="#008000">CWnd</font> is a hugely important MFC class. Roughly a third of all the MFC classes use it as a base class—classes like <font color="#008000">CDialog</font>, <font color="#008000">CEditView</font>, <font color="#008000">CButton</font>,
and many more. It serves as a wrapper for the old style windows class and the API functions that create and manipulate window classes. For example, the only public member variable is <font color="#008000">m_hWnd</font>, the member variable that stores the
window handle. This variable is set by the member function <font color="#008000">CWnd::Create()</font> and used by almost all the other member functions when they call their associated API functions.</P>
<P>You might think that the call to the API function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -