?? c-c++ mfc.htm
字號:
lang=EN-US>Afx:b:14ae:6:3e8f</SPAN></FONT></P>
<P><FONT color=#000000><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用</SPAN><SPAN
lang=EN-US>Spy++</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(</SPAN><SPAN
lang=EN-US>VC++</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所附的一個工具)觀察窗口類的名稱時,會發(fā)現(xiàn)窗口類名稱變成像</SPAN><SPAN
lang=EN-US>Afx:b:14ae:6:3e8f</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">這副奇怪的模樣?</SPAN></FONT></P>
<P><FONT color=#000000><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">原來是</SPAN><SPAN
lang=EN-US>Application Framework </SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把窗口類的名稱轉(zhuǎn)換為</SPAN><SPAN
lang=EN-US>Afx:</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:</SPAN>
<SPAN lang=EN-US>x </SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:</SPAN><SPAN
lang=EN-US>y </SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:</SPAN><SPAN
lang=EN-US>z </SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:</SPAN><SPAN
lang=EN-US>w</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的類型,成為獨一無二的窗口類型名稱:</SPAN></FONT></P>
<P><SPAN lang=EN-US><FONT color=#000000>x</FONT></SPAN><FONT
color=#000000><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:窗口風格的</SPAN><SPAN
lang=EN-US>hex</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值</SPAN></FONT></P>
<P><FONT color=#000000><SPAN lang=EN-US>y</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:窗口鼠標光標的</SPAN><SPAN
lang=EN-US>hex</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值</SPAN></FONT></P>
<P><FONT color=#000000><SPAN lang=EN-US>z</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:窗口后臺顏色的</SPAN><SPAN
lang=EN-US>hex</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值</SPAN></FONT></P>
<P><FONT color=#000000><SPAN lang=EN-US>w</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:窗口圖標的</SPAN><SPAN
lang=EN-US>hex</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值</SPAN></FONT></P>
<P><FONT color=#000000><SPAN lang=EN-US>7.<SPAN
style="FONT: 7pt 'Times New Roman'"> </SPAN>Serializable</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的必要條件</SPAN></FONT></P>
<P><FONT color=#000000><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"> 讓一個對象有</SPAN><SPAN
lang=EN-US>Serializable</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">能力,它必須派生自一個</SPAN><SPAN
lang=EN-US>Serializable</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">類,一個類要成為</SPAN><SPAN
lang=EN-US>Serializable</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,必須有下列五大條件:</SPAN></FONT></P>
<P><FONT color=#000000><SPAN lang=EN-US>(1)</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">從</SPAN><SPAN
lang=EN-US>Cobject</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">派生下來,如此一來,可保有</SPAN><SPAN
lang=EN-US>RTTI</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、動態(tài)生成等機能</SPAN></FONT></P>
<P><FONT color=#000000>(2)<SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">類的聲明部分必須有</SPAN><SPAN
lang=EN-US>DECLARE_SERIAL</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">宏,此宏需要一個參數(shù):類名稱</SPAN></FONT></P>
<P><FONT color=#000000>(3)<SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">類的實作部分必須有</SPAN><SPAN
lang=EN-US>IMPLEMENT_SERIAL</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">宏,此宏需要三個參數(shù):一是類名稱,二是基類名稱,三是</SPAN>
<SPAN lang=EN-US>schema no.</SPAN></FONT></P>
<P><FONT color=#000000>(4)<SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">改寫</SPAN><SPAN
lang=EN-US>Serialize</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">虛擬函數(shù),使它能夠適當?shù)匕杨惖某蓡T變量寫入檔案中</SPAN></FONT></P>
<P><FONT color=#000000>(5)<SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">為此類加上一個</SPAN><SPAN
lang=EN-US>default</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">構造函數(shù)(也就是無參數(shù)之構造函數(shù)),這個條件常為人所忽略,但它是必要的,因為若一個對象來自檔案,</SPAN><SPAN
lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">必須先動態(tài)生成它,而且在沒有任何參數(shù)的情況下調(diào)用其構造函數(shù),然后才從檔案中讀出對象數(shù)據(jù)。</SPAN></FONT></P>
<P><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT
color=#000000> 唉,好東東實在太多了,比如對</FONT></SPAN><FONT
color=#000000><SPAN lang=EN-US>MFC</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">六大關鍵技術之仿真,三大宏的內(nèi)部揭密等等(咦,我不是在推銷這本書吧,呵呵)感興趣的話請看看這本書吧,可以找我借哦,所謂“書非借不能讀也”。呵呵</SPAN><SPAN
lang=EN-US>………</SPAN></FONT><FONT color=#000000><SPAN
lang=EN-US></SPAN></FONT></P></DIV></TD></TR></TBODY></TABLE><BR>
<TABLE height=1884 cellSpacing=1 cellPadding=1 width=758 align=center
bgColor=#000000 border=0>
<TBODY>
<TR bgColor=#5f7189>
<TD width="19%" background="C-C++ MFC.files/bg_bar.gif" bgColor=#ffffff
height=24><FONT color=#000000><STRONG>《深入淺出MFC》學習筆記之二</STRONG></FONT><FONT
color=#000000 size=4><STRONG><A id=VC3 name=VC3></A> </STRONG></FONT><FONT
color=#000000>(文章來源:龔建偉老師的技術主頁http://www.gjwtech.com)</FONT><FONT
color=#000000 size=4> </FONT></TD></TR>
<TR bgColor=#5f7189>
<TD bgColor=#ffffff height=1861>
<P style="MARGIN-TOP: 1px"
align=justify> <BR>
MFC程序也是Windows程序,所以它也有一個WinMain,但是我們在程序中看不到它的蹤影。實際上,在程序進入點之前,有一個(而且只有一個)全局對象(在Hello程序中名為theApp),這是所謂的application
object,當操作系統(tǒng)將程序加載并激活時,這個全局對象獲得配置,其構造函數(shù)會先執(zhí)行,比WinMain更早<SPAN lang=EN-US
style="FONT-SIZE: 12pt; COLOR: black"><O:P>。</O:P></SPAN></P>
<P
align=justify> 書中所舉Hello程序,是一個簡單的MFC程序,其主體在于WinMain和WndProc,而這兩個部分其實都有相當程度的不變性。MFC就是把有著相當固定行為的WinMain內(nèi)部操作封裝在CWinApp中;把有著相當固定行為的WndProc內(nèi)部操作封裝在CFrameWnd中。也就是說,CWinApp代表程序本體,CFrameWnd代表一個主框窗口
</P>
<P
align=justify> 雖然WinMain和WndProc內(nèi)部操作有相當程度的不變性,但面對不同應用程序也需有變化,所以必須以這兩個類為基礎,派生自己的類,并改寫其中一部分成員函數(shù)。
</P>
<P align=justify>CWinApp----取代WinMain的地位 </P>
<P align=justify>傳統(tǒng)上SDK程序的WinMain所完成的工作現(xiàn)由CWinApp的三個函數(shù)完成:<BR>virtual BOOL
InitApplication( );</P>
<P align=justify>virtual BOOL InitInstance( );</P>
<P align=justify>virtual BOOL Run( );</P>
<P align=justify>CFrameWnd-----取代WndProc的地位 </P>
<P align=justify>CFrameWnd主要用來掌握一個窗口</P>
<P align=justify>引爆器------Application object </P>
<P
align=justify> 當執(zhí)行Hello程序時,這個全局對象產(chǎn)生,于是構造函數(shù)(見APPCORE.CPP)執(zhí)行起來,CWinApp之中的成員變量將因為這個全局對象的誕生而獲得配置與初值,配置完成后,WinMain(MFC早已準備好,并由鏈接器直接加到應用程序中去的)登場。
</P>
<P
align=justify> AfxWinInit:是繼CWinApp構造函數(shù)之后的第一個操作;此后的操作是pApp->InitApplication(其中的pApp指向CMyWinApp對象,即本例中的theApp),因為CMyWinApp繼承自CWinApp,而InitApplication又是CWinApp的一個虛擬函數(shù),我們沒有改寫它(大部分情況下也不需要改它),所以上述操作相當于調(diào)用CWinApp::InitApplication。此程序的代碼出現(xiàn)在APPCORE.CPP中;
</P>
<P
align=justify> 繼InitApplication之后,AfxWinMain調(diào)用pApp->InitInstance,InitInstance是CWinApp的一個虛擬函數(shù)(應用程序一定要改寫這個函數(shù),因為它在CWinApp中是個空函數(shù),沒有任何默任操作),我們改寫了它,所以上述操作就是調(diào)有我們自己的這個InitInstance函數(shù),我們將在該處展開我們的主窗口生命。
</P>
<P
align=justify> CMyWinApp::InitInstance一開始new了一個CMyFrameWnd對象,new會引發(fā)構造函數(shù)CmyFrameWnd::CMyFrameWnd,其中調(diào)用了CFrameWnd的成員函數(shù)Create,它將產(chǎn)生一個窗口。
</P>
<P
align=justify> Create函數(shù)共八個參數(shù),第一個,指定WNDCLASS窗口類,如果放置NULL,表示要以MFC內(nèi)建的窗口類產(chǎn)生一個標準的外框窗口(Create函數(shù)在產(chǎn)生窗口之前會引發(fā)窗口類的注冊操作,下一段講述這一內(nèi)容);第二個,指定窗口標題;第三個,指定窗口風格,默認是WS-OVERLAPPEDWINDOW,如果你不想要窗口右上角的極大極小鈕,可以改成WS-OVERLAPPED|WS-CAPTION|WS-SYSMENU|WS-THICKFRAME|WS-MINIMIZEBOX|WS-MAXIMIZEBOX,如果希望有垂直滾動條,再加上WS-VSCROLL;第四個參數(shù),指定窗口的位置與大小,默認值rectDefault
;第五個,指定父窗口,第六個指定菜單;第七個,為擴充風格,唯有以:CreateWindowEx(而非:CreateWindow)函數(shù)才能完成,事實上,CFrameWnd:Create最終調(diào)用的正是:CreateWindowEx;第八個,是一個指向CCreateContext結(jié)構的指針,framework利用它,在具備Document/View結(jié)構的程序中初始化外框窗口,默認值NULL
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -