Question

I am creating a MFC program using the document/view architecture. In the view I call on a cell class that extends CEdit to draw a text box. That works fine, however, when I try to catch a lose focus message for that text box nothing happens. I tried to overwrite PreTranslateMessage but that didn't work.

Here's the code in the CGridView.cpp class:

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

    Invalidate();   
    UpdateWindow();
}

the 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);
}

When I debug, the onkillfocus and pretranslatemessage aren't called at all.

Thanks,

Was it helpful?

Solution

You have to handle the EN_KILLFOCUS notification code in the parent window. You shouldn't have to derive from CEdit to do that.

EN_KILLFOCUS notification code

UPDATE:

The parent window of the edit control receives this notification code through a WM_COMMAND message.

wParam: The LOWORD contains the identifier of the edit control. The HIWORD specifies the notification code.

lParam: - Handle to the edit control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top