VC++技術內幕(第四版)筆記(第14章)

/****************************/

第十四章:可重用框架窗口類


1, 可重用基類的設計:爲某個工程所設計的類應該能夠被提取出來 ,使它進一步一般化後被應用於其他的應用程序。

2, 一些重要函數:
1)CFrameWnd::ActivateFrame
virtual void ActivateFrame( int nCmdShow = -1 );
//以nCmdShow作爲參數調用CWnd::ShowWindow函數來顯示框架窗口(其菜單視圖控制欄窗口也會被同時顯示)。nCmdShow決定窗口是最大化還是最小化等。
//在派生類中重載ActivateFrame ,在nCmdShow傳遞給CFrameWnd::ActivateFrame之前修改nCmdShow的值。還可以調用CWnd::SetWindowPlacement設置框架窗口的位置和尺寸並還可以設置控制欄的可視狀態。
2)CWnd::PreCreateWindow
virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
//PreCreateWindow在調用ActivateFrame函數之前被框架調用,用以在窗口顯示之前改變其窗口的特性(特性主要參考:CREATESTRUCT結構。
//在派生類中重載它,在CS傳給基類之前改變CS結構中值,從而實現定製(顯示)窗口。
3)在MDI中,主框架窗口的ShowWindow函數是由應用程序類InitInstance函數調用,不是由CFrameWnd::ActivateFrame函數調用。故要控制MDI主框架窗口的一些特性,應該在應用程序類InitInstance函數中添加相應的代碼。

3,Window註冊表
1)Window註冊表是一組系統文件,由Window管理,Windows和其他的應用程序可以在註冊表中保存一些永久的信息。
Windows 註冊表被組織成一個層次的數據庫,字符和整型數據可以通過一個多部分鍵值來訪問。
2)操作:
SetRegistryKey   Causes application settings to be stored in the registry instead of .INI files.

GetProfileInt   Retrieves an integer from an entry in the application’s .INI file.
WriteProfileInt  Writes an integer to an entry in the application’s .INI file.
GetProfileString  Retrieves a string from an entry in the application’s .INI file.
WriteProfileString  Writes a string to an entry in the application’s .INI file.
說明:
1)vc5開始,AppWziard在應用程序類的InitInstance函數中有對CWinApp::SetRegistryKey函數的調用,如下:
 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
該函數的調用使應用程序使用註冊表,如果去掉則應用程序不使用註冊表,僅在Windows目錄下創建一個INI文件,並使用該文件。
SetRegistryKey的字符串參數建立最上層鍵,其後的註冊表函數定義下面的兩層(稱爲頭名和入口名)。
2)爲能夠使用註冊表訪問函數,需要一個指向應用實例對象的指針,可以通過AfxGetApp函數獲得。如下:
AfxGetApp()->WriteProfileString("Text formatting","Font","Times Roman");
AfxGetApp()->WriteProfileInt("Text Formatting","Points",10);
3)註冊表訪問函數可以將註冊表當成CString對象或無符號整數來處理。


4,CString類中,LPCTSTR並不是一個指向CString對象的指針,而是一個用來代替const char*的Unicode版本。
1) CString str;
 char ch[]="abcdefg";
 strcpy(str.GetBuffer(strlen(ch)),ch);
 str.ReleaseBuffer();
 pDC->TextOut(0,0,str);
輸出:abcdefg
2) CString str("abcdefg");
 char ch[20];
 strcpy(ch,LPCTSTR(str));
 pDC->TextOut(0,0,ch);
輸出:abcdefg
說明:
char *strcpy( char *strDestination, const char *strSource );
3)編寫帶字符串參數的函數原則:
a,如果不改變字符串內容:可以使用const char*形參,可以使用CString&類型形參
b,如果改變字符串內容,可以使用CString&類型形參(當然可以使用指針,但這兒不建議)。


5,獲取窗口座標
GetWindowPlacement
//Retrieves the show state and the normal (restored), minimized, and maximized positions of a window.
SetWindowPlacement(可以返回最大化前的屏幕座標)
//Sets the show state and the normal (restored), minimized, and maximized positions for a window.
GetWindowRect
//Gets the screen coordinates of CWnd.


/////////////////////////
////////////////////

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章