【轉載之VC++】Visual C++ 入門精解
From: http://www.pconline.com.cn
程
序作者:管寧 個人網站:www.cndev-lab.com
作者保留作品的所有權利,如需轉
載,請務必注明出處和作者。
被過濾廣告 VC作為一個主流的開發平臺一直深受編程愛好者的喜愛,但是很多人卻對它的入門感到難于上青天,究其原因主要是大家 對他錯誤的認識造成的,嚴格的來說VC++不是門語言,雖然它和C++之間有密切的關系,如果形象點比喻的話,可以C++看作為一種”工業標準”,而 VC++則是某種操作系統平臺下的”廠商標準”,而”廠商標準”是在遵循”工業標準”的前提下擴展而來的。
VC++應用程序的開發主要有兩種模式,一種是WIN API方式,另一種則是MFC方式,傳統的WIN API開發方式比較繁瑣,而MFC則是對WIN API再次封裝,所以MFC相對于WIN API開發更具備效率優勢,但為了對WINDOWS開發有一個較為全面細致的認識,筆者在這里還是以講解WIN API的相關內容為主線。
話說到這里可能更多人關心的是學習VC++需要具備什么條件,為什么對于這扇門屢攻不破呢?
要想學習好VC必須具備良好的C/C++的基礎,必要的英語閱讀能力也是必不可少的,因為大量的技術文檔多以英文形式發布。
許多初學VC++的人對于它怪異的寫法和程序奇特的工作方式非常不理解,為了幫助大家對它的入門有一個比較概括的了解,我們把 這一小節內容分成若干部分講解。
第一部分: VC++中的對象的命名規則、常用宏定義的命名,以及VC++下的數據類型。
注:這部分簡單瀏覽即可。
第二部分:VC++常用技術術語的解釋。
第三部分:HelloWin程序的詳細分析。
第一部分
匈牙利命名法規則
一般情況下,變量的取名方式為:
<scope_> + <prefix_> + <qualifier>。
范圍前綴_,類型前綴_,限定詞。
特殊的類型命名,前綴表示:
類、接口
前綴 |
類型 |
例子 |
備注 |
Lm |
Class |
LmObject |
表示類型本身 |
I |
Interface 接口 |
IUnknown |
注:類名前綴改為Lm,對于非全局的類最好有語義表示其所屬模塊。類的實例命名與類名大致相同,只是類名語義表示類的通用含義,而類名表示此實 例的具體語義。如類名LmSketPoint表示草圖點的類定義,而它的兩個實例 _StartPoint,_EndPoint分別代表起點和終點的語義。類的實例命名帶上前綴_。
特殊約定:
a. MouseTool的派生類的前綴為_Mt.
b. 對話框類的前綴為CDlg.
c.
橡皮條類的前綴為_Rb.
凡圍前綴:
前綴 |
類型 |
例子 |
備注 |
g_ |
全局作用域 |
g_Servers |
|
m_ |
成員變量 |
m_pDoc, |
|
l_ |
局部作用域 |
l_strName |
少用 |
注: 編程時盡量少用全程變量,對于全程變量還應在類型前綴后加上如下關鍵字:
特征模塊 : Fea
草圖模塊 : Sket
裝配模塊 :
Asm
工程圖模塊: Lay
曲面模塊 : Surf
界面模塊 : Ui
常用的一般數據類型的前綴
前綴 |
類型 |
內存規格描述 |
例子 |
ch |
char |
8-bit character |
chGrade |
ch |
TCHAR |
16-bit character if _UNICODE is defined |
chName |
b |
BOOL |
Boolean value |
bEnabled |
n |
int |
Integer (size dependent on operating system) |
nLength |
n |
UINT |
Unsigned value (size dependent on operating system) |
nLength |
w |
WORD |
16-bit unsigned value |
wPos |
l |
LONG |
32-bit signed integer |
lOffset |
dw |
DWORD |
32-bit unsigned integer |
dwRange |
p |
* |
Ambient memory model pointer |
pDoc |
lp |
FAR* |
Far pointer |
lpDoc |
lpsz |
LPSTR |
32-bit pointer to character string |
lpszName |
lpsz |
LPCSTR |
32-bit pointer to constant character string |
lpszName |
lpsz |
LPCTSTR |
32-bit pointer to constant character string if _UNICODE is defined |
lpszName |
h |
handle |
Handle to Windows object |
hWnd |
lpfn |
(*fn)() |
callbackFar pointer to CALLBACK function |
lpfnAbort |
常用Windows對象名稱縮寫
Windows 對象 |
例子變量 |
MFC 類 |
例子對象 |
HWND |
hWnd; |
CWnd* |
pWnd; |
HDLG |
hDlg; |
CDialog* |
pDlg; |
HDC |
hDC; |
CDC* |
pDC; |
HGDIOBJ |
hGdiObj; |
CGdiObject* |
pGdiObj; |
HPEN |
hPen; |
CPen* |
pPen; |
HBRUSH |
hBrush; |
CBrush* |
pBrush; |
HFONT |
hFont; |
CFont* |
pFont; |
HBITMAP |
hBitmap; |
CBitmap* |
pBitmap; |
HPALETTE |
hPalette; |
CPalette* |
pPalette; |
HRGN |
hRgn; |
CRgn* |
pRgn; |
HMENU |
hMenu; |
CMenu* |
pMenu; |
HWND |
hCtl; |
CStatic* |
pStatic; |
HWND |
hCtl; |
CButton* |
pBtn; |
HWND |
hCtl; |
CEdit* |
pEdit; |
HWND |
hCtl; |
CListBox* |
pListBox; |
HWND |
hCtl; |
CComboBox* |
pComboBox; |
Visual C++常用宏定義命名列表
前綴 |
符號類型 |
符號例子 |
范圍 |
IDR_ |
標識多個資源共享的類型 |
IDR_MAINFRAME |
1 to 0x6FFF |
IDD_ |
對話框資源(Dialog) |
IDD_SPELL_CHECK |
1 to 0x6FFF |
IDB_ |
位圖資源(Bitmap) |
IDB_COMPANY_LOGO |
1 to 0x6FFF |
IDC_ |
光標資源(Cursor) |
IDC_PENCIL |
1 to 0x6FFF |
IDI_ |
圖標資源(Icon) |
IDI_NOTEPAD |
1 to 0x6FFF |
ID_IDM_ |
工具欄或菜單欄的命令項 |
ID_TOOLS_SPELLING |
0x8000 to 0xDFFF |
HID_ |
命令上下文幫助(Command Help context) |
HID_TOOLS_SPELLING |
0x18000 to 0x1DFFF |
IDP_ |
消息框提示文字資源 |
IDP_INVALID_PARTNO |
8 to 0xDFFF |
HIDP_ |
消息框上下文幫助(Message-box Help context) |
HIDP_INVALID_PARTNO |
0x30008 to 0x3DFFF |
IDS_ |
字符串資源(String) |
IDS_COPYRIGHT |
1 to 0x7FFF |
IDC_ |
對話框內的控制資源(Control) |
IDC_RECALC |
8 to 0xDFFF |
VISUAL C++ 下的數據類型
類型 |
含義 |
ATOM |
Atom. For more information, see Atoms. |
BOOL |
Boolean variable (should be TRUE or FALSE). |
BOOLEAN |
Boolean variable (should be TRUE or FALSE). |
BYTE |
Byte (8 bits). |
CALLBACK |
Calling convention for callback functions. |
CHAR |
8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts. |
COLORREF |
Red, green, blue (RGB) color value (32 bits). See COLORREF for information on this type. |
CONST |
Variable whose value is to remain constant during execution. |
DWORD |
32-bit unsigned integer. |
DWORD_PTR |
Unsigned long type for pointer precision. Use when casting a pointer to a long type to perform pointer arithmetic. (Also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows. ) |
DWORD32 |
32-bit unsigned integer. |
DWORD64 |
64-bit unsigned integer. |
FLOAT |
Floating-point variable. |
HACCEL |
Handle to an accelerator table. |
HANDLE |
Handle to an object. |
HBITMAP |
Handle to a bitmap. |
HBRUSH |
Handle to a brush. |
HCONV |
Handle to a dynamic data exchange (DDE) conversation. |
HCONVLIST |
Handle to a DDE conversation list. |
HCURSOR |
Handle to a cursor. |
HDC |
Handle to a device context (DC). |
HDDEDATA |
Handle to DDE data. |
HDESK |
Handle to a desktop. |
HDROP |
Handle to an internal drop structure. |
HDWP |
Handle to a deferred window position structure. |
HENHMETAFILE |
Handle to an enhanced metafile. |
HFILE |
Handle to a file opened by OpenFile , not CreateFile . |
HFONT |
Handle to a font. |
HGDIOBJ |
Handle to a GDI object. |
HGLOBAL |
Handle to a global memory block. |
HHOOK |
Handle to a hook. |
HICON |
Handle to an icon. |
HIMAGELIST |
Handle to an image list. |
HIMC |
Handle to input context. |
HINSTANCE |
Handle to an instance. |
HKEY |
Handle to a registry key. |
HKL |
Input locale identifier. |
HLOCAL |
Handle to a local memory block. |
HMENU |
Handle to a menu. |
HMETAFILE |
Handle to a metafile. |
HMODULE |
Handle to a module. The value is the base address of the module. |
HMONITOR |
Handle to a display monitor. |
HPALETTE |
Handle to a palette. |
HPEN |
Handle to a pen. |
HRGN |
Handle to a region. |
HRSRC |
Handle to a resource. |
HSZ |
Handle to a DDE string. |
HWINSTA |
Handle to a window station. |
HWND |
Handle to a window. |
INT |
32-bit signed integer. |
INT_PTR |
Signed integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic. |
INT32 |
32-bit signed integer. |
INT64 |
64-bit signed integer. |
LANGID |
Language identifier. For more information, see Locales. |
LCID |
Locale identifier. For more information, see Locales. |
LCTYPE |
Locale information type. For a list, see Locale and Language Information. |
LONG |
32-bit signed integer. |
LONG_PTR |
Signed long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic. |
LONG32 |
32-bit signed integer. |
LONG64 |
64-bit signed integer. |
LONGLONG |
64-bit signed integer. |
LPARAM |
Message parameter. |
LPBOOL |
Pointer to a BOOL . |
LPBYTE |
Pointer to a BYTE . |
LPCOLORREF |
Pointer to a COLORREF value. |
LPCRITICAL_SECTION |
Pointer to a CRITICAL_SECTION . |
LPCSTR |
Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. |
LPCTSTR |
An LPCWSTR if UNICODE is defined, an LPCTSTR otherwise. |
LPCVOID |
Pointer to a constant of any type. |
LPCWSTR |
Pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. |
LPDWORD |
Pointer to a DWORD . |
LPHANDLE |
Pointer to a HANDLE . |
LPINT |
Pointer to an INT . |
LPLONG |
Pointer to a LONG . |
LPSTR |
Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. |
LPTSTR |
An LPWSTR if UNICODE is defined, an LPSTR otherwise. |
LPVOID |
Pointer to any type. |
LPWORD |
Pointer to a WORD . |
LPWSTR |
Pointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. |
LRESULT |
Signed result of message processing. |
LUID |
Locally unique identifier. |
PBOOL |
Pointer to a BOOL . |
PBOOLEAN |
Pointer to a BOOL . |
PBYTE |
Pointer to a BYTE . |
PCHAR |
Pointer to a CHAR . |
PCRITICAL_SECTION |
Pointer to a CRITICAL_SECTION . |
PCSTR |
Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. |
PCTSTR |
A PCWSTR if UNICODE is defined, a PCSTR otherwise. |
PCWCH |
Pointer to a constant WCHAR . |
PCWSTR |
Pointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. |
PDWORD |
Pointer to a DWORD . |
PFLOAT |
Pointer to a FLOAT . |
PHANDLE |
Pointer to a HANDLE . |
PHKEY |
Pointer to an HKEY . |
PINT |
Pointer to an INT . |
PLCID |
Pointer to an LCID . |
PLONG |
Pointer to a LONG . |
PLUID |
Pointer to a LUID . |
POINTER_32 |
32-bit pointer. On a 32-bit system, this is a native pointer. On a 64-bit system, this is a truncated 64-bit pointer. |
POINTER_64 |
64-bit pointer. On a 64-bit system, this is a native pointer. On a 32-bit system, this is a sign-extended 32-bit pointer. |
PSHORT |
Pointer to a SHORT . |
PSTR |
Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts. |
PTBYTE |
Pointer to a TBYTE . |
PTCHAR |
Pointer to a TCHAR . |
PTSTR |
PWSTR if UNICODE is defined, a PSTR otherwise. |
PTBYTE |
Pointer to a TBYTE . |
PTCHAR |
Pointer to a TCHAR . |
PTSTR |
A PWSTR if UNICODE is defined, a PSTR otherwise. |
PUCHAR |
Pointer to a UCHAR . |
PUINT |
Pointer to a UINT . |
PULONG |
Pointer to a ULONG . |
PUSHORT |
Pointer to a USHORT . |
PVOID |
Pointer to any type. |
PWCHAR |
Pointer to a WCHAR . |
PWORD |
Pointer to a WORD . |
PWSTR |
Pointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. |
REGSAM |
Security access mask for registry key. |
SC_HANDLE |
Handle to a service control manager database. For more information, see SCM Handles. |
SC_LOCK |
Handle to a service control manager database lock. For more information, see SCM Handles. |
SERVICE_STATUS_HANDLE |
Handle to a service status value. For more information, see SCM Handles. |
SHORT |
Short integer (16 bits). |
SIZE_T |
The maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer. |
SSIZE_ T |
Signed SIZE_T . |
TBYTE |
A WCHAR if UNICODE is defined, a CHAR otherwise. |
TCHAR |
A WCHAR if UNICODE is defined, a CHAR otherwise. |
UCHAR |
Unsigned CHAR . |
UINT |
Unsigned INT . |
UINT_PTR |
Unsigned INT_PTR . |
UINT32 |
Unsigned INT32 . |
UINT64 |
Unsigned INT64 . |
ULONG |
Unsigned LONG . |
ULONG_PTR |
Unsigned LONG_PTR . |
ULONG32 |
Unsigned LONG32 . |
ULONG64 |
Unsigned LONG64 . |
ULONGLONG |
64-bit unsigned integer. |
UNSIGNED |
Unsigned attribute. |
USHORT |
Unsigned SHORT . |
VOID |
Any type. |
WCHAR |
16-bit Unicode character. For more information, see Character Sets Used By Fonts. |
WINAPI |
Calling convention for system functions. |
WORD |
16-bit unsigned integer. |
WPARAM |
Message parameter. |
第
二部分
WINDOWS應用程序設計用到的基本術語:
1.窗口
任何一個使用過Windows的人對窗口這個概念絕對不會陌生,窗口是windows應用程序的基本操作單元,用戶通過它與應用程序發生交互, 例如輸入輸出操作等等,從程序的內部工作原來來看,每一個窗口對應一個消息處理隊列,應用程序主要通過窗口消息處理函數對用戶的輸入操作進行響應與處理。 要想從程序員的角度充分理解窗口的含義,那么對WNDCLASS這個數據結構進行充分的了解是必須的。
2.實例
單個實例代表一個可執行程序在內存中的拷貝,如果一個應用程序執行許多次,那么在內存中就有多少個拷貝,也就可以說明有多少個實例存在。
3.句柄
句柄在windows環境下被定義成了一個無符號的整數,用于標識應用程序中不同的對象和同類對象中的不同實例。句柄可以看成是對象的編號,聯 系上面的實例,那么一個實例句柄就可以看作是單個應用程序在內存中拷貝的唯一身份編號,通常系統只能通過實例句柄去識別不同的應用程序,或者是相同應用程 序的不同副本。
4.資源
Windows應用程序包含很多資源,例如,菜單,圖標,對話框等等,VC++環境下我們不僅僅可以使用系統下原有的資源,我們也可以定義自己 的資源,這些資源被定義在.RC文件中,通過應用程序最后的編譯,這些資源文件和程序代碼連接在一起,形成一個可執行的.EXE文件或者是一個.DLL的 庫文件。在使用這些資源的時候,通過WIN API函數學將這些資源調用使用。
5.窗口消息處理函數
窗口是人機交互的接口,當窗口接受到輸入請求的時候,就會把這一請求交給某一個函數進行處理,而這個函數就是窗口消息處理函數,它能夠決定當一 個消息被接受到的時候采取什么行動。
消息通常是由一系列的輸入操作觸發的,比如當我按下鼠標左鍵那么窗口消息處理函數就會收到一個WM_LBUTTONDOWN的消息信號。在窗口 消息處理函數中,我們可以利用switch和case結構進行控制, 針對此消息作出我們想要的操作。
6.圖形設備接口
應用程序的任何輸出操作都需要通過圖形設備接口(GDI)中的函數來完成操作,GDI負責系統與用戶或繪圖程序之間的信息交換,并控制在輸出設 備上顯示圖形或者文字,它將程序員與具體的硬件設備隔離開,讓程序員不需要考慮硬件設備操作的細節。
7.回調函數
回調函數是windows操作系統自己調用的函數,用戶是不能直接調用他們的。回調函數的定義必須嚴格的按照windows標準進行編寫。
在下面我們將要看到的HelloWin程序中,WndProc就是一個回調函數,它是是應用程序的窗口消息處理函數,當注冊窗口類的時候,要把
窗口消息處理函數的地址告訴Windows,Windows通過調用此函數進行消息處理。
第三部
分
Windows應用程序的基本運行機制與HelloWin程序詳細解
總的來說最基本的Windows應用程序的運行執行順序總是以如下的基本順序執行的。
順序結構:
調用WinMain函數開始執行--à定義窗口類--à初始化窗口類---à窗口的實例化--à通過消息循環獲取消息并將消 息發送給消息處理函數做出相應的操作
由于windows應用程序運行的邏輯結構特殊所以代碼的詳細解釋筆者就不把程序于敘述分開了了,這樣有利于閱讀與分析。
程序運行預覽圖
下載該程序:點 擊這里下載(82K, winzip壓縮文件)
分析代碼如下:

















































































































































































































































































































posted on 2010-03-26 21:41 LynnRaymond 閱讀(1042) 評論(0) 編輯 收藏 引用