我正在使用文档/视图架构创建一个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焦点和预翻译消息根本不会被调用。
谢谢,
发布于 2012-07-06 10:14:54
您必须在父窗口中处理EN_KILLFOCUS通知代码。您不应该为了做到这一点而从CEdit派生。
EN_KILLFOCUS notification code
更新:
编辑控件的父窗口
通过WM_COMMAND消息接收此通知代码。
LOWORD wParam:包含编辑控件的标识符。HIWORD指定通知代码。
lParam: -编辑控件的句柄。
https://stackoverflow.com/questions/11354475
复制相似问题