Link to home
Start Free TrialLog in
Avatar of solomon021499
solomon021499

asked on

Activating a spin control programatically

I am trying to activate a spinner programmatically.
I thought I should do this by sending the spin
control a message.
i.e.: spinCtrl->SendMessage(UDN_DELTAPOS,0,(LPNMHDR)&nmHdr);
where nmHdr is a pointer to a NM_UPDOWN structure
which sets the iDelta field to indicate how the spinner changed.
I must be getting the message wrong.

Any suggestions?
I am using MSVC5.0.

Thanks
Avatar of snoegler
snoegler

I hope i did understand your question right :)

MFC version:

spinCtrl->SetPos(...);

SDK:

SendMessage(hWndSpinCtrl,UDM_SETPOS,0,...);
Avatar of solomon021499

ASKER

Yes, you understand me.
Both of your suggestions should work, but neither is.

I have a handler for the spin control - i.e.:
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_CTRL, OnDeltaposSpinCtrl)

Therefore, OnDeltaposSpinCtrl() should executed when I send
the UDM_SETPOS message or call the SetPos member function.

The problem is that this is not happening.
Yes, you understand me.
Both of your suggestions should work, but neither is.

I have a handler for the spin control - i.e.:
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_CTRL, OnDeltaposSpinCtrl)

Therefore, OnDeltaposSpinCtrl() should executed when I send
the UDM_SETPOS message or call the SetPos member function.

The problem is that this is not happening.
UDN_DELTAPOS is only sent if the spin control is being changed by the keyboard or the
mouse.  It isn't sent if you set the position using 'UDM_SETPOS'.
Why do you need the UDN_DELTAPOS handler to be called?
I want the same behavior is the up arrow sping control was clicked with the mouse button or the up arrow key was hit.  The handler and codle already exists.  Therefore, sending the message that would execute that code is the way to go.

Unfortunately, sending the UDM_SETPOS message isn't doing the trick either.  The handler is not being executed.

I can of course work around this, but this should be easy.

I want the same behavior is the up arrow sping control was clicked with the mouse button or the up arrow key was hit.  The handler and codle already exists.  Therefore, sending the message that would execute that code is the way to go.

Unfortunately, sending the UDM_SETPOS message isn't doing the trick either.  The handler is not being executed.

I can of course work around this, but this should be easy.

I think I need to send a WM_NOTIFY message with the appropriate information in an NM_UPDOWN structure cast to an LPARAM in the SendMessage function.  

However, I haven't gotten this to work either.
Hi Solomon,

I think you need to change the value when the spin button ctrl is clicked.

Hi Solomon,

I think you need to change the value when the spin button ctrl is clicked.

Using the classwizard create a message map for the UDN_DELTAPOS.

ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN1, OnDeltaposSpin1)

Increment the value

      pNMUpDown->iDelta = pNMUpDown->iDelta+5;

Here I have incremented it to 5. It works fine.


Hi Kanni,
Your suggestion works fine if you can get the handler to execute.  I have a message map and it works fine when the spin control is activated via a mouse click.  I am trying to send this message so that this functions executes as if the mouse had been activated when in actuality it has not.

I meant as if the control had been activated by the mouse when it had not.
I meant as if the control had been activated by the mouse when it had not.
I meant as if the control had been activated by the mouse when it had not.
Can you elaborate why SetPos() doesn't work? Or what you expect the spinner to do?
snoegler,
No I can not elaborate on why SetPos() doesn't work.

I want the application to behave as if the spinner was activated.

For example, if I were talking about a button, I would send do the following:

buttonCtrl->SendMessage(WM_LBUTTONDOWN, MK_LBUTTON,
                        MAKELONG(USHORT(1), USHORT(1)) );
buttonCtrl->SendMessage(WM_LBUTTONUP, MK_LBUTTON,
                        MAKELONG( USHORT(1), USHORT(1)));

to progromatically activate the button.  As a result the button's ON_BUTTON_CLICKED handler would execute.

This is what I want with the spin control.

Thanks.
What i mean is:

Does SetPos() don't work? Or is the call of OnDeltaPos() not working, but the position can
be changed?
If the last case fits, could you describe why OnDeltaPos() must be called? Isn't there a other
way?
First SetPos() does not work.
Second, the UDN_DELTAPOS works when it is executed, but it is not being executed.
Finally, yes there is another way, but it would be the wrong way.  The correct thing to do is simply send this message and have the messaging system handle the rest i.e.: execute the handler.

There must be something wrong with the message that I am sending.
I am working with the MFC for quite a while now ... and i've never found a situation where
the SetPos() function didn't work.
I am assuming that maybe the ID you use for the UDN_DELTAPOS handler is wrong.
The SetPos() function _will_ set the value of the spin control. If not, either the spin control
can't be adressed (ID wrong) or the spin control is disabled.
Please check if the ID of the spin control is really equal to the one you use in your ON_xxx
message map entry. If not, i wonder if SetPos(5); ASSERT(GetPos()==5); would work.
BTW: UDN_DELTAPOS is a internal message of the spin control, and is called from within
the spin controls' WM_LBUTTONDOWN handler. I don't think that you can emulate the
UDN_DELTAPOS notification.
Isn't it possible to encapsulate the common functionality you like for the UDN_DELTAPOS
and for your 'position set issue' into a different function ...
BTW: Do you expect the spin control to set another control (Edit control)?
Thanks,
I'll look into it and get back to you.
snoegler,
Your last bit of information enabled me to figure it out.  You can emulate the UDN_DELTAPOS notification, but you need to send the WM_LBUTTONDOWN message instead of the UDM_SETPOS.

Thanks.  Make another posting as an answer and I'll give you an A.
ASKER CERTIFIED SOLUTION
Avatar of snoegler
snoegler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial