VC ++ MFC Control Learning

Source: Internet
Author: User

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); // get the control pointer. IDC_BUTTON1 is the Control ID pWnd-> SetWindowPos (NULL, 50, 80, SWP_NOZORDER | SWP_NOSIZE ); // move the button to the (50, 80) Position of the window pWnd = GetDlgItem (IDC_EDIT1); pWnd-> SetWindowPos (NULL, 80, SWP_NOZORDER | SWP_NOMOVE ); // set the size of the editing control to (, 80), and the position remains unchanged pWnd = GetDlgItem (IDC_EDIT1); pWnd-> SetWindowPos (NULL, 80, SWP_NOZORDER ); // modify the widget's 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 (LPRECT 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); // obtain the Edit Control pointer, IDC_MYEDIT is the Control ID: pEditWnd-> SetWindowPos (NULL, rect. right, rect. bottom-50, SWP_NOMOVE | SWP_NOZORDER );// Set the size of the Control. bottom-50 is used to make 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; // when the first radio button is selected, m_Ridio4 = 0; // In the initial stage, the fourth radio button was selected. //} 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); // set the editing control to the spin control's partner window 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 (); // obtain the current value of the spin control}

5. Set "set bueey integer" of the spin control to true.
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 indicates the Document Modification flag, set it to True {int t; t =: MessageBox (NULL, "the text has changed. Do you want to save the file? "," Warning ", MB_YESNOCANCEL | MB_ICONWARNING); // In the displayed dialog box, 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 (); // 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. close () ;}} return CDocument: CanCloseFrame (pFrame );}

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.