如何从“CEdit"框中获取通知? [英] How do I get notification from a `CEdit` box?

查看:31
本文介绍了如何从“CEdit"框中获取通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CEdit 框,用户可以在其中输入相关信息.一旦他/她开始在框中书写,我就需要一个通知,以便我可以调用 doSomething() 来执行其他一些任务.Windows 是否提供回调,如果提供,我该如何使用它?

I have a CEdit box where a user can enter relevant information. As soon as he\she starts writing in the box, I need a notification so that I can call doSomething() to perform some other task. Does Windows provide a callback, and if so, how do I use it?

推荐答案

对于 MFC,没有回调本身,而是通过为适当的事件实现处理程序来实现.您需要处理以下两个事件之一:WM_CHAREN_CHANGE

With MFC there's no callback as such, rather you do this by implementing a handler for the appropriate event. You need to handle one of two events: WM_CHAR or EN_CHANGE

处理对话框的EN_CHANGE,例如在对话框的其他地方实时复制输入的文本.您需要首先在对话框的消息映射中添加一个条目,然后覆盖相应的处理程序:

Handle the dialog's EN_CHANGE for example duplicating in realtime the entered text elsewhere on the dialog. You need to firstly add an entry in the dialog's message map, and secondly override the appropriate handler:

BEGIN_MESSAGE_MAP(CstackmfcDlg, CDialog)
    ON_EN_CHANGE(IDC_EDIT1, &CstackmfcDlg::OnEnChangeEdit1)
END_MESSAGE_MAP()

void CstackmfcDlg::OnEnChangeEdit1()
    {
    CString text;
    m_edit.GetWindowText(text);
    m_label.SetWindowText(text); // update a label control to match typed text
    }

或者,处理编辑框类的 WM_CHAR 例如防止输入某些字符,例如忽略数字输入以外的任何内容.从 CEdit 派生一个类,处理该类(不是对话框)的 WM_CHAR 事件并使您的编辑控件成为该类的实例.

Or, handle the editbox class's WM_CHAR for example preventing input of certain characters, e.g. ignore anything other than a digit for numerical entry. Derive a class from CEdit, handle the WM_CHAR event of that class (not the dialog) and make your edit control an instance of that class.

BEGIN_MESSAGE_MAP(CCtrlEdit, CEdit)
    ON_WM_CHAR()
END_MESSAGE_MAP()

void CCtrlEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // Do nothing if not numeric chars entered, otherwise pass to base CEdit class
    if ((nChar >= '0' && nChar <= '9') || VK_BACK == nChar)
        CEdit::OnChar(nChar, nRepCnt, nFlags);
    }

请注意,您可以使用 VS IDE 通过使用属性栏和消息映射块中的鼠标选择来放置处理程序覆盖的存根.

Note that you can use the VS IDE to put in stubs for the handler overrides by using the Properties bar with the mouse selection in the message map block.

添加了示例代码,并更正了我错误的 WM_CHAR 的解释.

Added example code, and corrected explanation of WM_CHAR which I had wrong.

这篇关于如何从“CEdit"框中获取通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