11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

……<br />

}<br />

}<br />

if(hwndFocus == hwndEdit)<br />

{<br />

PostMessage(WM_COMBO_RETURN, (WPARAM)IDC_COMBO_DROPDOWN, (LPARAM)0);<br />

return TRUE;<br />

}<br />

First the handle of currently focused window is stored in variable hwndFocus. If it is a valid window<br />

handle, we use m_cbDropDown to get the first child window of IDC_COMBO_DROPDOWN. Then this child<br />

window’s class name is retrieved by calling function ::GetClassName(…). If the class name is “Edit”, we<br />

compare its handle with the focused window handle. Otherwise we need to get the handle of the other child<br />

window before doing the comparison. This will assure that the handle being compared is the handle of the<br />

edit box. If the edit box has the current focus, we post the user defined message WM_COMBO_RETURN, whose<br />

WPARAM parameter is assigned the ID of combo box. Finally a TRUE value is returned to prevent the dialog<br />

box from further processing this message.<br />

Message WM_COMBO_RETURN is processed in class CCCtlDlg. The member function used to trap this<br />

message is CCCtlDlg::OnComboReturn(…). The following code fragment shows how this function is<br />

declared and message mapping is implemented:<br />

Function declaration:<br />

……<br />

……<br />

class CCCtlDlg : public CDialog<br />

{<br />

protected:<br />

};<br />

afx_msg LONG OnComboReturn(UINT, LONG);<br />

DECLARE_MESSAGE_MAP()<br />

Message mapping macros:<br />

……<br />

BEGIN_MESSAGE_MAP(CCCtlDlg, CDialog)<br />

ON_MESSAGE(WM_COMBO_RETURN, OnComboReturn)<br />

END_MESSAGE_MAP()<br />

Function implementation:<br />

LONG CCCtlDlg::OnComboReturn(UINT wParam, LONG lParam)<br />

{<br />

CComboBox *ptrCombo;<br />

CEdit *ptrEdit;<br />

HWND hwndEdit;<br />

CString szStr;<br />

CString szStrLB;<br />

char szClassName[256];<br />

int nSize;<br />

int i;<br />

BOOL bHit;<br />

ptrCombo=(CComboBox *)GetDlgItem(wParam);<br />

ptrEdit=(CEdit *)ptrCombo->GetWindow(GW_CHILD);<br />

hwndEdit=ptrEdit->GetSafeHwnd();<br />

::GetClassName(hwndEdit, szClassName, 256);<br />

if(memcmp(szClassName, "Edit", sizeof("Edit")))<br />

{<br />

ptrEdit=(CEdit *)ptrEdit->GetWindow(GW_HWNDNEXT);<br />

hwndEdit=ptrEdit->GetSafeHwnd();<br />

}<br />

ptrEdit->GetWindowText(szStr);<br />

if(!szStr.IsEmpty())<br />

{<br />

110

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!