VC Study Notes-Controls (1)

Source: Internet
Author: User

The content is from an online article. I did not write down the name. I would like to thank the author!

VC Study Notes 1: Enable and disable buttons

Use the member variables of classwizard to define variables for buttons, such as m_button1;
Then
M_button1.enablewindow (true); enable the button
M_button1.enablewindow (false); disables the button and changes it to Gray.

VC Study Notes 2: Hide and display controls

You can use the function bool showwindow (INT ncmdshow) of the cwnd class to hide or display a control.

Example 1:
Cwnd * pwnd;
Pwnd = getdlgitem (idc_edit1); // gets the control pointer. idc_edit indicates the Control ID.
Pwnd-> showwindow (sw_hide); // hide the control

Example 2:
Cwnd * pwnd;
Pwnd = getdlgitem (idc_edit1); // gets the control pointer. idc_edit indicates the Control ID.
Pwnd-> showwindow (sw_show); // Display Control

The above method is often used to dynamically generate controls. Although the create function of the control can dynamically generate controls, this control is not easy to control, so hiding and displaying is an alternative.

VC study Note 3: Change the widget size and position

You can use the functions movewindow () or setwindowpos () of the cwnd class to change the widget size and position.

Void movewindow (int x, int y, int nwidth, int nheight );
Void movewindow (lpcrect lprect );
The first method requires the new coordinates, width, and height of the control;
The second method provides the crect object of the storage location;
Example:
Cwnd * pwnd;
Pwnd = getdlgitem (idc_edit1); // gets the control pointer. idc_edit1 indicates the Control ID.
Pwnd-> movewindow (crect (100,100, 100); // an editing control with a width of 100 and a height of is displayed in the upper left corner of the window.

The setwindowpos () function is more flexible to use. It is mainly used when only the control position is changed but the size is not changed or only the size is changed but the position is not changed:
Bool setwindowpos (const cwnd * pwndinsertafter, int X, int y, int CX, int cy, uint nflags );
I will not use the first parameter. It is generally set to NULL;
Position of the X and Y controls; width and height of the Cx and Cy controls;
Common nflags values:
Swp_nozorder: Ignore the first parameter;
Swp_nomove: Ignore X and Y and keep the position unchanged;
Swp_nosize: Ignore CX and Cy and keep the size unchanged;
Example:
Cwnd * pwnd;
Pwnd = getdlgitem (idc_button1); // gets the control pointer. idc_button1 indicates the Control ID.
Pwnd-> setwindowpos (null, 50, 80, swp_nozorder | swp_nosize); // move the button to the window (50, 80)
Pwnd = getdlgitem (idc_edit1 );
Pwnd-> setwindowpos (null, 80, swp_nozorder | swp_nomove); // set the size of the Edit Control to (, 80), and the position remains unchanged
Pwnd = getdlgitem (idc_edit1 );
Pwnd-> setwindowpos (null, 80, swp_nozorder); // modify the widget size and position
The preceding method also applies to various windows.

VC study NOTE 4: when to set the initial size of the video control?

I added an edit control to the cformview to fill the client zone at runtime, and changed the window when it changed.
You can change the widget size either in the ondraw () function or in the calcwindowrect () function. When the window size changes, they are all executed and calcwindowrect () the function is prior to the ondraw () function. The following example shows how to modify the widget size in the calcwindowrect () function.
Reload the calcwindowrect function of the View class and add the statement for setting the widget size to this function.
Example:
Void cmyeditview: calcwindowrect (ldivct lpclientrect, uint nadjusttype)
{
// Todo: add your specialized code here and/or call the base class

Cframewnd * pframewnd = getparentframe (); // obtain the frame window pointer

Crect rect;
Pframewnd-> getclientrect (& rect); // obtain the customer area size

Cwnd * peditwnd = getdlgitem (idc_myedit); // gets the Edit Control pointer. idc_myedit is the Control ID.
Peditwnd-> setwindowpos (null, 0, 0, rect. Right, rect. Bottom-50, swp_nomove | swp_nozorder); // set the control size. Bottom-50 is used to give the output status bar position.

Cformview: calcwindowrect (lpclientrect, nadjusttype );
}

VC study Note 5: Use of the single-choice button control (ridio button)

1. Group Single-choice buttons:
Set attributes for the first single-choice button in each group: group, tabstop, and auto. Set attributes for other buttons: tabstop and auto.

For example:
Ridio1, ridio2, and ridio3 are a group, and ridio4 and ridio5 are a group.

Set ridio1 attributes: group, tabstop, auto
Set the ridio2 attribute: tabstop, auto
Set the ridio3 attribute: tabstop, auto

Set ridio4 attributes: group, tabstop, auto
Set the ridio5 attribute: tabstop, auto

2. Use classwizard to define variables for single-choice controls. Each group can only define one variable. For example, m_ridio1 and m_ridio4.

3. Use classwizard to generate the click message function of each radio button and add the following content:

Void cweditview: onradio1 ()
{
M_ridio1 = 0; // The first radio button is selected
}

Void cweditview: onradio2 ()
{
M_ridio1 = 1; // The second radio button is selected.
}

Void cweditview: onradio3 ()
{
M_ridio1 = 2; // The third radio button is selected.
}

Void cweditview: onradio4 ()
{
M_ridio4 = 0; // The fourth radio button is selected.
}

Void cweditview: onradio5 ()
{
M_ridio4 = 1; // The fifth radio button is selected.
}

4. Set the default button:
When defining a control variable, classwizard sets the initial value of the variable to-1 in the constructor. You only need to change it to another value.
For example:
// {Afx_data_init (cweditview)
M_ridio1 = 0; // The first radio button is selected at the beginning.
M_ridio4 = 0; // The fourth radio button is selected in the initial stage.
//} Afx_data_init

VC study Note 6: Use of the rotation control (spin)

When you click the button on the rotating control, the value of the corresponding edit control increases or decreases. The general steps are as follows:
1. Put a spin control and an edit control in the dialog box as the partner window of the spin control,
Set attributes of the spin control: Auto Buddy, set buddy integer, and arrow keys.
Set the property of the text control: Number

2. Use classwizard to define the variable m_spin for the spin control and the variable m_edit for the Edit Control. Set m_edit to the int type when defining the control.

3. Add a statement to the oninitdialog () function of the dialog box:
Bool cmydlg: oninitdialog ()
{
Cdialog: oninitdialog ();

M_spin.setbuddy (getdlgitem (idc_edit1); // you can specify the partner window for the spin control.
M_spin.setrange (0, 10); // set the data range to 0-10.
Return true;
}

4. Use classwizard to add the en_change message processing function for the editing control, and then add the statement:
Void cmydlg: onchangeedit1 ()
{
M_edit = m_spin.getpos (); // gets the current value of the spin control.
}

OK!

VC study notes 7: saving files when the program ends

In the document-view structure, automatic saving of files using serialization is described in various VC books. The problem now is that I do not use serialization, but save it by myself. When I click the close button in the window, how can I prompt and save the document.

Use classwizard to add the cancloseframe () function to the file class (cxxdoc), and then add the statement to save the file.
Note: The data to be saved should be stored in the document class (cxxdoc) or application class (cxxapp), not in the View class.

Example:
// Exit the program
Bool ceditdoc: cancloseframe (cframewnd * pframe)
{
Cfile file;
If (B _flag) // B _flag is the Document Modification flag, which is set to true when the document is modified.
{
Int T;
T =: MessageBox (null, "the text has changed. Do you want to save the disk? "," Warning ",
Mb_yesnocancel | mb_iconwarning); // The prompt dialog box is displayed.
If (t = 0 | T = idcancel)
Return false;
If (t = idyes)
{
Cstring sfilter = "text file (*. txt) | *. txt | ";
Cfiledialog m_dlg (false, "TXT", null, ofn_hidereadonly | ofn_overwriteprompt, (lpctstr) sfilter, null); // custom file dialog box

Int K = m_dlg.domodal (); // The file dialog box is displayed.
If (k = idcancel | K = 0)
Return false;
M_pathname = m_dlg.getpathname (); // obtain the selected file path name

File. Open (m_pathname, cfile: modecreate | cfile: modewrite );
File. Write (m_text, m_textlen); // write data to the file
File. Close ();
}
}
Return cdocument: cancloseframe (pframe );
}

VC Study Notes 8: updatedata ()

For controls that can receive data, such as editing controls, the updatedata () function is crucial. When the control content changes, the corresponding control variable value does not change. Similarly, when the control variable value changes, the control content does not change.
The updatedata () function solves this problem.

Updatedata (true); load the control content into the control variable
Updatedata (false); Update the control with the value of the control variable

For example, the idc_edit1 edit control corresponds to the m_edit1 string,
1. Modify the variable value and display it in the control:
M_edit1 = _ T ("Result: 50 ");
Updatedata (false );
2. Read the control value to the variable:
Use classwizard to add the en_change message processing function for idc_edit1,
Void ceditview: onchangeedit1 ()
{
Updatedata (true );
}

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.