VC Tips Summary of Control skills _c language

Source: Internet
Author: User

This article collects the VC small skill the control skill, for carries on the VC development to have certain reference value, concretely as follows:

1. How to hide and display controls

The function of the CWnd class, bool ShowWindow (int ncmdshow), can hide or display a control.
Example 1:

CWnd *pwnd;
pwnd = GetDlgItem (IDC_EDIT1); Gets the control pointer, idc_edit the control ID number
Pwnd->showwindow (sw_hide);   Hide Controls

Example 2:

CWnd *pwnd;
pwnd = GetDlgItem (IDC_EDIT1); Gets the control pointer, idc_edit the control ID number
Pwnd->showwindow (sw_show);   Display control

2. Enabling and banning of buttons

Define variables for the button using the ClassWizard member variables, such as: M_button1;
The

M_button1.enablewindow (true);  Leave the button in the Allowed state
M_button1.enablewindow (false);  Make the button disabled and dimmed to show

3. Change the size and position of the control

You can change the size and position of a control by using the function MoveWindow () or SetWindowPos () of the CWnd class.

void MoveWindow (int x,int y,int nwidth,int nheight);
void MoveWindow (Lpcrect lprect);

The first usage needs to give the control new coordinates and the width, the height;
The second usage gives the CRect object of the storage location;
Cases:

CWnd *pwnd;
pwnd = GetDlgItem (idc_edit1);  Gets the control pointer, idc_edit1 the control ID number
Pwnd->movewindow (CRect (0,0,100,100));//Display an edit control
with a width of 100 and 100 in the upper-left corner of the window The SetWindowPos () function is more flexible and is used to modify the position of the control only to the same size or to modify the size only:
BOOL setwindowpos (const cwnd* pwndinsertafter,int x,int y, int cx,int cy,uint nflags);

The first parameter is generally set to null;
x, y control position; CX, CY control width and height;

Nflags commonly used values:

Swp_nozorder: ignores the first parameter;
Swp_nomove: Ignore x, Y, maintain position unchanged;
Swp_nosize: Ignore CX, CY, maintain the same size;

Cases:

CWnd *pwnd;
pwnd = GetDlgItem (idc_button1);  Gets the control pointer, idc_button1 the control ID number
Pwnd->setwindowpos (Null,50,80,0,0,swp_nozorder | Swp_nosize); Move the button to the window (50,80) at
pwnd = GetDlgItem (idc_edit1);
Pwnd->setwindowpos (Null,0,0,100,80,swp_nozorder | Swp_nomove); The size of the edit control is set to (100,80) and the position is constant
pwnd = GetDlgItem (idc_edit1);
Pwnd->setwindowpos (Null,0,0,100,80,swp_nozorder); Edit controls vary in size and position

The above methods are also applicable to various windows.

4. Use of radio button controls (Radio buttons)

(1) to group radio buttons:

The first radio button in each group sets properties: Group,tabstop,auto; The remaining buttons set properties Tabstop,auto.
Such as:
Radio1, Radio2, Radio3 as a group, Radio4, Radio5 as a group
Set RADIO1 properties: Group,tabstop,auto
Set Radio2 properties: Tabstop,auto
Set Radio3 properties: Tabstop,auto
Set Radio4 properties: Group,tabstop,auto
Set RADIO5 properties: Tabstop,auto

(2) Define variables with ClassWizard for the radio control, each group can only define one. such as: M_radio1, M_radio4.

(3) Use ClassWizard to generate the click Message function of each radio button, and add content:

void Cweditview::onradio1 () 
{
  m_radio1 = 0;  The first radio button is selected
}
void Cweditview::onradio2 () 
{
  m_radio1 = 1;  The second radio button is selected
}
void Cweditview::onradio3 () 
{
  m_radio1 = 2;  The third radio button is selected
}
void Cweditview::onradio4 () 
{
  M_radio4 = 0;  The fourth radio button is selected
}
void Cweditview::onradio5 () 
{
  M_radio4 = 1;  The Fifth radio button is selected
}

When the control variable value is 0 o'clock, it is selected for the first radio button of the group.

(4) Set the default button:

When you define a control variable, ClassWizard sets the variable initial value to 1 in the constructor, just change it to something else.
Such as:

{{Afx_data_init (cweditview)
m_ridio1 = 0;  Initially, the first radio button is selected
M_ridio4 = 0;  The fourth radio button is selected at the beginning
//}}afx_data_init

4. Use of rotating controls (Spin)

When you click a button on a rotated control, the corresponding edit control value increases or decreases. The general steps for its setting are:

(1) Put a spin control and an edit control in the dialog box as the Partner window for the spin control

Set spin control properties: Auto buddy, set buddy integer, Arrow keys
Set Text Control properties: number

(2) Define variable M_spin for the Spin control with ClassWizard, define variable M_edit for the edit control, and note that the M_edit is set to int when defined.

(3) Add the statement in the OnInitDialog () function of the dialog box:

BOOL Cmydlg::oninitdialog () 
{
  cdialog::oninitdialog ();

  M_spin.setbuddy (GetDlgItem (idc_edit1)); Sets the partner window for the edit control as the Spin control
  m_spin.setrange (0);  Set data range to 0-10 return
  TRUE;
}

(4) Add the En_change message handler function for the edit control with ClassWizard and add the statement:

void Cmydlg::onchangeedit1 () 
{
  m_edit = M_spin.getpos ();  Get spin control current value
}

5.UpdateData ()

The UpdateData () function is critical for controls that can receive data, such as edit controls. When the content of the control changes, the value of the corresponding control variable does not change, and the content of the control is not changed as the value of the control variable changes.
UpdateData () function is to solve this problem.
UpdateData (TRUE); To load a control's contents into a control variable
UpdateData (false); To update a control with the value of a control variable
such as: There are edit Control idc_edit1, the corresponding variable is string m_edit1,

(1) Modify the value of the variable and display it in the control:

M_edit1 = _t ("result is)";
UpdateData (FALSE);

(2) Read the value of the control into the variable:

Add the En_change message handler function for idc_edit1 with ClassWizard, which is executed when editing the content of the control changes.

void Ceditview::onchangeedit1 ()
{
  UpdateData (true);  Update variable Value
}

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.