用MFC创建一对话框程序,添加资源-ACCELERATOR,如果在对话框中使用这些加速键呢?
是不是在对话框中根本就不能绑定ACCELERATOR资源?只有CFrameWnd才可以加载ACCELERATOR资源?//在ACCELERATOR资源中,已定义好一些快捷键,如ID_ACCELERATOR_XXX对应Ctrl+X//命令消息映射
ON_COMMAND(ID_ACCELERATOR_XXX, OnAcceleratorXxx)//命令处理函数声明
afx_msg void OnAcceleratorXxx();//命令处理函数定义
void CMyAppDlg::OnAcceleratorXxx()
{
AfxMessageBox("用快捷键打开", MB_ICONINFORMATION + MB_OK);
}
是不是在对话框中根本就不能绑定ACCELERATOR资源?只有CFrameWnd才可以加载ACCELERATOR资源?//在ACCELERATOR资源中,已定义好一些快捷键,如ID_ACCELERATOR_XXX对应Ctrl+X//命令消息映射
ON_COMMAND(ID_ACCELERATOR_XXX, OnAcceleratorXxx)//命令处理函数声明
afx_msg void OnAcceleratorXxx();//命令处理函数定义
void CMyAppDlg::OnAcceleratorXxx()
{
AfxMessageBox("用快捷键打开", MB_ICONINFORMATION + MB_OK);
}
//#if !defined(AFX_MGDLG_H__36B4A48A_D935_11D5_95F8_009027225A94__INCLUDED_)
#define AFX_MGDLG_H__36B4A48A_D935_11D5_95F8_009027225A94__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
// CMgDlg dialogclass CMgDlg : public CDialog
{
// Construction
public:
BOOL m_bHotKeyHSuc;
BOOL m_bHotKeyJSuc;
CMgDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CMgDlg)
enum { IDD = IDD_MG_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMgDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CMgDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnClose();
//}}AFX_MSG
afx_msg void OnHotKey(WPARAM wparam,LPARAM lparam);
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MGDLG_H__36B4A48A_D935_11D5_95F8_009027225A94__INCLUDED_).cpp文件// MgDlg.cpp : implementation file
//#include "stdafx.h"
#include "Mg.h"
#include "MgDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#define ID_HOTKEY_J 300
#define ID_HOTKEY_H 301
/////////////////////////////////////////////////////////////////////////////
// CMgDlg dialogCMgDlg::CMgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMgDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bHotKeyHSuc = FALSE;
m_bHotKeyJSuc = FALSE;
}void CMgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMgDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMgDlg, CDialog)
//{{AFX_MSG_MAP(CMgDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_HOTKEY,OnHotKey)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMgDlg message handlersBOOL CMgDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_bHotKeyHSuc = RegisterHotKey(GetSafeHwnd(),ID_HOTKEY_H,NULL,'H');
m_bHotKeyJSuc = RegisterHotKey(GetSafeHwnd(),ID_HOTKEY_J,MOD_CONTROL,'J');
if(!m_bHotKeyHSuc)
AfxMessageBox("热键注册'H'失败!!!");
if(!m_bHotKeyJSuc)
AfxMessageBox("热键注册'J'失败!!!");
return TRUE;
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CMgDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMgDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CMgDlg::OnHotKey(WPARAM wparam,LPARAM lparam)
{
switch(wparam)
{
case ID_HOTKEY_H: AfxMessageBox("H键!!!");
break;
case ID_HOTKEY_J: AfxMessageBox("J键!!!");
break;
}
}void CMgDlg::OnClose()
{
if(m_bHotKeyHSuc)
UnregisterHotKey(GetSafeHwnd(),ID_HOTKEY_H);
if(m_bHotKeyJSuc)
UnregisterHotKey(GetSafeHwnd(),ID_HOTKEY_J);
CDialog::OnClose();
}
http://support.microsoft.com/?kbid=222829