VC++66教材.docx
《VC++66教材.docx》由会员分享,可在线阅读,更多相关《VC++66教材.docx(11页珍藏版)》请在冰豆网上搜索。
VC++66教材
VC++教材第一课宏定义
__cplusplus是C++编译相关的默认宏__stdc__是C编译相关的默认宏。
__STDC__是预定义宏。
当它被定义后,编译器将按照ansic标准来编译你的c程序。
ANSIC标准定义了以下6种可供C语言使用的预定义宏:
__LINE__在源代码中插入当前源代码行号
__FILE__在源代码中插入当前源代码文件名
__DATE__在源代码中插入当前编译日期〔注意和当前系统日期区别开来〕
__TIME__在源代码中插入当前编译时间〔注意和当前系统时间区别开来〕
__STDC__当要求程序严格遵循ANSIC标准时该标识符被赋值为1。
__cplusplus
标识符__LINE__和__FILE__通常用来调试程序;
标识符__DATE__和__TIME__通常用来在编译后的程序中加入一个时间标志,以区分程序的不同版本;
当要求程序严格遵循ANSIC标准时,标识符__STDC__就会被赋值为1;
当用C++编译程序编译时,标识符__cplusplus就会被定义。
根据以上定义,说明C++都是按着宏定义来解释C++代码的,目前分俩种跟编译器相关的宏定义。
还有#define__EXTERN_Cextern"C"也是为了C和C++互相调用相关的定义。
Extern本意是“外部”的意思。
C语句里常出现,C++里一般不建议使用,因为面向对象封装的原因。
一般用于将C++代码以标准C形式输出(即以C的形式被调用),这是因为C++虽然常被认为是C的超集,但是C++的编译器还是与C的编译器不同的。
C中调用C++中的代码这样定义会是安全的。
一般的考虑跨平台使用方法如下:
#ifdefined(__cplusplus)||defined(c_plusplus)//跨平台定义方法extern"C"{#endif//...正常的声明段#ifdefined(__cplusplus)||defined(c_plusplus)}#endif简单的用在windows下可以如下定义:
#ifdef__cplusplusextern"C"{//...正常的声明段}#endif
typedefintLONG32,*PLONG32;
typedefintINT32,*PINT32;
这跟宏定义差不多,学名是“类型定义”。
C++为了跟C区别就定义了很多这类大写的类型名LONGUINTINT等等。
当然同时支持C的小写类型名。
Intlong等。
例如:
typedefunsignedintULONG32,*PULONG32;
typedefunsignedintDWORD32,*PDWORD32;
typedefunsignedintUINT32,*PUINT32;
另:
对于32,64位数据类型,目前多数是32位使用的比较多。
64位基本上没有。
因为32位基本上满足了我们的需求。
64位的宏开关是这样写的:
#ifdef_WIN64
typedef__int64INT_PTR,*PINT_PTR;
typedefunsigned__int64UINT_PTR,*PUINT_PTR;
指针类型和整形类型之间的转换函数定义:
__inline
unsignedlong
HandleToUlong(
void*h
)
{
return((unsignedlong)h);
}
__inline
unsignedlong
PtrToUlong(
void*p
)
{
return((unsignedlong)p);
}
__inline
unsignedshort
PtrToUshort(
void*p
)
{
return((unsignedshort)p);
}
__inline
long
PtrToLong(
void*p
)
{
return((long)p);
}
__inline
short
PtrToShort(
void*p
)
{
return((short)p);
}
#endif
#pragmawarning(3:
4311)//typecasttruncation
#else
typedeflongINT_PTR,*PINT_PTR;
typedefunsignedlongUINT_PTR,*PUINT_PTR;
#defineMAXINT_PTR(0x7fffffffL)指针类型的数据最大值
#defineMININT_PTR(0x80000000L)指针类型的数据最小值
#defineMAXUINT_PTR(0xffffffffUL)指针类型的数据最大值
typedefunsignedshortUHALF_PTR,*PUHALF_PTR;short短无符号整形定义的短指针类型(小指针)
typedefshortHALF_PTR,*PHALF_PTR;short短有符号整形定义的短指针类型(小指针)
#defineMAXUHALF_PTR0xffff短指针类型的最大值
#defineMAXHALF_PTR0x7fff短指针类型的最大值
#defineMINHALF_PTR0x8000短指针类型的最小值
//函数形式的宏定义
#defineHandleToUlong(h)((ULONG)(h))
#definePtrToUlong(p)((ULONG)(p))
#definePtrToLong(p)((LONG)(p))
#definePtrToUshort(p)((unsignedshort)(p))
#definePtrToShort(p)((short)(p))
#endif
inline的函数必须和函数定义一起用才起作用,而且调用方必须能够看到这个函数的定义,如:
//a.h
inlinevoidfoo(void);
//a.c
inlinevoidfoo(void)
{
}
//b.c
#include"a.h"
voidbar(void)
{
foo();
}
这种用法inline是不起任何作用的,只有这样:
//a.h
inlinevoidfoo(void)
{
}
//b.c
#include"a.h"
voidbar(void)
{
foo();
}
这样才能真正的内联;内联是在编译期间处理的,第一种情况,如果编译器编译b.c的时候,只看到foo的声明,而看不到其定义,编译器就没办法把foo函数内联到调用处!
所谓的内联就是把函数里面的代码直接拷贝到执行的代码里面再编译,而不是按着函数实现体来编译。
(以上是basetsd.h文件。
Stdafx.h文件是用来包含标准头文件的,一般C++的函数找不到时就是因为头文件没有包含进来。
)
总结:
宏定义本身其实就是替换掉代码里的宏名为定义体。
只是置换的作用。
函数形式的宏定义用来实现复杂的一些代码形式。
比如参数类型的互换,C++重载功能的宏实现等。
VC++教材第二课MFC类库介绍
CWinApp类:
应用程序入口类,一般从这里开始执行代码。
先简单介绍一下C++类头文件H和实现体文件CPP基本结构。
1,头文件里有针对类的宏定义
//test.h:
mainheaderfilefortheTESTapplication
//
#if!
defined(AFX_TEST_H__67369212_1733_4E81_8D0C_4EB216B09392__INCLUDED_)
#defineAFX_TEST_H__67369212_1733_4E81_8D0C_4EB216B09392__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#ifndef__AFXWIN_H__
#errorinclude'stdafx.h'beforeincludingthisfileforPCH
#endif
#include"resource.h"//mainsymbols
/////////////////////////////////////////////////////////////////////////////
//CTestApp:
//Seetest.cppfortheimplementationofthisclass
//
classCTestApp:
publicCWinApp
{
public:
CTestApp();
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CTestApp)
public:
virtualBOOLInitInstance();
//}}AFX_VIRTUAL
//Implementation
//{{AFX_MSG(CTestApp)
//NOTE-theClassWizardwilladdandremovememberfunctionshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_TEST_H__67369212_1733_4E81_8D0C_4EB216B09392__INCLUDED_)
2,实现体文件
//test.cpp:
Definestheclassbehaviorsfortheapplication.
//
#include"stdafx.h"
#include"test.h"
#include"testDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CTestApp
BEGIN_MESSAGE_MAP(CTestApp,CWinApp)
//{{AFX_MSG_MAP(CTestApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
ON_COMMAND(ID_HELP,CWinApp:
:
OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CTestAppconstruction
CTestApp:
:
CTestApp()
{
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
/////////////////////////////////////////////////////////////////////////////
//TheoneandonlyCTestAppobject
CTestApptheApp;
/////////////////////////////////////////////////////////////////////////////
//CTestAppinitialization
BOOLCTestApp:
:
InitInstance()
{
if(!
AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
returnFALSE;
}
AfxEnableControlContainer();
//Standardinitialization
//Ifyouarenotusingthesefeaturesandwishtoreducethesize
//ofyourfinalexecutable,youshouldremovefromthefollowing
//thespecificinitializationroutinesyoudonotneed.
#ifdef_AFXDLL
Enable3dControls();//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();//CallthiswhenlinkingtoMFCstatically
#endif
CTestDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithCancel
}
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'smessagepump.
returnFALSE;
}
3,CWinApp的InitInstance函数:
应用程序的事例初始化函数,包括初始化代码和初期窗口显示代码。
CTestDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();//建立模式对话框
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithCancel
}
这是对话框模式的代码。
CSingleDocTemplate*pDocTemplate;
//文档模版的创建
pDocTemplate=newCSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CViewDoc),
RUNTIME_CLASS(CMainFrame),//mainSDIframewindow
RUNTIME_CLASS(CViewView));
AddDocTemplate(pDocTemplate);
//解析SHELL命令行
//Parsecommandlineforstandardshellcommands,DDE,fileopen
CCommandLineInfocmdInfo;
ParseCommandLine(cmdInfo);
//执行SHELL命令行
//Dispatchcommandsspecifiedonthecommandline
if(!
ProcessShellCommand(cmdInfo))
returnFALSE;
//显示窗口
//Theoneandonlywindowhasbeeninitialized,soshowandupdateit.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
这是文档模式的代码。