MFC about Windows operations.

Source: Internet
Author: User

VC Common Tips--to let the window start to maximize

Window

Maximize the window as soon as it starts
Put the Application class (Cxxxapp) in the InitInstance () function
M_pmainwnd->showwindow (Sw_show); Switch
M_pmainwnd->showwindow (sw_showmaximized);
The window is maximized as soon as it starts.

How to set the initial size of a window
In the InitInstance () function of the application class (Cxxapp), add:
M_pmainwnd->setwindowpos (Null,x,y,width,height,swp_nomove);
Width is window, height is window
Swp_nomove indicates that the location (x, y) is ignored.
Such as:

to center the window
Here are two ways to choose one:
① in the InitInstance () function of the application class (Cxxxapp):
② is added in the OnCreate () function of the main frame class (MainFrm.cpp):
CenterWindow (GetDesktopWindow ());
Such as:How to modify the window caption
The general form of the window caption is: Document title-Program title
1, set the document title:
Add a statement in the OnNewDocument () function of the document class (Cxxxdoc): Settitle ("document name");
Such as: Texteditordoc.cpp:① can delete the Debug folder and the Release folder;
② in principle you can also delete all files in the home folder that are icons, including. APS,. NCB,. Opt,. PLG, and so on, all of which can be rebuilt at compile time. But generally. CLW do not delete, it may cause classwizard bad use.



Control
How to hide and show controls
A 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 controls

enable and disable of buttons
Use ClassWizard's member variables to define variables for the button, such as: M_button1;
The
M_button1.enablewindow (TRUE); Leave the button in the allowed state
M_button1.enablewindow (FALSE); Causes the button to be suppressed and dimmed to display

change the size and position of a control
The function MoveWindow () or SetWindowPos () of the CWnd class can 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 should give the new coordinates and width and height of the control.
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 a height of 100 in the upper-left corner of the window
The SetWindowPos () function is more flexible and is used in situations where the position of the control is changed only if the size is unchanged or only the size is changed:
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 Common 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)
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), the position is unchanged
PWnd = GetDlgItem (IDC_EDIT1);
Pwnd->setwindowpos (Null,0,0,100,80,swp_nozorder); The size and position of the edit control are changed
The above methods also apply to various windows.

radio Button control (Radio Button) is used
First, the radio buttons are grouped:
The first radio button of each group sets the property: Group,tabstop,auto; the rest of the buttons set the property Tabstop,auto.
Such as:
Radio1, Radio2, Radio3 as a group, Radio4, Radio5 as a group
Set Radio1 property: Group,tabstop,auto
Set Radio2 property: Tabstop,auto
Set Radio3 property: Tabstop,auto
Set Radio4 property: Group,tabstop,auto
Set Radio5 property: Tabstop,auto
Second, use ClassWizard to define variables for the radio control, each group can only define one. such as: M_radio1, M_radio4.
C. Use ClassWizard to generate a click message function for each radio button and add content:
void Cweditview::onradio1 ()
{
M_radio1 = 0; The first radio button is selected
}
void Cweditview::onradio2 ()
{
M_radio1 = 1; A second radio button is selected
}
void Cweditview::onradio3 ()
{
M_radio1 = 2; A third radio button is selected
}
void Cweditview::onradio4 ()
{
M_radio4 = 0; A fourth radio button is selected
}
void Cweditview::onradio5 ()
{
M_radio4 = 1; A fifth radio button is selected
}
When the control variable has a value of 0 o'clock, it corresponds to the first radio button of the group that is selected.

BOOL cdzyapp::initinstance ()
... {
AfxEnableControlContainer ();
......

The one and only window have been initialized, so show and update it.
M_pmainwnd->setwindowpos (null,0,0,750,555,swp_nomove);//Set the initial size of the window to 750*555
M_pmainwnd->showwindow (Sw_show);
M_pmainwnd->updatewindow ();

return TRUE;
}

M_pmainwnd->centerwindow (GetDesktopWindow ());

int CMainFrame::OnCreate (lpcreatestruct lpcreatestruct) {
if (cframewnd::oncreate (lpcreatestruct) = =-1)
return-1;
......

Todo:delete These three lines if you don ' t want the toolbar to
Be dockable
M_wndtoolbar.enabledocking (Cbrs_align_any);
EnableDocking (Cbrs_align_any);
DockControlBar (&m_wndtoolbar);

CenterWindow (GetDesktopWindow ()); In the middle of the screen when the window is opened

return 0;
}

BOOL ctexteditordoc::onnewdocument () {
if (! Cdocument::onnewdocument ())
return FALSE;
Todo:add reinitialization code here
(SDI documents would reuse this document)
Settitle ("not named. txt"); Set the document title
return TRUE;
}

