【问题标题】:MFC CEdit lose focus handlerMFC CEdit 失去焦点处理程序
【发布时间】:2012-07-06 10:57:10
【问题描述】:

我正在使用文档/视图架构创建一个 MFC 程序。在视图中,我调用了一个扩展 CEdit 的单元格类来绘制一个文本框。但是,当我尝试为该文本框捕捉失去焦点的消息时,它没有任何反应。我试图覆盖 PreTranslateMessage,但没有奏效。

这是 CGridView.cpp 类中的代码:

void CGridView::OnInsertText()
{
    CWnd* pParentWnd = this;
    CellText* pEdit = new CellText(&grid, pParentWnd);

    Invalidate();   
    UpdateWindow();
}

CellText.cpp:

CellText::CellText(Grid *pgrid, CWnd* pParentWnd)
{

    int *pcoordinates = pgrid->GetSelectedCellCoodrinates();
    cedit.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(*pcoordinates+10, *(pcoordinates+1)+10, *(pcoordinates+2)-10, *(pcoordinates+3)-10), pParentWnd, 1);

    cell = pgrid->GetSelectedCell();
    pgrid->SetCellType(cell, "text");

    grid = pgrid;
}


BEGIN_MESSAGE_MAP(CellText, CEdit)
    ON_WM_KILLFOCUS()
    ON_WM_KEYDOWN()
END_MESSAGE_MAP()



// CellText message handlers

void CellText::OnKillFocus(CWnd* pNewWnd)
{
    CEdit::OnKillFocus(pNewWnd);

    CString str;
    GetWindowTextW(str);
    grid->SetCellText(cell, str);

    cedit.DestroyWindow(); 
}

BOOL CellText::PreTranslateMessage(MSG* pMsg) 
{
    if(pMsg->message==WM_KEYDOWN)
    {
        if(pMsg->wParam==VK_UP)
        {

        }
    }   

    return CWnd::PreTranslateMessage(pMsg);
}

当我调试时,根本不调用 onkillfocus 和 pretranslatemessage。

谢谢,

【问题讨论】:

    标签: c++ mfc cedit


    【解决方案1】:

    您必须在父窗口中处理EN_KILLFOCUS 通知代码。您不必从 CEdit 派生来执行此操作。

    EN_KILLFOCUS notification code

    更新:

    编辑控件的父窗口收到此通知码 通过 WM_COMMAND 消息。

    wParam: LOWORD 包含编辑控件的标识符。这 HIWORD 指定通知代码。

    lParam: - 编辑控件的句柄。

    【讨论】:

    • 如果我创建了多个文本框,句柄中的任何代码将应用于各自的文本框,还是应用于所有文本框(在这种情况下,我想删除该框)。谢谢,
    • @mgalal:您可以获得失去焦点的唯一编辑框的标识符。我的答案已更新。
    • 非常感谢,真的帮了我大忙。
    • 父类也可以将ON_EN_KILLFOCUS(id, memberFunc)添加到它的MESSAGE_MAP,如果你有特定的编辑控件想要处理。
    猜你喜欢
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多