VC tips summary of control skills

  • 2020-04-02 02:31:14
  • OfStack

This paper collects and summarizes the control skills of VC tips, for the development of VC has a certain reference value, specific as follows:

1. How to hide and display controls

The CWnd class function BOOL ShowWindow(int nCmdShow) can be used to hide or display a control.
Case 1:


CWnd *pWnd;
pWnd = GetDlgItem( IDC_EDIT1 ); //Gets the control pointer, IDC_EDIT is the control ID number
pWnd->ShowWindow( SW_HIDE );   //Hidden controls

Example 2:


CWnd *pWnd;
pWnd = GetDlgItem( IDC_EDIT1 ); //Gets the control pointer, IDC_EDIT is the control ID number
pWnd->ShowWindow( SW_SHOW );   //According to the control

2. Enable and disable buttons

Define Variables for buttons using Member Variables in the ClassWizard, such as m_Button1;
the


m_Button1.EnableWindow(true);  //Put the button in the allowed state
m_Button1.EnableWindow(false);  //Causes the button to be disabled and grayed out

3. Change the size and position of the control

Use the CWnd class function MoveWindow() or SetWindowPos() to change the size and position of the control.


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 width, height;
The second use gives the location of the CRect object.
Ex. :


CWnd *pWnd;
pWnd = GetDlgItem( IDC_EDIT1 );  //Gets the control pointer, IDC_EDIT1 is the control ID number
pWnd->MoveWindow( CRect(0,0,100,100) ); //Displays an edit control 100 by 100 in the upper left corner of the window
SetWindowPos() Function is more flexible, and is mostly used to change the position of the control but the size is unchanged or to change the size but the position is unchanged: 
BOOL SetWindowPos(const CWnd* pWndInsertAfter,int x,int y,int cx,int cy,UINT nFlags);

The first parameter is usually set to NULL;
X, y control position; Width and height of cx and cy controls;

NFlags commonly used value:

SWP_NOZORDER: ignores the first parameter;
SWP_NOMOVE: ignore x, y, keep the position unchanged;
SWP_NOSIZE: ignore cx and cy and keep the size unchanged;

Ex. :


CWnd *pWnd;
pWnd = GetDlgItem( IDC_BUTTON1 );  //Gets the control pointer, IDC_BUTTON1 is the control ID number
pWnd->SetWindowPos( NULL,50,80,0,0,SWP_NOZORDER | SWP_NOSIZE ); //Move the button to the window at (50,80)
pWnd = GetDlgItem( IDC_EDIT1 );
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER | SWP_NOMOVE ); //Set the size of the edit control to (100,80) in the same position
pWnd = GetDlgItem( IDC_EDIT1 );
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER ); //The size and position of the edit control change

The above method also applies to various Windows.

4. Use of Radio Button control

(1) group radio buttons:

The first radio button of each Group sets properties: Group, Tabstop, Auto; The rest of the buttons set the properties Tabstop, Auto.
Such as:
Radio1, Radio2, Radio3 in groups, Radio4, Radio5 in groups
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 for the radio control using the ClassWizard, only one per group. For example: m_Radio1, m_Radio4.

(3) use ClassWizard to generate the click message function of each radio button and add the 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 value of the control variable is 0, the first radio button of its corresponding group is selected.

(4) set the default button:

When defining a control variable, the ClassWizard sets the initial value of the variable to -1 in the constructor, simply changing it to something else.
Such as:


//{{AFX_DATA_INIT(CWEditView)
m_Ridio1 = 0;  //Initially the first radio button is selected
m_Ridio4 = 0;  //Initially the fourth radio button is selected
//}}AFX_DATA_INIT

4. Use of Spin control

When the button on the rotate control is clicked, the corresponding edit control value increases or decreases. The general steps of its setting are as follows:

(1) place a Spin control and an edit control in the dialog box as the partner window of the Spin control

Set Spin control properties: Auto buddy, Set buddy integer, Arrow keys
Sets the text control property: Number

(2) define the variables m_Spin for the Spin control and m_Edit for the edit control with the ClassWizard.

(3) add a statement to the OnInitDialog() function of the dialog box:


BOOL CMyDlg::OnInitDialog() 
{
  CDialog::OnInitDialog();

  m_Spin.SetBuddy( GetDlgItem( IDC_EDIT1 ) ); //Sets the edit control to the partner window of the Spin control
  m_Spin.SetRange( 0, 10 );  //Set the data range to 0-10
  return TRUE;
}

(4) add the EN_CHANGE message handler function for the edit control with ClassWizard, and then add the statement:


void CMyDlg::OnChangeEdit1() 
{
  m_Edit = m_Spin.GetPos();  //Gets the current value of the Spin control
}

5. The UpdateData ()

The UpdateData() function is critical for controls that can receive data, such as edit controls. When the content of a control changes, the value of the corresponding control variable does not change, and likewise, when the value of a control variable changes, the control content does not change.
The UpdateData() function solves this problem.
The UpdateData (true);       Loads control contents into control variables
The UpdateData (false);     Updates the control with the value of the control variable
If there is an edit control IDC_EDIT1, the corresponding variable is the string m_Edit1,

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


m_Edit1 = _T(" The results for 50");
UpdateData(false);

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

Use the ClassWizard to add the EN_CHANGE message handler for IDC_EDIT1, which is executed when the content of the edit control changes.


void CEditView::OnChangeEdit1()
{
  UpdateData(true);  //Update variable value
}

Related articles: