Application Generated by AppwizardProgramThe main window of the framework has the default window style, such as automatically adding the document name in the window title bar, overlapping window, and changing the window size. To modify the default window style, you must reload the cwnd: precreatewindow (createstruct & CS) function and modify the createstruct parameter CS.
The cwnd: precreatewindow function is executed before the window creation function. If the function is overloaded, the window creation function uses the window style defined by the createstruct CS parameter returned by the cwnd: precreatewindow function to create the window. Otherwise, the predefined window style is used.
The createstruct structure defines the initial parameters used to create the function creation window. The definitions are as follows:
[CPP]
View plain
Copy
Print
?
-
- TypedefStructTagcreatestruct {
- LpvoidLpcreateparams;// Create basic parameters for the window
-
- HandleHinstance;// Has a module instance handle for the window to be created
-
- HmenuHmenu;// Menu handle of the new window
-
- HwndHwndparent;// The parent window handle of the new window
- IntCy;// The height of the new window
-
- IntCX;// The width of the new window
-
- IntY;// Y coordinate in the upper left corner of the new window
-
- IntX;// X coordinate in the upper left corner of the new window
- LongStyle;// New window style
-
- LpcstrLpszname;// New window name
-
- LpcstrLpszclass;// Name of the new window class
-
- DWORDDwexstyle;// New window extension Parameters
- } Createstruct;
Typedef struct tagcreatestruct {lpvoid lpcreateparams; // handle hinstance, which is the basic parameter for creating a window; // hmenu, which has the module instance handle for the created window; // New Window menu handle hwnd hwndparent; // new window parent window handle int Cy; // The height of the new window int CX; // The width of the new window int y; // y coordinate int X in the upper left corner of the new window; // long style in the upper left corner of the new window; // new window class name DWORD dwexstyle; // new window extension parameter} createstruct;
The style field of the createstruct structure defines the style of the window. For example, the style of the default MDI Main Window includes fws_addtotitle.CompositionFile name), fws_prefixtitle (put the file name in front of the program title), ws_thickframe (the window has a scalable border) and other styles. Because multiple style parameters are combined by logic or ("|"), to add a style, you only need to use "|" to add the corresponding parameters to the style domain of the createstruct structure; to delete an existing style, you need to connect the style field of the createstruct structure with the logical non-value of the style.
The X, Y, CX, and Cy fields of the createstruct structure define the initial position and size of the window respectively. Therefore, assign values to them in the cwnd: precreatewindow function, the initial display position and size of the window can be defined.
In the following exampleCodeThe size of the window in the main box is fixed to 1/4. Only the window name is displayed in the title bar, but the document name is not displayed.
[CPP]
View plain
Copy
Print
?
-
- BoolCmainframe: precreatewindow (createstruct & CS)
-
- {
- // Todo: Modify the window class or styles here by modifying
-
- // The createstruct CS
-
-
- // Modify the main window style
-
- CS. Style & = ~ Fws_addtotitle;// Remove the document name from the title bar
- CS. Style & = ~ Ws_thickframe;// Remove the border that can be changed
-
- CS. Style | = ws_dlgframe;// Add a border that cannot be changed
-
-
- // Determine the size and initial position of the Main Window
-
- IntCxscreen =: getsystemmetrics (sm_cxscreen );// Obtain the screen width
- IntCyscreen =: getsystemmetrics (sm_cyscreen );// Obtain the screen height
-
- CS. x = 0;// The main window is located in the upper left corner.
-
- CS. Y = 0;
-
- CS. Cx = cxscreen/2;// The width of the main window is 1/2. The screen width
- CS. Cy = cxscreen/2;// The height of the main window is 1/2.
-
- ReturnCmdiframewnd: precreatewindow (CS );
-
- }
Bool cmainframe: precreatewindow (createstruct & CS) {// todo: Modify the window class or styles here by modifying // The createstruct CS // modify the main window style CS. style & = ~ Fws_addtotitle; // remove the document name CS. Style in the title bar ~ Ws_thickframe; // remove the CS. style | = ws_dlgframe; // Add a border that cannot be changed. // determine the size and initial position of the main window. Int cxscreen =: getsystemmetrics (sm_cxscreen ); // obtain the screen width int cyscreen =: getsystemmetrics (sm_cyscreen); // obtain the screen height CS. X = 0; // The main window is located in the upper left corner of the CS. y = 0; CS. cx = cxscreen/2; // The main window width is 1/2. The screen width is CS. cy = cxscreen/2; // The height of the main window is 1/2. Return cmdiframewnd: precreatewindow (CS );}
Modify the window class to change the background color.
Method 1:
[CPP]
View plain
Copy
Print
?
- BoolCmainwindow: precreatewindow (createstruct & CS)
-
- {
-
- // Todo: Add dedicated code and/or call the base class here
-
- If(Cframewnd: precreatewindow (CS ))
-
- {
-
- // Change the window class
-
- Wndclass;
-
- : Getclassinfo (AfxGetInstanceHandle (), CS. lpszclass, & wndclass );
-
- // Wndclass. hbrbackground = (hbrush) (color_3dface + 1 );
-
- // Wndclass. hbrbackground = (hbrush) getstockobject (black_brush );
-
- Wndclass. hbrbackground = createsolidbrush (RGB (0,100,100 ));
- Wndclass. hbrbackground = m_bkbrush;// M_bkbrush cannot be a local variable of the Function
-
- Wndclass. hbrbackground = *(NewCbrush (RGB (25, 25, 0 )));// The most convenient method
-
- // Wndclass. hcursor = afxgetapp ()-> loadcursor (idc_cursor1 );
-
- Wndclass. lpszclassname = _ T ("Newviewclassname");
- Verify (afxregisterclass (& wndclass ));
-
- CS. lpszclass = wndclass. lpszclassname;
-
- ReturnTrue;
-
- }
-
- ReturnFalse;
-
- }
Bool cmainwindow: precreatewindow (createstruct & CS) {// todo: Add dedicated code and/or call the base class if (cframewnd: precreatewindow (CS )) {// change the window class wndclass;: getclassinfo (AfxGetInstanceHandle (), CS. lpszclass, & wndclass); // wndclass. hbrbackground = (hbrush) (color_3dface + 1); // wndclass. hbrbackground = (hbrush) getstockobject (black_brush); wndclass. hbrbackground = createsolidbrush (RGB (0,100,100); wndclass. hbrbackground = m_bkbrush; // m_bkbrush cannot be a local variable of the function wndclass. hbrbackground = * (New cbrush (RGB (25, 25, 0); // the most convenient method // wndclass. hcursor = afxgetapp ()-> loadcursor (idc_cursor1); wndclass. lpszclassname = _ T ("newviewclassname"); Verify (afxregisterclass (& wndclass); CS. lpszclass = wndclass. lpszclassname; return true;} return false ;}
Method 2:
[CPP]
View plain
Copy
Print
?
- BoolCMFC _ single document view: precreatewindow (createstruct & CS)
-
- {
-
- Wndclass;
-
-
- // 1. Call the function of the same name of the base class to automatically generate a window class for modification.
-
- Cview: precreatewindow (CS );
-
-
- // 2. Obtain the window class
-
- : Getclassinfo (AfxGetInstanceHandle (), CS. lpszclass, & wndclass );
-
- // 3. Modify the window class
-
- Wndclass. hbrbackground = createsolidbrush (RGB (0,100,100 ));
-
- Outputdebugstring (wndclass. lpszclassname );
-
-
- // 4. Update window class
-
- // Unregister the window class automatically generated by MFC (because the names of the new and old window classes are the same)
-
- : Unregisterclass (CS. lpszclass, AfxGetInstanceHandle ());
- // Register the modified window class
-
- Afxregisterclass (& wndclass );
-
-
- // Because the names of the new and old window classes are the same, you do not need to execute the following code:
-
- // CS. lpszclass = wndclass. lpszclassname;
-
-
- // Note that true is returned.
-
- ReturnTrue;
-
- }