1.mfc中的主要类继承图
2.MFC程序启动
传统上SDK 程序的WinMain 所完成的工作现在由CWinApp 的三个函数完成:
class CWinApp : public CCmdTarget
{
HINSTANCE m_hInstance;
HINSTANCE m_hPrevInstance;
LPTSTR m_lpCmdLine;
int m_nCmdShow;
virtual BOOL InitApplication();
virtual BOOL InitInstance();
virtual int Run();
}
WinMainCRTStartup
-> _tWinMain ( APPMODUL.CPP)
-> AfxWinMain (WINMAIN.CPP)
AfxWinInit()
pApp->InitApplication()
pApp->InitInstance()
thread->Run() // 执行CWinApp::Run()里调用的CWinThread::Run()进行消息循环
- WinMainCRTStartup 程序入口,里面调用_tWinMain,_tWinMain直接调用AfxWinMain。
- AfxWinInit 窗口参数、线程参数初始化
- pApp->InitApplication() 应用程序全局初始化 调用WinApp的InitApplication
- pApp->InitInstance() 执行CMyApp的InitInstance,
// Register the application’s document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSDocDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CSDocView));
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); - thread->Run() // 执行CWinApp::Run()里调用的CWinThread::Run()进行消息循环
■ CWinApp 代表程序本体
■ CFrameWnd 代表一个主框窗口(Frame Window)
由于CDocument 衍生自CObject,所以它就有了CObject 所支持的一切性质,包括执行时期型别信息(RTTI)、动态生成(Dynamic Creation)、文件读写(Serialization 序列化)。又由于它也衍生自CCmdTarget,所以它可以接收来自菜单或工具栏的WM_COMMAND 讯息(消息处理)。
由于CView 衍生自CWnd,所以它可以接收一般Windows 消息(如WM_SIZE、
WM_PAINT 等等),又由于它也衍生自CCmdTarget,所以它可以接收来自菜单或工具列的WM_COMMAND 消息。
3. 添加自定义消息
#define WM_REFRESHINFO WM_USER+5
BEGIN_MESSAGE_MAP(C****, CWnd)
ON_MESSAGE(WM_REFRESHINFO,OnRefreshInfo)
END_MESSAGE_MAP()
afx_msg void OnRefreshInfo(WPARAM wParam,LPARAM lParam);
::SendMessage(((CMainFrame *)AfxGetMainWnd())->m_hWnd,WM_REFRESHDEVICE,0,0);
4.Message Map
定义了数组_messageEntries[]和messageMap,messageMap取messageMap信息。
头文件:DECLARE_MESSAGE_MAP()
#define DECLARE_MESSAGE_MAP()
private:
static const AFX_MSGMAP_ENTRY _messageEntries[];
protected:
static AFX_DATA const AFX_MSGMAP messageMap;
virtual const AFX_MSGMAP* GetMessageMap() const; \
BEGIN_MESSAGE_MAP(C*****, CDialog)
ON_WM_CTLCOLOR()
ON_MESSAGE(WM_REFRESHINFO,OnRefreshInfo)
END_MESSAGE_MAP()
#define BEGIN_MESSAGE_MAP(theClass, baseClass)
const AFX_MSGMAP* theClass::GetMessageMap() const
{ return &theClass::messageMap; }
AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap =
{ &baseClass::messageMap, &theClass::_messageEntries[0] };
AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] =
{ \
#define END_MESSAGE_MAP()
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
}; \
#define ON_MESSAGE(message, memberFxn)
{ message, 0, 0, 0, AfxSig_lwl,
(AFX_PMSG)(AFX_PMSGW)(LRESULT (AFX_MSG_CALL CWnd:?)(WPARAM, LPARAM))&memberFxn }
5. DC
6.数据库
ADO接口 _ConnectionPtr
class CMainFrame : public CFrameWnd
{
CCoolBar m_wndMyBar1;///我们将它作为左边的窗口
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CChildView m_wndView;
CReBar m_wndReBar;
CTreeCtrl m_wndTree;
CEdit m_wndEdit;
}
- comm
opencom()
{
CreateFile
GetCommState
SetCommState
SetCommTimeouts
}
send()
{
WriteFile
}
readcom()
{
ReadFile
}