扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:刘涛 来源:天极开发 2007年10月16日
关键字:
///////////////////////////////////////// #include "stdafx.h" // Generic dialog-that-uses-accelerators. class CDlgWithAccelerators : public CDialog { public: CDlgWithAccelerators(UINT nIDTemplate, CWnd* pParentWnd = NULL); CDlgWithAccelerators(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL); ~CDlgWithAccelerators(); protected: HACCEL m_hAccel; // accelerator table // MFC overrides virtual BOOL OnInitDialog(); virtual BOOL PreTranslateMessage(MSG* pMsg); DECLARE_MESSAGE_MAP() }; /////////////////////////////////////////// // CDlgWithAccelerators is a general-purpose class that adds accelerators to CDialog. #include "stdafx.h" #include "dlgaccel.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BEGIN_MESSAGE_MAP(CDlgWithAccelerators,CDialog) END_MESSAGE_MAP() CDlgWithAccelerators::CDlgWithAccelerators(LPCTSTR lpszTemplateName, CWnd* pParentWnd) : CDialog(lpszTemplateName, pParentWnd) {} CDlgWithAccelerators::CDlgWithAccelerators(UINT nIDTemplate, CWnd* pParentWnd) : CDialog(nIDTemplate, pParentWnd) {} CDlgWithAccelerators::~CDlgWithAccelerators() {} /////////////////////////// Pre-translate message: translate keystrokes using acclerator table. BOOL CDlgWithAccelerators::PreTranslateMessage(MSG* pMsg) { if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST) { HACCEL hAccel = m_hAccel; if (hAccel && ::TranslateAccelerator(m_hWnd, hAccel, pMsg)) return TRUE; } return CDialog::PreTranslateMessage(pMsg); } //////////////////// Initialize dialog: load accelerators BOOL CDlgWithAccelerators::OnInitDialog() { BOOL bRet = CDialog::OnInitDialog(); // Load dialog's accelerators m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(), m_lpszTemplateName); // use same resource name as dialog return bRet; } ///////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "resource.h" #include "dlgaccel.h" #include "TraceWin.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif /////////////MFC app class CMyApp : public CWinApp { public: CMyApp(); ~CMyApp(); virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP() }; CMyApp theApp; // THE one-and-only app //////////// frame window class CMainFrame : public CFrameWnd { protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); public: CMainFrame(); ~CMainFrame(); }; //////////////////// Typical dialog class CMyDlg : public CDlgWithAccelerators { public: CMyDlg(CWnd* pParent = NULL); // standard constructor protected: HICON m_hIcon; void NextInTabOrder(); // MFC overrides virtual BOOL OnInitDialog(); afx_msg void OnMyEnter(); afx_msg LRESULT OnGetDefID(WPARAM wp, LPARAM lp); DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CMyApp, CWinApp) END_MESSAGE_MAP() CMyApp::CMyApp() { // nothing to do } CMyApp::~CMyApp() { // nothing to do } //////////////////// InitInstance: create dialog as child BOOL CMyApp::InitInstance() { // create frame window and load it CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPED, NULL, NULL); CMyDlg dlg(pFrame); // create dialog and run it int nResponse = dlg.DoModal(); if (nResponse == IDOK) {} else if (nResponse == IDCANCEL) {} return FALSE; // quit } CMainFrame::CMainFrame() { // nothing to do } CMainFrame::~CMainFrame() { // nothing to do } ///////////// Pre-create window: set WS_EX_TOOLWINDOW style to hide dialog from task bar BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if (CFrameWnd::PreCreateWindow(cs)) { cs.dwExStyle |= WS_EX_TOOLWINDOW; return TRUE; } return FALSE; } BEGIN_MESSAGE_MAP(CMyDlg, CDlgWithAccelerators) ON_COMMAND(ID_MY_ENTER, OnMyEnter) // The following is NOT needed since I am using accelerators to map // ENTER to ID_MY_ENTER. But if all you want to do is ignore the Enter key, // you can handle DM_GETDEFID as below. // ON_MESSAGE(DM_GETDEFID, OnGetDefID) // not used END_MESSAGE_MAP() CMyDlg::CMyDlg(CWnd* pParent) : CDlgWithAccelerators(IDD_MYDIALOG, pParent) {} //////////////////// Initialize dialog: BOOL CMyDlg::OnInitDialog() { CDlgWithAccelerators::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); ASSERT(m_hIcon); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // use same resource name as dialog to load dialog's accelerators m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(),m_lpszTemplateName); ASSERT(m_hAccel); return TRUE; // return TRUE unless you set the focus to a control } //////////////////// This is called to handle ID_MY_ENTER--ie, Enter key. void CMyDlg::OnMyEnter() { TRACE(_T("CMyDlg::OnMyEnter\n")); NextInTabOrder(); // move to next control } //////////////////// Helper function to move focus to the next control. void CMyDlg::NextInTabOrder() { CWnd* pWndNext = GetNextDlgTabItem(GetFocus()); if (pWndNext) { pWndNext->SetFocus(); } } ////////////////// // This function is not used, since its message map entry is commented out. // If all you want to do is ignore the Enter key (not map it to a command), // then all you have to do is return zero here. Note that you MUST return // the special code DC_HASDEFID in the high-order word!! LRESULT CMyDlg::OnGetDefID(WPARAM wp, LPARAM lp) { TRACE(_T("CMyDlg::OnGetDefID\n")); return MAKELONG(0,DC_HASDEFID); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者