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 />

{<br />

protected:<br />

};<br />

MCCharEdit m_editChar;<br />

MCNumEdit m_editNum;<br />

In the dialog box’s initialization stage, we need to implement subclass and change the default behavior<br />

of the edit boxes. Remember in the previous chapter, function CWnd::SubclassDlgItem(…) is used to<br />

implement subclass for an item contained in a dialog box. Although the edit box within a combo box is a<br />

indirect child window of the dialog box, it is not created from dialog template. So here we must call<br />

function CWnd::SubclassWindow(…) to implement subclass. The following is the format of this function:<br />

BOOL CWnd::SubclassWindow(HWND hWnd);<br />

Here, parameter hWnd is the handle of the window whose behavior is to be customized. From sample<br />

5.9\CCtl, we know how to obtain the handle of the edit box that belongs to a combo box. The following is<br />

the procedure of implementing subclass for IDC_COMBO_DROPDOWN combo box:<br />

BOOL CCCtlDlg::OnInitDialog()<br />

{<br />

CEdit *ptrEdit;<br />

HWND hwndEdit;<br />

char szClassName[256];<br />

……<br />

……<br />

}<br />

CDialog::OnInitDialog();<br />

ptrEdit=(CEdit *)m_cbDropDown.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 />

m_editChar.SubclassWindow(hwndEdit);<br />

With the above implementation, the combo box is able to filter out the characters we do not want.<br />

5.11 Owner Draw List Box and Combo Box<br />

Like menu, list box and combo box do not have to bear plain text interface all the time. Sometimes we<br />

can customize them to display images. In the previous samples, when implementing a list box or a combo<br />

box, we always select “No” for the “Owner draw” style. Actually, the “Owner draw” style can be set to<br />

other two selections: “Fixed” and “Variable”. For a “fixed” type owner-draw list box or combo box, each<br />

item contained in the list box must have a same height. For a “variable” type of owner draw list box or<br />

combo box, this height can be variable. Like the menu, the owner-draw list box or combo box are drawn by<br />

their owner. The owner will receive message WM_MEASUREITEM and WM_DRAWITEM when the list box or the<br />

combo box needs to be updated. For “fixed” type owner draw list box or combo box, WM_MEASUREITEM is<br />

sent when it is first created and the returned size will be used for all items. For “variable” type owner-draw<br />

list box or combo box, this message is sent for each item separately. Message WM_DRAWITEM will be sent<br />

when the interface of list box or combo box needs to be updated.<br />

113

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

Saved successfully!

Ooh no, something went wrong!