Multiple dynamically created Spin Button Controls
-
Hello, ENVIRONMENT: MFC I seek help regarding the dynamic creation and use of Spin Button Controls. Every example/tutorial I've found provides a lot of information, all of which I already know. They all state the obvious, and avoid addressing the only non-obvious (at least to me) aspect. When dealing with a statically created (or a single dynamically created) control, "up" and "down" messages are generally handled by a method of the spin button's parent window, having a prototype such as:
void Dlg::OnDeltaposSpinFoo(NMHDR* pNMHDR, LRESULT* pResult)
{
}The NMHDR* parm can be type cast to an NMUPDOWN*, which provides data specific to a Spin Button Control. The message map for the parent window contains an entry such as:
ON\_NOTIFY(UDN\_DELTAPOS, IDC\_SPIN\_ADJUST\_FOO, OnDeltaposSpinFoo)
In such a case, it's obvious which spin control has been clicked, since the method is invoked for only a single control. Suppose I want a range of ID's to be associated with OnDeltaposSpinFoo()? Say I dynamically create a number of spin controls on a window, for which I wish to use a single message handler. For some controls, such as buttons, there are "range" macros that may be placed in the message map. How about for a spin button control? If I could manage to have OnDeltaposSpinFoo() called for a range of ID's, I could identify the control by the idFrom member of the NMHDR parm (see structures below). I'd probably subtract a base value from the idFrom value to obtain an index to be used programmatically). [
The relevant structures are:
typedef struct tagNMHDR {
HWND hwndFrom;
UINT_PTR idFrom;
UINT code;
} NMHDR;typedef struct _NM_UPDOWN {
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, *LPNMUPDOWN;] The bottom-line questions are: 1) How can I place an entry in the message map that will cause a "spin button prototype" function to be called (with an NMUPDOWN*) for a range of control ID's? 2) If that's not possible, how else can I handle the problem? I'd appreciate suggestions. Even more, I'd appreciate a working example project that will build and run using Visual Studio 2008 (I don't expect anyone to spend a lot of time creating one, but if you have one, or can quickly create one from something you have....).
-
Hello, ENVIRONMENT: MFC I seek help regarding the dynamic creation and use of Spin Button Controls. Every example/tutorial I've found provides a lot of information, all of which I already know. They all state the obvious, and avoid addressing the only non-obvious (at least to me) aspect. When dealing with a statically created (or a single dynamically created) control, "up" and "down" messages are generally handled by a method of the spin button's parent window, having a prototype such as:
void Dlg::OnDeltaposSpinFoo(NMHDR* pNMHDR, LRESULT* pResult)
{
}The NMHDR* parm can be type cast to an NMUPDOWN*, which provides data specific to a Spin Button Control. The message map for the parent window contains an entry such as:
ON\_NOTIFY(UDN\_DELTAPOS, IDC\_SPIN\_ADJUST\_FOO, OnDeltaposSpinFoo)
In such a case, it's obvious which spin control has been clicked, since the method is invoked for only a single control. Suppose I want a range of ID's to be associated with OnDeltaposSpinFoo()? Say I dynamically create a number of spin controls on a window, for which I wish to use a single message handler. For some controls, such as buttons, there are "range" macros that may be placed in the message map. How about for a spin button control? If I could manage to have OnDeltaposSpinFoo() called for a range of ID's, I could identify the control by the idFrom member of the NMHDR parm (see structures below). I'd probably subtract a base value from the idFrom value to obtain an index to be used programmatically). [
The relevant structures are:
typedef struct tagNMHDR {
HWND hwndFrom;
UINT_PTR idFrom;
UINT code;
} NMHDR;typedef struct _NM_UPDOWN {
NMHDR hdr;
int iPos;
int iDelta;
} NMUPDOWN, *LPNMUPDOWN;] The bottom-line questions are: 1) How can I place an entry in the message map that will cause a "spin button prototype" function to be called (with an NMUPDOWN*) for a range of control ID's? 2) If that's not possible, how else can I handle the problem? I'd appreciate suggestions. Even more, I'd appreciate a working example project that will build and run using Visual Studio 2008 (I don't expect anyone to spend a lot of time creating one, but if you have one, or can quickly create one from something you have....).
The
hwndFrom
andidFrom
parameters in the notification structure tell you which control is sending the message. Use theON_NOTIFY_RANGE
macro, as described in http://msdn.microsoft.com/en-us/library/749htf6k(v=vs.110).aspx[^].Veni, vidi, abiit domum
-
The
hwndFrom
andidFrom
parameters in the notification structure tell you which control is sending the message. Use theON_NOTIFY_RANGE
macro, as described in http://msdn.microsoft.com/en-us/library/749htf6k(v=vs.110).aspx[^].Veni, vidi, abiit domum
Thank you for your feedback. The range macros of which I was aware were: ON_COMMAND_RANGE, and ON_CONTROL_RANGE, neither of which matched the function prototype I normally use for a spin button control. The ON_NOTIFY_RANGE worked great. However, I did have to add an ID parameter to the message handling function. [The first parameter, in case anyone is interested.] Due to the additional parameter, one need not use the mentioned structure members to identify the control.