Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls
17.1.5..The Spin Button Events......................................................................
When the user clicks one of the arrow buttons of a spin button or presses an up or a down
arrow key when the control has focus, the operaing sends a UDN_DELTAPOS message
to the parent window of the spin button, notifying this parent that the position (the value)
of the control is about to be changed. The syntax of the event fired by the
UDN_DELTAPOS message is:
OnDeltaPosSpin(NMHDR* pNMHDR, LRESULT* pResult)
Because this event is fired before the value of the spin button is changed, you can use it
to check, validate, allow or deny the change. The first argument, pNMHDR, is an
NMHDR structure value. The NMHDR structure is sometimes used to carry information
about a message. It is defined as follows:
typedef struct tagNMHDR {
HWND hwndFrom;
UINT idFrom;
UINT code;
} NMHDR;
The hwndFrom member variable is a handle to the window that is sending the message.
The idFrom is the identifier of the control that is sending the message. The code member
is the actual notification code.
When implementing the event of the UDN_DELTAPOS message, instead of using the
value of the NMHDR argument, Visual C++ takes the liberty of casting the pNMHDR
pointer into a pointer to NM_UPDOWN. Therefore, the event provided to you appears as
follows:
void CDlgSpin::OnDeltaPosSpinNew(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
}
The NM_UPDOWN structure is defined as follows:
typedef struct _NM_UPDOWN {
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, FAR *LPNMUPDOWN;
This structure was specifically created to carry notification information for a spin button.
The first member variable of the NM_UPDOWN structure, hdr, is an NMHDR. The hdr
itself carries additional information about the message being sent, as mentioned above.
The iPos member variable is the value of the current position of the spin button. The
iDelta member is the intended change that would be performed on the spin button.
Practical Learning: Using the Spin Button Events
- If you are using MSVC 6, display the ClassWizard and the Message Maps property
page. Click IDC_SPIN_RED. In the Messages list, double-click UDN_DELTAPOS.