VC tips summary of the window skills
- 2020-04-02 02:31:05
- OfStack
This paper collects and summarizes the window skills of VC tips, which have certain reference value for the window design of VC program development. Details are as follows:
Maximize the window as soon as it starts
Put the InitInstance() function in the application class (CxxxApp)
m_pMainWnd->ShowWindow(SW_SHOW);
Instead of
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
Maximizes the display as soon as the window starts.
2. How to set the initial size of the window
Add to the InitInstance() function of the application class (CxxAPP) :
m_pMainWnd->SetWindowPos(NULL,x,y,Width,Height,SWP_NOMOVE);
Width is the Width of the window and Height is the Height of the window
SWP_NOMOVE means ignore position (x,y).
Such as:
BOOL CDzyApp::InitInstance()
{
AfxEnableControlContainer();
...
// The one and only window has 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;
}
3. Center the window
Either of the following two methods is available:
Add in the InitInstance() function of the application class (CxxxApp) :
m_pMainWnd->CenterWindow( GetDesktopWindow() );
(2) in the main frame class (mainfrm.cpp) OnCreate() function add:
CenterWindow( GetDesktopWindow() );
Such as:
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() ); //Make the window open in the middle of the screen
return 0;
}
4. How do I change the window title
The window title generally takes the form of: document title - program title
(1) set the document title:
Add a statement to the OnNewDocument() function of the document class (CxxxDoc) : SetTitle(" document name ");
Such as: TextEditorDoc. CPP:
BOOL CTextEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
SetTitle(" unnamed .txt"); //Set the document title
return TRUE;
}
(2) set program title:
Add the statement m_strTitle = _T(" program title ") to the PreCreateWindow() function of the frame class (CMainFrame).
Such as: MainFrm. CPP:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
m_strTitle = _T(" Text organizer "); //Set program title
return TRUE;
}
These two points apply to view-document structures, where the OnNewDocument() function is automatically run when a new document is created, where the appropriate title can be set. For programs that are not documented, you can modify the title by:
(3) modify window title:
Modifying the window title is usually done in the open file function OnFileOpen() and save as the function OnFileSaveAs(). You can use the following functions:
AfxGetMainWnd()->SetWindowText(" Document title "+" - "+" Program title ");
Document titles and program titles can use defined string variables.