2, set the program title:
Add a statement to the PreCreateWindow () function of the Framework class (CMainFrame): M_strtitle = _t ("program title");
such as: MainFrm.cpp:

BOOL CMainFrame::P Recreatewindow (createstruct& cs) {
if (! CFrameWnd::P Recreatewindow (CS))
return FALSE;
Todo:modify the Window class or styles here by modifying
The CREATESTRUCT CS
cs.style&=~fws_addtotitle;//Remove "Untitled" in front of title bar text
M_strtitle = _t ("text collation"); Set Program title
return TRUE;
}

The above two points compare to the program for view-document structure, when you create a new document, the system automatically runs the OnNewDocument () function, where you can set the appropriate caption. For programs that do not take a document, you can modify the title in the following ways:
3, modify the window caption:
Modifying the window caption is generally done in open file function OnFileOpen () and Save As Function OnFileSaveAs (), you can use the following function: where the document title and program title can use the defined string variables.



Project
How to delete a class cleanly?
1. Delete the corresponding. h and. cpp files in the project first, (delete with delete after selection)
2. After saving, exit the project and delete the actual. h and. cpp files into the folder;
3. Delete the. clw file;
4, re-enter the project, to complete the reconstruction (rebuild all).

How to create a new class?
    Select New Class from the Insert menu, select the base class in the Pop-up dialog box, and enter the name of the new class in name (usually starting with C).
If you want to create a custom class that does not have a base class, set the class type to generic in the New Class dialog box, and then type the name.

How do I add foreign files to my project?
Copy the foreign file into the current project directory, and from the Project menu, select the Files menu item under Add to project, and open the foreign file from the pop-up file dialog box.

How do I open multiple projects in one workspace?
    The general programmer has this experience: To do a project, because of dissatisfaction, want to redo from scratch, but also want to copy some of the old project to the new project to avoid duplication of work, then you need to open the old project in the new project.
Open the new project, and from the Project menu, choose Insert Project to workspace (insert projects into Workspace) to open the. asp file for the old project from the Open File dialog box that pops up.
You can then switch between open projects with the option to set active project (select Active projects) under the Project menu.
Note: Items that are open in one workspace cannot have the same name.

How do I categorize the files in my project?
When we add a new class to the project, it places the source file under the sources files, and the header file is placed under the header files. When a lot of files in the project, management inconvenience, it is best to add a new node, the file classification placement.
Right-click the root node of the project node tree, select "New Folder ...", in the Popup dialog box to fill in the new node name, then the new node is established, with the mouse node tree files dragged into the new node, you can classify the file.
The above classification is only categorized in the project's node tree, it does not affect the location of the file on disk, all. cpp files and. h files are still in the root directory of the project, and the best files themselves can be categorized in different folders.
Under Windows, use "New Folder" to create a subfolder under the root of the project, such as Dialogclass, to drag all of the dialog class's. cpp files and. h files into it.
Back to VC, right-click on the project tree to change the path of the node, select "Properties", in the popup dialog to modify the file path, such as: the original path "./dialog1.cpp" instead of "./dialogclass/dialog1.cpp".
Open the Dialog1.cpp file and modify the file path it contains. Such as:
#include "stdafx.h"
#include "PluckBox.h"
#include "Dialog1.h"
Switch
#include "stdafx.h"
#include ". PluckBox.h "
#include "Dialog1.h"
Open ClassWizard, it will prompt you that the file does not exist, click "OK", from the dialog box "Browse ..." to select the path of the file, the ClassWizard can also be used normally.



Edit
What do I do when I edit my code and follow the prompts?
Click the Options menu item in the Tools menu, select the Editor tab page in the Pop-up Options dialog box, and select the bottom four check boxes (auto list member, auto type info, Code Comments, Auto parameter info), so that when the user enters "--" or "." , the following prompts are automatically displayed, reducing the input burden.



dialog box
How to modify the background color of a dialog box
Add the following statement to the OnPaint () function of the dialog box:
CRect rect;
GetClientRect (&rect); Calculate the size of the dialog box
dc. Fillsolidrect (&rect,rgb (192,248,202)); Draw dialog box background color

How to make pop-up dialogs have a uniform background color
Add the following statement to the InitInstance () function of the application class Cxxxapp:
SetDialogBkColor (RGB (192,248,202));
All user-defined pop-up dialogs are colored in RGB (192,248,202) and do not need to be set individually.


How to open a file dialog box to make multiple selections
When customizing the Open File dialog box, add Ofn_allowmultiselect

MFC about Windows operations.

Related Article

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.