11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5. Common Controls<br />

(CBN_CLOSEUP for IDC_COMBO_DROPDOWN and IDC_COMBO_DROPLIST, CBN_SELCHANGE for IDC_COMBO_SIMPLE).<br />

3) Click button “Add function” and confirm the member function name.<br />

These functions will be called when the user makes a new selection from the list box of a combo box.<br />

We can retrieve the index of the current selection of a combo box by calling function CComboBox::<br />

GetCurSel(), and further retrieve the text of that item by calling function CComboBox::GetLBText(…). At<br />

last we can call function CWnd::SetWindowText(…) to display the updated content of the selected item in<br />

one of the static text controls. The implementations of three message handlers are almost the same. The<br />

following is one of them:<br />

void CCCtlDlg::OnCloseupComboDropdown()<br />

{<br />

CString szStr;<br />

int nSel;<br />

}<br />

nSel=m_cbDropDown.GetCurSel();<br />

if(nSel != CB_ERR)<br />

{<br />

m_cbDropDown.GetLBText(nSel, szStr);<br />

}<br />

GetDlgItem(IDC_STATIC_DROPDOWN)->SetWindowText(szStr);<br />

First index of the current selection is retrieved and stored in variable nSel. Then we check if the<br />

returned value is CB_ERR. This is possible if there is nothing being currently selected. If the returned value<br />

is a valid index, we call function CComboBox::GetLBText(…) to retrieve the text string and store it in<br />

CString type variable szStr. Finally function CWnd::GetDlgItem(…) is called to obtain the pointer to the<br />

static text window, and CWnd::SetWindowText(…) is called to update its contents.<br />

5.9 Trapping RETURN key strokes for the Combo Box<br />

Problem & Workaround<br />

One feature we may want to add to the combo boxes is to let the user dynamically add new items<br />

through using their edit boxes. We can let the user input a string into the edit box of a drop-down or simple<br />

combo box, then hit the RETURN key to add the input to list item. However, in a dialog box, the RETURN<br />

key (also the ESC key) is used to exit the application by default. Even if we add message handler for<br />

combo box to trap RETURN keystrokes, it still can not receive this message because after the message<br />

reaches the dialog box, the application will exit. The message has no chance to be further routed to the<br />

child windows of a dialog box.<br />

If we want to process RETURN keystroke events, we need to intercept the message before it is<br />

processed by the dialog box. In <strong>MFC</strong>, there is a function CWnd::PreTranslateMessage(…) that can be<br />

overridden for this purpose. This function will be called just before a message is about to be processed by<br />

the destination window. Since CDialog is derived from CWnd, we can trap any message sent to the dialog<br />

box if we override the above function. This function has the following format:<br />

BOOL CWnd::PreTranslateMessage(MSG *pMsg);<br />

Its only parameter is a pointer to MSG type object:<br />

typedef struct tagMSG {<br />

HWND hwnd;<br />

UINT message;<br />

WPARAM wParam;<br />

LPARAM lParam;<br />

DWORD time;<br />

POINT pt;<br />

} MSG;<br />

// msg<br />

107

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

Saved successfully!

Ooh no, something went wrong!