Use of the spin control in VC

Source: Internet
Author: User

Address: http://leichaofu.blog.163.com/blog/static/45948182009868543162/

Set attributes:
Auto Buddy (automatically take the associated control as the first one in the tab order)
Set buddy interger (enable the control to set the value of the associated control, which can be in decimal or hexadecimal format)
Wrap (loop when the value exceeds the range)
Arrow keys (the control can be increased or decreased when the up and down direction keys are pressed)

Set the control value range in the oninitdialog function:
Setrange (0,255 );

Rotate button control

"Rotate button controls" (also known as up and down controls) are a pair of Arrow buttons. You can click them to increase or decrease
Value, such as a scroll position or a number displayed in the corresponding control. A rotation control is usually accompanied by
This control is called the "partner window ".

A rotating button control can be automatically positioned next to its partner window and looks like a single control.
Generally, a rotate button control is used with an edit control to prompt users to enter numbers. Click up
The arrow moves the current position to the maximum value, and click the downward arrow to move the current position to the minimum value. Default
The minimum value is 100, and the maximum value is 0. When you click the up arrow, the value is reduced, and click the down arrow to increase.
It, which looks like reversing, so we also need to use the member function cspinbuttonctrl: setrange to change
The maximum and minimum values.

(1) The style commonly used for rotating button controls

The commonly used rotation button controls have the following styles:

 

Uds_horz

The arrow of the control points to the left and right, rather than to the top and bottom.

 

Uds_wrap

If the increase or decrease of the control exceeds the end or start, the value can be "cyclically ".

 

Uds_arrowkeys

When you press the up or down arrow keys, the control can be increased or decreased.

 

Uds_setbuddyint

Enables the control to set the value of the partner window. The value can be in decimal or hexadecimal format.

 

Uds_nothousands

Do not add thousands of delimiters to every three decimal digits.

 

Uds_autobuddy

The previous window in Z-order is automatically selected as the control partner window.

 

Uds_alignright

Hover the button window to the right of the partner window. The width of the partner window is reduced to adapt to the width of the control.

 

Uds_alignleft

Hover the button window to the right of the partner window. The partner window is moved to the right and its width is reduced to fit the width of the control.

You can also set these styles in the rotate Button Control Properties dialog box. The Styles attributes are described as follows:

 

. Orientation

Control placement direction: vedical (vertical), hofizontal (horizontal, same as uds_horz ).

 

. Alignment

Controls in the partner window: Unattached (irrelevant), right (right, same as uds_alignright), left (left, same as uds_allgnleft ).

 

. Auto Buddy

Same as uds_autobuddy.

 

. Set buddy integer

The same as uds_setbuddyint.

 

. No thousands

Same as uds_nothousands.

 

. Wrap

Same as uds_wrap.

 

. Arrow Keys

The same as uds_arrowkeys.

 

. Hot track

Mouse hotspot tracking.

(2) Basic operations for rotating button controls

The basic operations include base, range, location setting, and acquisition. They are described as follows:

Set and obtain the base number

The member function setbase is used to set its base. Its prototype is as follows:

Int setbase (INT nbase );

This member function is used to set the base number of a rotating button control. This base value determines that the number displayed in the partner window is in decimal format.
It is a hexadecimal number. The hexadecimal number is always unsigned; the decimal number is signed. If successful, the previous base value is returned,
If an invalid base number is given, a non-zero value is returned. The parameter nbase indicates the new base of the control.

This function corresponds to a member function that obtains the base number of a rotating button control: uint getbase () const;

Set and obtain the range and current position

The setpos and setrange functions are used to set the current position and range of a rotating button control. The prototype is as follows:

Int setpos (INT NPOs );

The NPOs parameter indicates the new position of the control. This value must be within the range specified by the upper and lower limits of the control.

Void setrange (INT nlower, int nupper );
Void setrange32 (INT nlower, int nupper );

The nlower and nupper parameters indicate the upper and lower limits of the control. Any boundary value cannot be greater than or less than ud_maxval.
Ud_minval. In addition, the difference between two boundary values must not exceed ud_maxval. The member function setrange32 is
Rotate the button control to set the 32-bit range.

