按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
速键表装入内存。此后我们可以用对函数PreTranslateMessage的调
用保证加速键表在适当的时候被调用。例如:
BOOL DWindowingClass::PreTranslateMessage(MSG *pMsg)
{
if(d_hAccel==NULL)
return FALSE;//我们不处理
return ::TranslateAccelertor(m_hWnd;d_hAccel;pMsg);
}
作为对本章中键盘消息响应的结束,我们给出一个有趣的例程,读者
不妨运行之,看看是否很有意思。
// echofill。h : main header file for the ECHOFILL application
//
#ifndef __AFXWIN_H__
#error include 'stdafx。h' before including this file for PCH
#endif
#include 〃resource。h〃 // main symbols
/////////////////////////////////////////////////////////////////////////////
// DApp:
// See echofill。cpp for the implementation of this class
//
class DApp : public CWinApp
{
public:
…………………………………………………………Page 257……………………………………………………………
DApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(DApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(DApp)
afx_msg void OnAppAbout();
// NOTE the ClassWizard will add and remove member functions here。
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file。
// Used by ECHOFILL。RC
//
#define IDR_MAINFRAME 128
#define IDD_ABOUTBOX 100
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
…………………………………………………………Page 258……………………………………………………………
#define _APS_3D_CONTROLS 1
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#define _APS_NEXT_MAND_VALUE 32771
#endif
#endif
// echofill。cpp : Defines the class behaviors for the application。
//
#include 〃stdafx。h〃
#include 〃echofill。h〃
#include 〃mainfrm。h〃
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE'' = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DApp
BEGIN_MESSAGE_MAP(DApp; CWinApp)
//{{AFX_MSG_MAP(DApp)
ON_MAND(ID_APP_ABOUT; OnAppAbout)
// NOTE the ClassWizard will add and remove mapping macros here。
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document mands
ON_MAND(ID_FILE_NEW; CWinApp::OnFileNew)
…………………………………………………………Page 259……………………………………………………………
ON_MAND(ID_FILE_OPEN; CWinApp::OnFileOpen)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DApp construction
DApp::DApp()
{
// TODO: add construction code here;
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only DApp object
DApp theApp;
/////////////////////////////////////////////////////////////////////////////
// DApp initialization
BOOL DApp::InitInstance()
{
// Step 1: Allocate C++ window object。
DMainFrame * pFrame;
pFrame = new DMainFrame();
// Step 2: Initialize window object。
pFrame…》LoadFrame(IDR_MAINFRAME);
// Make window visible
pFrame…》ShowWindow(m_nCmdShow);
// Assign frame as application's main window
m_pMainWnd = pFrame;
return TRUE;
…………………………………………………………Page 260……………………………………………………………
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// Implementation
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
…………………………………………………………Page 261……………………………………………………………
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg; CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App mand to run the dialog
void DApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg。DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// DApp mands
// mainfrm。cpp : implementation of the DMainFrame class
//
#include 〃stdafx。h〃
#include 〃echofill。h〃
#include 〃mainfrm。h〃
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE'' = __FILE__;
#endif
…………………………………………………………Page 262……………………………………………………………
/////////////////////////////////////////////////////////////////////////////
// DMainFrame
IMPLEMENT_DYNCREATE(DMainFrame; CFrameWnd)
BEGIN_MESSAGE_MAP(DMainFrame; CFrameWnd)
//{{AFX_MSG_MAP(DMainFrame)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DMainFrame construction/destruction
DMainFrame::DMainFrame()
{
ASSERT (MAX_COLORS == 8); // Table fill assumes this many colors。
crBackground'0' = g_crGray;
crBackground'1' = g_crYellow;
crBackground'2' = g_crRed;
crBackground'3' = g_crBlue;
crBackground'4' = g_crGreen;
crBackground'5' = g_crWhite;
crBackground'6' = g_crBlack;
crBackground'7' = g_crCyan;
iNextColor = 0;
lpszClassName = _T(〃Afx:No:Redraw:Bits〃);
}
DMainFrame::~DMainFrame()
{
…………………………………………………………Page 263……………………………………………………………
}
//…………………………………………………………………………………………………………………………………………………………………………………………………………
// PreCreateWindow …Called before Windows API window is created。
BOOL DMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// Register our own window class to avoid the default
// class style of CS_HREDRAW | CS_VREDRAW;
HICON hIcon = ::LoadIcon(AfxGetResourceHandle();
MAKEINTRESOURCE(IDR_MAINFRAME));
HCURSOR hCursor = ::LoadCursor((HINSTANCE)0; IDC_ARROW);
LPCTSTR lpszClassName =
AfxRegisterWndClass(0; // Class style。
hCursor; // Mouse cursor。
(HBRUSH)COLOR_WINDOW+1; // Background color。
hIcon); // Icon。
// Pass class name on to creation code。
cs。lpszClass = lpszClassName;
return CFrameWnd::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// DMainFrame diagnostics
#ifdef _DEBUG
void DMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
…………………………………………………………Page 264……………………………………………………………
void DMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG