搜狐首页-新闻-体育-娱乐-财经-IT-汽车-房产-家居-女人-TV-ChinaRen-邮件-博客-BBS-搜狗 

数码天下 > 网狐学园-全中国人的电脑学习中心 > 程序开发-网狐学园 > 程序设计其他
教你VC程序开发中实现定制回车键行为的方法
时间:2006年01月13日09:25 我来说两句(0)  

 
来源:天极网


  三、程序代码

/////////////////////////////////////////
#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);
}

  四、小结

  综上所述,MFC中为对话框添加加速键功能的方法就是:加载加速键和重载PreTranslateMessage()函数。也就是说,如果开发人员决定在对话框中使用加速键,不用去操心OnGetDefID()函数的处理,而是要重点实现加速键对应的WM_COMMAND消息响应处理函数。

热门新闻排行
01 广州Vista盗版江湖调查 全部破解版本大曝光
02 Windows Vista不兼容的应用程序,还有谁?
03 中国海贼版Vista入侵日本 "番茄花园"打头阵
04 挑战视觉享受极限 五款流行多媒体播放器比拼
05 Vista SP1发布计划已定 Windows XP SP3不再来
热门教程排行
01 安装系统不求人 就算没有光驱和软驱也能行
02 让电脑运转如飞 Windows Vista九则加速技巧
03 逐一辩驳:破解Windows Vista的四个性能谣言
04 专家经验谈:Excel工作表的几点设计策略
05 整容大法!Photoshop简单教程之美女去斑篇
蜘蛛侠主题Mod美图赏

蜘蛛侠主题Mod美图赏
两款可爱的iPod音箱

两款可爱的iPod音箱
小心别丢了 全球最袖珍5款科技玩意

全球最袖珍5款科技玩意
最强的概念车设计:超级巴士

概念车设计:超级巴士
LLADRó瓷器诠释人性情感

LLADRó瓷器诠释人性
德国家具设计精选作品

德国家具设计精选
最具创意的多功能床(图)

最具创意的多功能床
2006离生活最近的创意

2006离生活最近的创意
欢迎访问:软件频道 下载频道 网狐学院 数字艺术 考试 病毒与安全 创意赏析

[上一页][1][2]

(责任编辑:luwei)



共找到 860 个相关新闻.


我来说两句 全部跟贴(0条) 精华区(0条) 辩论区(0条)

用户:  匿名发表:  隐藏地址:


设为辩论话题      


精彩图片新闻


激情四溢沙滩宝贝


数码公社专栏

相关链接





搜狐短信 小灵通 性感丽人 言语传情
三星图铃专区
[周杰伦] 千里之外
[誓 言] 求佛
[王力宏] 大城小爱
[王心凌] 花的嫁纱
精品专题推荐
短信企业通秀百变功能
浪漫情怀一起漫步音乐
同城约会今夜告别寂寞
敢来挑战你的球技吗?
 精彩生活 

星座运势 每日财运
花边新闻 魔鬼辞典
情感测试 生活笑话


今日运程如何?财运、事业运、桃花运,给你详细道来!!!





菊花台
迷迭香
青青河边草
丁香花
原来你也在这里
爱如空气
不要再来伤害我

公社热图


·北美惊现若干神秘UFO/图
·范冰冰真空写真露点[图]
·传说中国内第一美女车模


频道精彩推荐

·关注2006年铁路春运
·大陆赠送台湾大熊猫
·2006年德国足球世界杯
·李宇春发单曲张朝阳捧场
·MOP收购DONEWS
·2005年经济回顾
·东风标致206正式下线
·春节晚会
·2006达喀尔拉力赛
·湖南人禽流感病患者病变






不怕不怕
吉祥三宝
桃花朵朵开
千里之外
大城小爱
梁祝
迷糊娃娃可爱粉红卡通
四季美眉给你最想要的

news
搜狐分类 ·搜狐招商


创意就是这么简单
女星豪乳尺码大曝光
·现场抓拍明星着透视装
·芙蓉二代横空出世
·央视女主持辞职拍艳照
·徐若瑄15岁全裸写真曝光
·拍摄:娱乐圈性交易过程
·抓拍模特海滩宽衣解带
·外国女生流行拍的艺术照
·台湾十大波霸照片大比拼
·网络美女全裸人体摄影
·范冰冰终于还是脱了

推荐企业
2006年世界电信展 诺基亚 瑞星 联想 摩托罗拉 三星 清华紫光 金山 索尼爱立信 索尼
给编辑写信



设置首页 - 搜狗输入法 - 支付中心 - 搜狐招聘 - 广告服务 - 客服中心 - 联系方式 - 保护隐私权 - About SOHU - 公司介绍 - 全部新闻 - 全部博文
Copyright © 2018 Sohu.com Inc. All Rights Reserved. 搜狐公司 版权所有
搜狐不良信息举报邮箱:jubao@contact.sohu.com