Visual C入门精解.doc_第1页
Visual C入门精解.doc_第2页
Visual C入门精解.doc_第3页
Visual C入门精解.doc_第4页
Visual C入门精解.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Visual C+ 入门精解 文章来源:pconline程序作者:管宁个人网站: 作者保留作品的所有权利,如需转载,请务必注明出处和作者。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程序的详细分析。第一部分匈牙利命名法规则一般情况下,变量的取名方式为: + + 。范围前缀_,类型前缀_,限定词。特殊的类型命名,前缀表示:类、接口前缀 类型例子备注LmClass LmObject表示类型本身不与范围前缀结合使用IInterface 接口IUnknown注:类名前缀改为Lm,对于非全局的类最好有语义表示其所属模块。类的实例命名与类名大致相同,只是类名语义表示类的通用含义,而类名表示此实例的具体语义。如类名LmSketPoint表示草图点的类定义,而它的两个实例 _StartPoint,_EndPoint分别代表起点和终点的语义。类的实例命名带上前缀_。特殊约定:a MouseTool的派生类的前缀为_Mt.b 对话框类的前缀为CDlg.c 橡皮条类的前缀为_Rb.凡围前缀:前缀类型例子备注g_全局作用域g_Serversm_成员变量m_pDoc, l_局部作用域l_strName少用注:编程时尽量少用全程变量,对于全程变量还应在类型前缀后加上如下关键字:特征模块 : Fea草图模块 : Sket装配模块 : Asm工程图模块: Lay曲面模块 : Surf界面模块 : Ui常用的一般数据类型的前缀前缀类型内存规格描述例子chchar8-bit characterchGradechTCHAR16-bit character if _UNICODE is definedchNamebBOOLBoolean valuebEnablednintInteger (size dependent on operating system)nLengthnUINTUnsigned value (size dependent on operating system)nLengthwWORD16-bit unsigned valuewPoslLONG32-bit signed integerlOffsetdwDWORD32-bit unsigned integerdwRangep*Ambient memory model pointerpDoclpFAR*Far pointerlpDoclpszLPSTR32-bit pointer to character stringlpszNamelpszLPCSTR32-bit pointer to constant character stringlpszNamelpszLPCTSTR32-bit pointer to constant character string if _UNICODE is definedlpszNamehhandleHandle to Windows objecthWndlpfn(*fn)()callbackFar pointer to CALLBACK functionlpfnAbort常用Windows对象名称缩写Windows 对象例子变量MFC类例子对象HWNDhWnd;CWnd*pWnd;HDLGhDlg;CDialog*pDlg;HDChDC;CDC*pDC;HGDIOBJhGdiObj;CGdiObject*pGdiObj;HPENhPen;CPen*pPen; HBRUSHhBrush;CBrush*pBrush; HFONThFont;CFont*pFont; HBITMAPhBitmap;CBitmap*pBitmap; HPALETTEhPalette;CPalette*pPalette; HRGNhRgn;CRgn*pRgn; HMENUhMenu;CMenu*pMenu; HWNDhCtl;CStatic*pStatic; HWNDhCtl;CButton*pBtn;HWNDhCtl;CEdit*pEdit; HWNDhCtl;CListBox*pListBox;HWNDhCtl;CComboBox*pComboBox;Visual C+常用宏定义命名列表前缀符号类型符号例子范围IDR_标识多个资源共享的类型IDR_MAINFRAME1 to 0x6FFFIDD_对话框资源(Dialog)IDD_SPELL_CHECK1 to 0x6FFFIDB_位图资源(Bitmap)IDB_COMPANY_LOGO1 to 0x6FFFIDC_光标资源(Cursor)IDC_PENCIL1 to 0x6FFFIDI_图标资源(Icon)IDI_NOTEPAD1 to 0x6FFFID_IDM_工具栏或菜单栏的命令项ID_TOOLS_SPELLING0x8000 to 0xDFFFHID_命令上下文帮助(Command Help context)HID_TOOLS_SPELLING0x18000 to 0x1DFFFIDP_消息框提示文字资源IDP_INVALID_PARTNO8 to 0xDFFFHIDP_消息框上下文帮助(Message-box Help context)HIDP_INVALID_PARTNO0x30008 to 0x3DFFFIDS_字符串资源(String)IDS_COPYRIGHT1 to 0x7FFFIDC_对话框内的控制资源(Control)IDC_RECALC8 to 0xDFFFVISUAL C+ 下的数据类型类型含义ATOMAtom. For more information, see Atoms.BOOLBoolean variable (should be TRUE or FALSE).BOOLEANBoolean variable (should be TRUE or FALSE).BYTEByte (8 bits).CALLBACKCalling convention for callback functions.CHAR8-bit Windows (ANSI) character. For more information, see Character Sets Used By Fonts.COLORREFRed, green, blue (RGB) color value (32 bits). See COLORREF for information on this type.CONSTVariable whose value is to remain constant during execution.DWORD32-bit unsigned integer.DWORD_PTRUnsigned 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. )DWORD3232-bit unsigned integer.DWORD6464-bit unsigned integer.FLOATFloating-point variable.HACCELHandle to an accelerator table.HANDLEHandle to an object.HBITMAPHandle to a bitmap. HBRUSHHandle to a brush.HCONVHandle to a dynamic data exchange (DDE) conversation.HCONVLISTHandle to a DDE conversation list.HCURSORHandle to a cursor.HDCHandle to a device context (DC).HDDEDATAHandle to DDE data.HDESKHandle to a desktop.HDROPHandle to an internal drop structure.HDWPHandle to a deferred window position structure.HENHMETAFILEHandle to an enhanced metafile.HFILEHandle to a file opened by OpenFile, not CreateFile.HFONTHandle to a font.HGDIOBJHandle to a GDI object.HGLOBALHandle to a global memory block.HHOOKHandle to a hook.HICONHandle to an icon.HIMAGELISTHandle to an image list.HIMCHandle to input context.HINSTANCEHandle to an instance.HKEYHandle to a registry key.HKLInput locale identifier.HLOCALHandle to a local memory block.HMENUHandle to a menu.HMETAFILEHandle to a metafile.HMODULEHandle to a module. The value is the base address of the module.HMONITORHandle to a display monitor.HPALETTEHandle to a palette.HPENHandle to a pen. HRGNHandle to a region.HRSRCHandle to a resource.HSZHandle to a DDE string.HWINSTAHandle to a window station.HWNDHandle to a window.INT32-bit signed integer.INT_PTRSigned integral type for pointer precision. Use when casting a pointer to an integer to perform pointer arithmetic.INT3232-bit signed integer.INT6464-bit signed integer.LANGIDLanguage identifier. For more information, see Locales.LCIDLocale identifier. For more information, see Locales.LCTYPELocale information type. For a list, see Locale and Language Information.LONG32-bit signed integer. LONG_PTRSigned long type for pointer precision. Use when casting a pointer to a long to perform pointer arithmetic.LONG3232-bit signed integer.LONG6464-bit signed integer.LONGLONG64-bit signed integer.LPARAMMessage parameter.LPBOOLPointer to a BOOL. LPBYTEPointer to a BYTE. LPCOLORREFPointer to a COLORREF value.LPCRITICAL_SECTIONPointer to a CRITICAL_SECTION.LPCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.LPCTSTRAn LPCWSTR if UNICODE is defined, an LPCTSTR otherwise.LPCVOIDPointer to a constant of any type.LPCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.LPDWORDPointer to a DWORD.LPHANDLEPointer to a HANDLE.LPINTPointer to an INT.LPLONGPointer to a LONG.LPSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.LPTSTRAn LPWSTR if UNICODE is defined, an LPSTR otherwise.LPVOIDPointer to any type.LPWORDPointer to a WORD.LPWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.LRESULTSigned result of message processing.LUIDLocally unique identifier. PBOOLPointer to a BOOL.PBOOLEANPointer to a BOOL.PBYTEPointer to a BYTE.PCHARPointer to a CHAR.PCRITICAL_SECTIONPointer to a CRITICAL_SECTION.PCSTRPointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.PCTSTRA PCWSTR if UNICODE is defined, a PCSTR otherwise.PCWCHPointer to a constant WCHAR.PCWSTRPointer to a constant null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts. PDWORDPointer to a DWORD.PFLOATPointer to a FLOAT.PHANDLEPointer to a HANDLE.PHKEYPointer to an HKEY.PINTPointer to an INT. PLCIDPointer to an LCID.PLONGPointer to a LONG.PLUIDPointer to a LUID. POINTER_3232-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_6464-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.PSHORTPointer to a SHORT.PSTRPointer to a null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.PTBYTEPointer to a TBYTE.PTCHARPointer to a TCHAR.PTSTRPWSTR if UNICODE is defined, a PSTR otherwise.PTBYTEPointer to a TBYTE.PTCHARPointer to a TCHAR.PTSTRA PWSTR if UNICODE is defined, a PSTR otherwise.PUCHARPointer to a UCHAR.PUINTPointer to a UINT.PULONGPointer to a ULONG.PUSHORTPointer to a USHORT.PVOIDPointer to any type.PWCHARPointer to a WCHAR.PWORDPointer to a WORD.PWSTRPointer to a null-terminated string of 16-bit Unicode characters. For more information, see Character Sets Used By Fonts.REGSAMSecurity access mask for registry key.SC_HANDLEHandle to a service control manager database. For more information, see SCM Handles.SC_LOCKHandle to a service control manager database lock. For more information, see SCM Handles.SERVICE_STATUS_HANDLEHandle to a service status value. For more information, see SCM Handles.SHORTShort integer (16 bits).SIZE_TThe maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer.SSIZE_ TSigned SIZE_T.TBYTEA WCHAR if UNICODE is defined, a CHAR otherwise.TCHARA WCHAR if UNICODE is defined, a CHAR otherwise.UCHARUnsigned CHAR.UINTUnsigned INT.UINT_PTRUnsigned INT_PTR.UINT32Unsigned INT32.UINT64Unsigned INT64.ULONGUnsigned LONG.ULONG_PTRUnsigned LONG_PTR.ULONG32Unsigned LONG32.ULONG64Unsigned LONG64.ULONGLONG64-bit unsigned integer.UNSIGNEDUnsigned attribute.USHORTUnsigned SHORT.VOIDAny type.WCHAR16-bit Unicode character. For more information, see Character Sets Used By Fonts.WINAPICalling convention for system functions.WORD16-bit unsigned integer.WPARAMMessage 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压缩文件)分析代码如下:/程序作者:管宁 /站点: /所有稿件均有版权,如要转载,请务必注明出处和作者#include #pragma comment(lib,winmm.lib)/为了要播放声音,必须导入这个库LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)/* HINSTANCE 类型的含义为实例句柄。 hInstance 事实上就是当前应用程序自身的标识代号,代号通常都是一个32位整数。 hPrevInstance 与过去的16位应用程序有关系,表示指向前一个实例的句柄。 PSTR 类型的含义是指向以0结尾的字符串指针。 szCmdLine 前面的sz同样是表示指向以0结尾的字符串指针,这个对象用于保存命令行。 最后iCmdShow是一个整型数据,标记了程序最初的显示状态。 为SW_SHOWNORAML的时候为一般大小显示方式。 为SW_SHOWMAXIMIZED的时候为最大化显示方式。 为SW_SHOWMINNOACTIVE的时候程序将显示在任务栏上。*/ static char szAppName = TEXT(HelloWin);/预先定义一个c风格字符串,稍后用于设置窗口类名称。 WNDCLASS wndclass;/定义窗口类对象/* 在这里不得不说一下的是,窗口类事实上是struct结构体,内部有10个分量,他们是用来于初始化窗口类对象而用的。 这个结构体在winuser.h头文件中定义,从方式上来说,分为ASCII版的WNDCLASSA和Unicode版的WNDCLASSW两个。 typedef struct tagWNDCLASSA UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCSTR lpszMenuName; LPCSTR lpszClassName; WNDCLASSA, *PWNDCLASSA, NEAR *NPWNDCLASSA, FAR *LPWNDCLASSA; typedef struct tagWNDCLASSW UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCWSTR lpszMenuName; LPCWSTR lpszClassName; WNDCLASSW, *PWNDCLASSW, NEAR *NPWNDCLASSW, FAR *LPWNDCLASSW;*/- 窗口类对象初始化过程 - wndclass.style = CS_HREDRAW | CS_VREDRAW; /* 设置窗口类对象的样式风格,CS_HREDRAW | CS_VREDRAW这两个值是通过位运算的与运算结合起来的。 表示了窗口在改变了水平和垂直大小的时候,窗口要强迫刷新。 这些通过define定义的标识,可以在WinUser.h头文件中找到。 #define CS_VREDRAW 0x0001 #define CS_HREDRAW 0x0002 #define CS_DBLCLKS 0x0008 #define CS_OWNDC 0x0020 #define CS_CLASSDC 0x0040 #define CS_PARENTDC 0x0080 #define CS_NOCLOSE 0x0200 #define CS_SAVEBITS 0x0800 #define CS_BYTEALIGNCLIENT 0x1000 #define CS_BYTEALIGNWINDOW 0x2000 #define CS_GLOBALCLASS 0x4000 #define CS_IME 0x00010000 */wndclass.lpfnWndProc = WndProc ;/指定窗口的处理函数为WndProc,WndProc将处理windows消息。 wndclass.cbClsExtra = 0;/窗口类无扩展 wndclass.cbWndExtra = 0;/窗口实例无扩展 wndclass.hInstance = hInstance;/指定当前应用程序实例句柄,也就是程序当前的标识号。wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION); /* 通过LoadIcon函数设置应用程序窗口标题的icon图标。 HICON LoadIcon(HINSTANCE hInstance,LPCTSTR lpIconName); 函数返回HICON类型的图标句柄。 第一个参数表示当前应用程序的窗口句柄,第二个参数表示图标。 默认状态下,第一个参数为NULL,第二个为IDI_APPLICATION,表示使用系统默认提供的图标,可以在WinUser.h头文件中找到。 #define IDI_APPLICATION 32512 */ wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; /* 通过LoadCursor函数设置应用程序窗口光标样式。 HCURSOR LoadCursor(HINSTANCE hInstance,LPCTSTR lpCursorName); 函数返回HCURSOR类型的光标句柄。 第一个参数表示当前应用程序的窗口句柄,第二个参数表示光标。 默认状态下,第一个参数为NULL,第二个为IDC_ARROW,表示使用系统默认提供的光标,可以在WinUser.h头文件中找到。 #define IDC_ARROW MAKEINTRESOURCE(32512) */ wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH); /* 通过GetStockObject函数设置应用程序窗口的背景颜色。 HGDIOBJ GetStockObject(int fnObject); 函数返回HCURSOR类型的GDI对象句柄,为了程序能够正确执行,必须把HGDIOBJ类型强制转换成HBRUSH画刷句柄。 参数表示当前使用的画刷颜色。 这些常量的定义可以在WinGDI.h头文件中找到。 #define WHITE_BRUSH 0 #define LTGRAY_BRUSH 1 #define GRAY_BRUSH 2 #define DKGRAY_BRUSH 3 #define BLACK_BRUSH 4 #define NULL_BRUSH 5 #define HOLLOW_BRUSH NULL_BRUSH */ wndclass.lpszMenuName = NULL;wndclass.lpszClassName = szAppName;/窗口类对象的名称/- RegisterClass (&wndclass); /* 注册窗口类,参数为窗口类对象的指针。 函数原形为: ATOM RegisterClass(CONST WNDCLASS *lpWndClass); */- 实例化过程 - HWND hwnd ; /创建用于保存窗口句柄的对象,窗口句柄是系统识别不同窗口的依据,它只是个代号。 hwnd = CreateWindow( szAppName, / 窗口类名称 你好世界, / 窗口标题 WS_OVERLAPPEDWINDOW, / 窗口样式 CW_USEDEFAULT, / 初始的窗口x轴位置 CW_USEDEFAULT, / 初始的窗口y轴位置

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论