VC++66教材Word文档格式.docx
《VC++66教材Word文档格式.docx》由会员分享,可在线阅读,更多相关《VC++66教材Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
C中调用C++中的代码这样定义会是安全的。
一般的考虑跨平台使用方法如下:
#ifdefined(__cplusplus)||defined(c_plusplus)//跨平台定义方法extern"
{#endif//...正常的声明段#ifdefined(__cplusplus)||defined(c_plusplus)}#endif简单的用在windows下可以如下定义:
#ifdef__cplusplusextern"
{//...正常的声明段}#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);
}
PtrToUlong(
void*p
return((unsignedlong)p);
unsignedshort
PtrToUshort(
return((unsignedshort)p);
long
PtrToLong(
return((long)p);
short
PtrToShort(
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))
inline的函数必须和函数定义一起用才起作用,而且调用方必须能够看到这个函数的定义,如:
//a.h
inlinevoidfoo(void);
//a.c
inlinevoidfoo(void)
//b.c
#include"
a.h"
voidbar(void)
foo();
这种用法inline是不起任何作用的,只有这样:
这样才能真正的内联;
内联是在编译期间处理的,第一种情况,如果编译器编译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>
#ifndef__AFXWIN_H__
#errorinclude'
stdafx.h'
beforeincludingthisfileforPCH
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//!
2,实现体文件
//test.cpp:
Definestheclassbehaviorsfortheapplication.
stdafx.h"
test.h"
testDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
//CTestApp
BEGIN_MESSAGE_MAP(CTestApp,CWinApp)
//{{AFX_MSG_MAP(CTestApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
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
Enable3dControlsStatic();
//CallthiswhenlinkingtoMFCstatically
CTestDlgdlg;
m_pMainWnd=&
dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
elseif(nResponse==IDCANCEL)
//dismissedwithCancel
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'
smessagepump.
returnFALSE;
3,CWinApp的InitInstance函数:
应用程序的事例初始化函数,包括初始化代码和初期窗口显示代码。
//建立模式对话框
这是对话框模式的代码。
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
ProcessShellCommand(cmdInfo))
//显示窗口
//Theoneandonlywindowhasbeeninitialized,soshowandupdateit.
m_pMainWnd->
ShowWindow(SW_SHOW);
UpdateWindow();
这是文档模式的代码。