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

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

问题描述

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

I have a CEdit box where a user can enter relevant information. As soon as heshe 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天全站免登陆