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

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_HOR))->SetRange(50, 0);<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_HOR))->SetBase(10);<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_HOR))->SetPos(25);<br />

}<br />

return TRUE;<br />

5.3 Displaying Text Strings in the Buddy Window<br />

Sometimes we want the buddy to display text strings rather than numerical numbers. For example, we<br />

may prefer the text displayed in the buddy window to be “One”, “Two”, “Three”… rather than “1”, “2”,<br />

“3”…. To customize this style, we could not use “Set buddy integer” style anymore. Instead, we need to<br />

write our own message handlers and set the buddy control’s text by ourselves.<br />

When the position of a spin has changed, the parent window of the spin control will receive a<br />

UDN_DELTAPOS message. From this message, we can get the current position of the spin control, along with<br />

the proposed change to the current position. Based on this information, we can decide what we should<br />

display in the buddy control window.<br />

Sample 5.3\CCtl demonstrates how to display text strings in a buddy window. It is based on sample<br />

5.2\CCtl, with a new spin control IDC_SPIN_STR and an edit box IDC_EDIT_STR added to the application.<br />

The edit control will display text strings “Zero”, “One”, “Two”,…, “Nine” instead of integers. The buddy<br />

of spin IDC_SPIN_STR is set automatically.<br />

The UDN_DELTAPOS message handler can be added through following steps: 1) Invoke Class Wizard,<br />

click “Messages Maps” tab. 2) Select “CCCtlDlg” class from “Class name” window, then highlight<br />

“IDC_SPIN_STR” in “Object IDs” window. 3) There will be two messages contained in “Messages”<br />

window, we need to highlight “UDN_DELTAPOS” and press “Add function” button. The newly added<br />

function will look like follows:<br />

void CCCtlDlg::OnDeltaposSpinStr(NMHDR* pNMHDR, LRESULT* pResult)<br />

{<br />

NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;<br />

}<br />

*pResult = 0;<br />

The first parameter here is a NMHDR type pointer. This is a structure that contains Windows<br />

notification messages. A notification message is sent to the parent window of a common control to notify<br />

the changes on that control. It is used to handle events such as mouse left button clicking, left button double<br />

clicking, mouse right button clicking, and right button double clicking performed on a common control.<br />

Many types of common controls use this message to notify the parent window. For spin control, after<br />

receiving this message, we need to cast the pointer type from NMHDR to NM_UPDOWN. Here structure<br />

MN_UPDOWN is defined as follows:<br />

typedef struct _NM_UPDOWN { nmud<br />

NMHDR hdr; // notification message header<br />

int iPos; // current position<br />

int iDelta; // proposed change in position<br />

} NM_UPDOWNW;<br />

In the structure, member iPos specifies the current position of the spin control, and iDelta indicates<br />

the proposed change on spin’s position. We can calculate the new position of the spin control by adding up<br />

these two members.<br />

The following function shows how the buddy’s text is set after receiving the message:<br />

void CCCtlDlg::OnDeltaposSpinStr(NMHDR* pNMHDR, LRESULT* pResult)<br />

{<br />

int nNewPos;<br />

char szNumber[][16]=<br />

{<br />

95

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

Saved successfully!

Ooh no, something went wrong!