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

📄 chxa.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<font color="#008000">CreateWindow()</font> would be handled automatically in the <font color="#008000">CWnd</font> constructor, <font color="#008000">CWnd::CWnd</font>, so that when the constructor is called to initialize a CWnd object the corresponding 
window on the screen is created. This would save you, the programmer, a good deal of effort, because you can't forget to call a constructor. In fact, that's not what Microsoft has chosen to do. The constructor looks like this:</P>
<pre><font 
color="#008000">CWnd::CWnd()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> AFX_ZERO_INIT_OBJECT(CCmdTarget);</font></pre>
<pre><font color="#008000">}</font></pre>
<p><font color="#008000">AFX_ZERO_INIT_OBJECT</font> is 
just a macro, expanded by the C++ compiler's preprocessor, that uses the C function <font color="#008000">memset</font> to zero out every byte of every member variable in the object, like this:</P>
<pre><font color="#008000">#define 
AFX_ZERO_INIT_OBJECT(base_class) [ccc]</font></pre>
<pre><font color="#008000"> memset(((base_class*)this)+1, 0, sizeof(*this) [ccc]</font></pre>
<pre><font color="#008000"> - sizeof(class base_class));</font></pre>
<P>The reason why Microsoft chose not to 
call <font color="#008000">CreateWindow()</font> in the constructor is that constructors cannot return a value. If something goes wrong with the window creation, there are no elegant or neat ways to deal with it. Instead, the constructor does almost 
nothing, a step that essentially cannot fail, and the call to <font color="#008000">CreateWindow()</font> is done from within the member function <font color="#008000">Cwnd::Create()</font>, or the closely related <font 
color="#008000">CWnd::CreateEx()</font>, which looks like the one in Listing A.2.:</P>
<P><I>Listing A.2 CWnd::CreateEx() from WINCORE.CPP</I></P>
<pre><font color="#008000">BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,</font></pre>

<pre><font color="#008000"> LPCTSTR lpszWindowName, DWORD dwStyle,</font></pre>
<pre><font color="#008000"> int x, int y, int nWidth, int nHeight,</font></pre>
<pre><font color="#008000"> HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)</font></pre>

<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // allow modification of several common create parameters</font></pre>
<pre><font color="#008000"> CREATESTRUCT cs;</font></pre>
<pre><font color="#008000"> cs.dwExStyle = 
dwExStyle;</font></pre>
<pre><font color="#008000"> cs.lpszClass = lpszClassName;</font></pre>
<pre><font color="#008000"> cs.lpszName = lpszWindowName;</font></pre>
<pre><font color="#008000"> cs.style = dwStyle;</font></pre>
<pre><font color="#008000"> 
cs.x = x;</font></pre>
<pre><font color="#008000"> cs.y = y;</font></pre>
<pre><font color="#008000"> cs.cx = nWidth;</font></pre>
<pre><font color="#008000"> cs.cy = nHeight;</font></pre>
<pre><font color="#008000"> cs.hwndParent = 
hWndParent;</font></pre>
<pre><font color="#008000"> cs.hMenu = nIDorHMenu;</font></pre>
<pre><font color="#008000"> cs.hInstance = AfxGetInstanceHandle();</font></pre>
<pre><font color="#008000"> cs.lpCreateParams = lpParam;</font></pre>
<pre><font 
color="#008000"> if (!PreCreateWindow(cs))</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> PostNcDestroy();</font></pre>
<pre><font color="#008000"> return FALSE;</font></pre>
<pre><font color="#008000"> }</font></pre>

<pre><font color="#008000"> AfxHookWindowCreate(this);</font></pre>
<pre><font color="#008000"> HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,</font></pre>
<pre><font color="#008000"> cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,</font></pre>