The member functions corresponding to the preceding functions include:

Int getpos () const;
DWORD getrange () const;
Void getrange (Int & Lower, Int & upper) const;
Void getrange32 (Int & Lower, Int & upper) const;

Other member functions

Cwnd * setbuddy (cwnd * pwndbuddy );

This member function is used to set a companion window for a rotating control. Returns a pointer to the previous partner window. Parameters
Pwndbuddy indicates a pointer to the new partner window.

Bool setaccel (INT naccel, udaccel * paccel );

This member function is used to set the acceleration of a rotating button control. If the operation succeeds, a non-zero value is returned; otherwise, 0 is returned. Parameter naccel table
Indicates the number of udaccel structures specified by paccel. paccel points to a udaccel structure array, which contains the acceleration
Information. Its structure is as follows:
Typedef struct {
Uint nsec; // The number of seconds before the position changes.
Uint ninc; // location increment.
} Udaccel, far * lpudaccel;

The member functions corresponding to the preceding functions include:

Cwnd * getbuddy () const;
Uint getaccel (INT naccel, udaccel * paccel) const;

(3) Notification Message for rotating button controls

There is only one notification message for the rotating button control: udn_deltapos, which indicates that the control's position will change.
Classwizard can be used to map this message. In this message processing function, there is a nm_updown structure that needs to be performed.

The structure is as follows:

Typedef struct _ nm_updown {
Nmhdr; // other notification code information
Int IPOs; // current location
Int idelta; // the increase or decrease of the position. Click the up arrow and the value is negative.
} Nmupdown, far * lpnmupdown;

Application Example: Use the spin control to increase or decrease characters

 

Response Message of udn_deltapos

Message udn_deltapos of the rotating button control

Indicates that the widget location will change.

The udn_deltapos message of the reload spin control will have the following functions:

Void ctestdlg: ondeltaposspin3 (nmhdr * pnmhdr, lresult * presult)

{

Nm_updown * pnmupdown = (nm_updown *) pnmhdr;

If (pnmupdown-> idelta = 1) // if this value is 1, the spin arrow is clicked.

{

// Process the characters. For example, convert "A" to "B"

}

Else if (pnmupdown-> idelta =-1) // if this value is-1, it means you have clicked the up arrow of spin.

{

// Process the characters. For example, convert "B" to ""

}

* Presult = 0;

}

Detailed practices:

1: assume that the edit box is idc_edit and the spin button is idc_spin.

2: Ctrl + W, Enter Class Wizard, select idc_spin in the object IDs column, select udn_deltapos in the messages column, and click "add function. the "ondeltaposspin" dialog box is displayed. Click OK to add this function. click "edit code" to enter the newly added function.

3: A new function will be added.

Void ctestdlg: ondeltaposspin (nmhdr * pnmhdr, lresult * presult )...

4: Add the following code:

Void ctestdlg: ondeltaposspin (nmhdr * pnmhdr, lresult * presult)

{

Nm_updown * pnmupdown = (nm_updown *) pnmhdr;

 

Cwnd * pwnd = (cwnd *) getdlgitem (idc_edit); // get the window pointer pointing to the edit box.

Cstring strvalue;

Pwnd-> getwindowtext (strvalue); // get the content in the edit box.

 

If (pnmupdown-> idelta = 1) // if you click the up button in spin

{

If (! Strvalue. isempty ())

{

Strvalue. setat (0, strvalue [0] + 1); // Add 1 to the first letter of the edit box

Pwnd-> setwindowtext (strvalue); // Save the modification

}

}

Else if (pnmupdown-> idelta =-1) // if you click the down button in spin

{

If (! Strvalue. isempty ())

{

Strvalue. setat (0, strvalue [0]-1); // The first letter of the edit box minus 1.

Pwnd-> setwindowtext (strvalue); // Save the modification

}

}

* Presult = 0;

}

After using the above method, I encountered a problem: when I keep holding up or down, I will not increase after increasing 16. Later I found that I can change the condition to be greater than 0 or less than 0, greater than 0 indicates the up button.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.