如何捕捉控件中的KEYDOWN事件

meteorkin 2003-12-04 06:18:13
必须我如何知道我在某个textbox中按下了del键
然后什么事也不做
...全文
115 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluestone 2003-12-04
  • 打赏
  • 举报
回复
如果不明白,继续说说PreTranslateMessage中的写法,例如:

BOOL 控件所在的对话框::PreTranslateMessage(MSG* pMsg)
{
CWnd * hwnd = GetDlgItem (控件ID);
if(pMsg->message == WM_KEYDOWN && pMsg->hwnd == hwnd)
{
switch(pMsg->wParam)
{
case VK_DELETE:
你的东东:);
return TRUE;
}
}

return CDialog::PreTranslateMessage(pMsg);
}
bluestone 2003-12-04
  • 打赏
  • 举报
回复
窗口的键盘消息在TranslateMessage中被处理了,所以在你的对话框中得不到这个消息,你可以这么做,覆盖对话框的PreTranslateMessage,他是在窗口处理键盘消息之前调用的,在它里面你可以接收到键盘消息,试试吧~~:)

函数原型:

virtual BOOL PreTranslateMessage( MSG* pMsg );
pubutan 2003-12-04
  • 打赏
  • 举报
回复
if(pMsg->message==WM_KEYDOWN)
{
UINT ID=GetFocus()->GetDlgCtrlID();
if(ID==IDC_MESSAGE_LIST)
{

}
}
qwexing 2003-12-04
  • 打赏
  • 举报
回复
1。Add a member variable to your CWinApp-derived class to hold the handle to the dialog box for which you want to trap the messages. You need to know the handle because ProcessMessageFilter is receiving messages for the entire application, while you only want a small selection of those messages:

SCRIBBLE.H

<Inside CScribbleApp class>

public:
HWND m_hwndDialog;

SCRIBBLE.CPP

<Inside CScribbleApp::InitInstance>

m_hwndDialog = NULL;
NOTE: Because this message filter applies to the entire application, there may be a performance hit when you use this method.


2。Override CWinApp::ProcessMessageFilter in your CWinApp-derived class. CWinApp::ProcessMessageFilter is a virtual function of CWinApp, so all you need to do is put a declaration in the header file and an implementation in the source file.

SCRIBBLE.H

public:
virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);

SCRIBBLE.CPP

BOOL CScribbleApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
// Check to make sure CPenWidthsDlg is up
if (m_hwndDialog != NULL)
{
if ((lpMsg->hwnd == m_hwndDialog) ||
::IsChild(m_hwndDialog, lpMsg->hwnd))
// Use ::IsChild to get messages that may be going
// to the dialog's controls. In the case of
// WM_KEYDOWN this is required.
{
if (lpMsg->message == WM_KEYDOWN)
TRACE("Got WM_KEYDOWN\n");
}
}
// Default processing of the message.
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
For more information on overriding ProcessMessageFilter, please see the MFC online Help.


3。Add initialization code for the dialog handle member variable in our OnInitDialog for the desired dialog box. You also have to add exit code to reset the member variable after the dialog box closes:

PENDLG.H

protected:
virtual BOOL OnInitDialog();
afx_msg void OnDestroy();

PENDLG.CPP

<Add to Message Map>
ON_WM_DESTROY()

BOOL CPenWidthsDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Use AfxGetApp() to get pointer to CWinApp-derived
// object; then cast the pointer to our type and assign
((CScribbleApp *)AfxGetApp())->m_hwndDialog = m_hWnd;

return TRUE; // Return TRUE
// unless you set the focus to a control.
}

void CPenWidthsDlg::OnDestroy()
{
CDialog::OnDestroy();

// Use AfxGetApp() to get pointer to CWinApp-derived
// object; then cast the pointer to our type and assign
((CScribbleApp *)AfxGetApp())->m_hwndDialog = NULL;

}
sorryIdonotknow 2003-12-04
  • 打赏
  • 举报
回复
up
ddszhan 2003-12-04
  • 打赏
  • 举报
回复
自己做一个继承自标准CEdit类的类,在其中处理KEYDOWN事件
然后可以用控件子类化的方法
m_myEdit.SubclassDlgItem(IDC_EDIT1,this);
来使用,当然也可以直接使用
CMyEdit m_myEdit;
m_myEdit.Create(......);

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