Visual C++ 入门精解

2016-02-19 20:52 29 1 收藏

图老师小编精心整理的Visual C++ 入门精解希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~

【 tulaoshi.com - 编程语言 】

程序作者:管宁 个人网站: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程序的具体分析。
   更多内容请看C/C++技术学堂  C/C++技术专题  Solaris基础知识入门专题,或
  第一部分匈牙利命名法规则  一般情况下,变量的取名方式为:scope_ + prefix_ + qualifier。  范围前缀_,类型前缀_,限定词。非凡的类型命名,前缀表示:  类、接口
  前缀    类型例子备注LmClass    LmObject表示类型本身
  不与范围前缀结合使用IInterface 接口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 常用的一般数据类型的前缀
  前缀类型内存规格描述例子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 0xDFFF
  VISUAL 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.  更多内容请看C/C++技术学堂  C/C++技术专题  Solaris基础知识入门专题,或
  
   第二部分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通过调用此函数进行消息处理。
   更多内容请看C/C++技术学堂  C/C++技术专题  Solaris基础知识入门专题,或
  第三部分Windows应用程序的基本运行机制与HelloWin程序具体解   
     
  总的来说最基本的Windows应用程序的运行执行顺序总是以如下的基本顺序执行的。顺序结构:调用WinMain函数开始执行--à定义窗口类--à初始化窗口类---à窗口的实例化--à通过消息循环获取消息并将消息发送给消息处理函数做出相应的操作  由于windows应用程序运行的逻辑结构非凡所以代码的具体解释笔者就不把程序于叙述分开了了,这样有利于阅读与分析。 程序运行预览图  下载该程序:点击这里下载(82K, WinZip压缩文件)  分析代码如下://程序作者:管宁 
  //站点:www.cndev-lab.com 
  //所有稿件均有版权,如要转载,请务必注明出处和作者 #include windows.h
  #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 类型的含义是指向以

来源:https://www.tulaoshi.com/n/20160219/1624734.html

延伸阅读
1. 用Microsoft Visual C++ 6.0编译的程序,或者发布在Windows 2000/NT/ME/98 系统 单纯通过拷贝MSVCRxx.DLL文件到应用程序目录或system32目录即可 2. 用Visual Studio 2005以上编译的程序,且发布在Windows XP及以上系统 为了减少DLL引发的配置问题(DLL hell),C和C++运行时由并行 (Side-by-Side) 程序集实现,单纯通过拷贝MSVCRxx.DLL并不...
一、背景 FlashGet的透明效果大家羡慕吧。传统的Windows应用程序想实现半透明效果,一般来说需要处理自己的窗口的WM_Paint消息窗口,很麻烦.现在好了,SetLayeredWindowAttributes是windows的新api,win2000以上才支持,它能使使窗体拥有透明效果.我在Google搜了下,介绍SetLayeredWindowAttributes的文章大多是delphi的和vb的。好不容易找...
标准(窗口)消息:窗口消息一般与窗口内部运作有关,如创建窗口,绘制窗口,销毁窗口,通常,消息是从系统发到窗口,或从窗口发到系统。发送函数SendMessage()或者PostMessage()。除WM_COMMAND之外,所有的以WM_开头的消息。从CWnd派生的类,都可以接受到这类消息。注意:标准消息并不需要我们指定处理函数名称,是默认的对应关系。 宏名称 对...
下面的是学C++时要注重的。 1.把C++当成一门新的语言学习(和C没啥关系!真的。); 2.看《Thinking In C++》,不要看《C++变成死相》; 3.看《The C++ Programming Language》和《Inside The C++ Object Model》,不要因为他们很难而我们自己是初学者所以就不看;4.不要被VC、BCB、BC、MC、TC等词汇所...
信使服务是Windows 2000/XP下面的一种通信服务,通过它我们可以在网上象QQ一样进行实时的交流,但和QQ不同的是信使服务不需要拥有一个号码,它只要知道对方的IP地址--如果在局域网中,只要知道对方的计算机名就可以了。我们可以利用"net send"在命令行下面发送消息,也可以通过"控制面板-管理工具-计算机管理"中的"操作"菜单里面的"所有任...

经验教程

415

收藏

37
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部