<pre><font color="#008000"> cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);</font></pre>
<pre><font color="#008000">#ifdef _DEBUG</font></pre>
<pre><font color="#008000"> if (hWnd == NULL)</font></pre>
<pre><font color="#008000"> {</font></pre>

<pre><font color="#008000"> TRACE1(&quot;Warning: Window creation failed: [ccc]</font></pre>
<pre><font color="#008000"> GetLastError returns 0x%8.8X\n&quot;,</font></pre>
<pre><font color="#008000"> GetLastError());</font></pre>
<pre><font 
color="#008000"> }</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000"> if (!AfxUnhookWindowCreate())</font></pre>
<pre><font color="#008000"> PostNcDestroy();</font></pre>
<pre><font color="#008000"> // cleanup if 
CreateWindowEx fails too soon</font></pre>
<pre><font color="#008000"> if (hWnd == NULL)</font></pre>
<pre><font color="#008000"> return FALSE;</font></pre>
<pre><font color="#008000"> ASSERT(hWnd == m_hWnd); // should have been set in send msg 
hook</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>WINCORE.CPP is code supplied with Developer Studio. It's typically in the folder \Program 
Files\DevStudio\VC\mfc\src.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>This sets up a <font color="#008000">CREATESTRUCT</font> structure very much like a <font color="#008000">WNDCLASS</font>, and fills it with the parameters that were passed to <font 
color="#008000">CreateEx()</font>. It calls <font color="#008000">PreCreateWindow</font>, <font color="#008000">AfxHookWindowCreate()</font>, <font color="#008000">::CreateWindow()</font>, and <font color="#008000">AfxUnhookWindowCreate()</font> before 
checking <font color="#008000">hWnd</font> and returning.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>The AFX prefix on many useful MFC functions dates back to the days when Microsoft's internal name for their class library was Application Framework. The 
<font color="#008000">::</font> in the call to <font color="#008000">CreateWindow</font> identifies it as an API function, sometimes referred to as an SDK function in this context. The other functions are member functions of <font 
color="#008000">CWnd</font> that set up other background boilerplate for you.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>So, on the face of it, there doesn't seem to be any savings here. You declare an instance of some <font color="#008000">CWnd</font> 
object, call its <font color="#008000">Create()</font> function, and have to pass just as many parameters as you did in the old C way of doing things. What's the point? Well, <font color="#008000">CWnd</font> is really a class from which to inherit. Things 
get much simpler in the derived classes. Take <font color="#008000">CButton</font>, for example, a class that encapsulates the concept of a button on a dialog box. A button is just a tiny little window, but its behavior is constrained -- for example the 
user cannot resize a button. Its <font color="#008000">Create()</font> member function looks like this:</P>
<pre><font color="#008000">BOOL CButton::Create(LPCTSTR lpszCaption, DWORD dwStyle,</font></pre>
<pre><font color="#008000"> const RECT&amp; rect, 
CWnd* pParentWnd, UINT nID)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> CWnd* pWnd = this;</font></pre>
<pre><font color="#008000"> return pWnd-&gt;Create(_T(&quot;BUTTON&quot;), lpszCaption, dwStyle, rect, 
pParentWnd, nID);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>That's a lot less parameters. If you want a button, you create a button, and let the class hierarchy fill in the rest.</P>
<H3><B>Getting a Handle on  All These MFC 
Classes</B></H3>
<P>There are over 200 MFC classes. Why so many? What do they do? How can any normal human keep track of them and know which one to use for what? Good questions. Questions that will take a pretty large piece of this book to answer. The 
first half of this book presents the most commonly used MFC classes. This section looks at some of the more important base classes.</P>
<P><B><I> </I></B><B>CObject</B></P>
<P><a href="chxa01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa01.gif">Figure A.1</a> shows a high-level overview of the 
inheritance tree for the classes in MFC. Only a handful of MFC classes do not inherit from <font color="#008000">CObject</font>. <font color="#008000">Cobject</font> contains the basic functionality that all the MFC classes (and most of the new classes you 
create) will be sure to need, like persistence support and diagnostic output. As well, classes derived from CObject can be contained in the MFC container classes, discussed in <A HREF="indexe.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexe.htm" target="text">Reference E</A>, &quot;Useful 
Classes.&quot;</P>
<B><a href="chxa01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa01.gif">Fig. A.1</a></b>
<P><I>Almost all the classes in MFC inherit from </I><I>CObject</I><I>.</I></P>
<P><B>CCmdTarget</B></P>
<P>Some of the classes that inherit from <font color="#008000">CObject</font>, 
like <font color="#008000">CFile</font> and <font color="#008000">CException</font>, and their derived classes, do not need to interact directly with the user and the operating system through messages and commands. All the classes that do need to receive 
messages and commands inherit from <font color="#008000">CCmdTarget</font>. <a href="chxa02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa02.gif">Figure A.2</a> shows a bird's eye view of <font color="#008000">CCmdTarget</font>'s derived classes, generally called command targets.</P>
<B><a 
href="chxa02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa02.gif">Fig. A.2</a></b>
<P><I>Any class that will receive a command must inherit from </I><I>CCmdTarget</I><I>.</I></P>
<P><B>CWnd</B></P>
<P>As already mentioned, <font color="#008000">CWnd</font> is a hugely important class. Only 
classes derived from <font color="#008000">CWnd</font> can receive messages; threads and documents can receive commands but not messages.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P><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; explores the distinction between commands and messages. <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>, &quot;Documents and Views,&quot; explains documents, and <A HREF="index27.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index27.htm" target="text">Chapter 27</A>, &quot;Multitasking with 
Windows Threads,&quot; explains threads.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<p><font color="#008000">Cwnd</font> provides window-oriented functionality like calls to <font color="#008000">CreateWindow</font> and <font 
color="#008000">DestroyWindow</font>, functions to handle painting the window in the screen, processing messages, talking to the Clipboard, and much more&#151;almost 250 member functions in all. Only a handful of these will need to be overridden in derived 
classes. <a href="chxa03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa03.gif">Figure A.3</a> shows the classes that inherit from <font color="#008000">CWnd</font>; there are so many control classes that to list them all would clutter up the diagram, so they are lumped together as control 
classes.</P>
<B><a href="chxa03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxa/chxa03.gif">Fig. A.3</a></b>
<P><I>Any class that will receive a message must inherit from </I><I>CWnd</I><I>, which provides lots of window-related </I><I>functions.</I></P>
<P><B><I>All Those Other Classes</I></B></P>

<P>So you've seen ten classes so far on these three figures. What about the other 200+? You'll meet them in context, throughout the book. If there&#146;s a specific class you were wondering about, check the index. Check the online help, too, because every 
class is documented there. And don't forget, the full source for MFC is included with every copy of Visual C++. Reading the source is a hard way to figure out how a class works, but sometimes you need that level of detail. </P>
<p><hr></p>
<center>

<p><font size=-2>
&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a
Simon and Schuster Company.</font></p>
</center>
</BODY></HTML>

⌨️ 快捷键说明

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