按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
// overrides for implementation
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual int Run();
virtual BOOL OnIdle(LONG lCount);
。。。
};
359
…………………………………………………………Page 422……………………………………………………………
第篇 湷觥 FC 程式設計
几乎可以说CWinApp 用来取代WinMain 在SDK 程序中的地位。这并不是说MFC 程序
没有WinMain (稍后我会解释),而是说传统上SDK 程序的WinMain 所完成的工作现
在由CWinApp 的三个函数完成:
virtual BOOL InitApplication();
virtual BOOL InitInstance();
virtual int Run();
WinMain 只是扮演役使它们的角色。
会不会觉得CWinApp 的成员变量中少了点什么东西?是不是应该有个成员变量记录主
窗口的handle (或是主窗口对应之C++ 对象)?的确,在MFC 2。5 中的确有
m_pMainWnd 这么个成员变量(以下节录自MFC 2。5 的AFXWIN。H ):
class CWinApp : public CCmdTarget
{
// Attributes
// Startup args (do not change)
HINSTANCE m_hInstance;
HINSTANCE m_hPrevInstance;
LPSTR m_lpCmdLine;
int m_nCmdShow;
// Running args (can be changed in InitInstance)
CWnd* m_pMainWnd; // main window (optional)
CWnd* m_pActiveWnd; // active main window (may not be m_pMainWnd)
const char* m_pszAppName; // human readable name
public: // set in constructor to override default
const char* m_pszExeName; // executable name (no spaces)
const char* m_pszHelpFilePath; // default based on module path
const char* m_pszProfileName; // default based on app name
public:
// hooks for your initialization code
virtual BOOL InitApplication();
virtual BOOL InitInstance();
// running and idle processing
virtual int Run();
virtual BOOL OnIdle(LONG lCount);
360
…………………………………………………………Page 423……………………………………………………………
第6章 MFC 程式的生死因果
// exiting
virtual int ExitInstance();
。。。
};
但从MFC 4。x 开始,m_pMainWnd 已经被移往CWinThread 中了(它是CWinApp 的父
类别)。以下内容节录自MFC 4。x 的AFXWIN。H :
class CWinThread : public CCmdTarget
{
// Attributes
CWnd* m_pMainWnd; // main window (usually same AfxGetApp()…》m_pMainWnd)
CWnd* m_pActiveWnd; // active main window (may not be m_pMainWnd)
// only valid while running
HANDLE m_hThread; // this thread's HANDLE
DWORD m_nThreadID; // this thread's ID
int GetThreadPriority();
BOOL SetThreadPriority(int nPriority);
// Operations
DWORD SuspendThread();
DWORD ResumeThread();
// Overridables
// thread initialization
virtual BOOL InitInstance();
// running and idle processing
virtual int Run();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual BOOL PumpMessage(); // low level message pump
virtual BOOL OnIdle(LONG lCount); // return TRUE if more idle processing
public:
// valid after construction
AFX_THREADPROC m_pfnThreadProc;
。。。
};
熟悉Win32 的朋友,看到CWinThread 类别之中的SuspendThread 和ResumeThread 成
员函数,可能会发出会心微笑。
361
…………………………………………………………Page 424……………………………………………………………
第篇 湷觥 FC 程式設計
CFrameWnd -取代 WndProc 的地位
CFrameWnd 主要用来掌握一个窗口,几乎你可以说它是用来取代SDK 程序中的窗口函
式的地位。传统的SDK 窗口函数写法是:
long FAR PASCAL WndProc(HWND hWnd; UNIT msg; WORD wParam; LONG lParam)
{
switch(msg) {
case WM_MAND :
switch(wParam) {
case IDM_ABOUT :
OnAbout(hWnd; wParam; lParam);
break;
}
break;
case WM_PAINT :
OnPaint(hWnd; wParam; lParam);
break;
default :
DefWindowProc(hWnd; msg; wParam; lParam);
}
}
MFC 程序有新的作法,我们在Hello 程序中也为CMyFrameWnd 准备了两个消息处理
例程,声明如下:
class CMyFrameWnd : public CFrameWnd
{
public:
CMyFrameWnd();
afx_msg void OnPaint();
afx_msg void OnAbout();
DECLARE_MESSAGE_MAP ()
};
OnPaint 处理什么消息?OnAbout 又是处理什么消息?我想你很容易猜到,前者处理
WM_PAINT ,后者处理WM_MAND 的IDM_ABOUT 。这看起来十分俐落,但让人搞
不懂来龙去