2012-07-06 50 views
1

我創建使用文檔/視圖結構的MFC程序。在視圖中,我調用一個擴展CEdit的單元類來繪製一個文本框。這工作正常,但是,當我試圖抓住一賠焦點消息的文本框中沒有任何反應。我試圖覆蓋PreTranslateMessage,但沒有奏效。MFC的CEdit失去焦點處理程序

這裏是在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不叫的。

感謝,

回答

2

你必須處理在父窗口的EN_KILLFOCUS通知代碼。你不應該從CEdit派生來做到這一點。

EN_KILLFOCUS notification code

UPDATE:

編輯控件的父窗口通過WM_COMMAND消息接收到該通知代碼 。

wParam: LOWORD包含編輯控件的標識符。該 HIWORD指定通知代碼。

的lParam: - 處理編輯控件。

+0

如果我創建了多個文本框,句柄中的任何代碼都將應用於相應的文本框或全部文本框(在這種情況下,我想刪除該框)。謝謝, – mgalal 2012-07-06 05:49:08

+0

@mgalal:您可以獲取失去焦點的獨特的編輯框內輸入一個標識符。我的答案已更新。 – 2012-07-06 13:20:29

+0

非常感謝,真的幫了我。 – mgalal 2012-07-06 15:17:54