MFC modifies the main window style and class.

Source: Internet
Author: User

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 ?
  1. TypedefStructTagcreatestruct {
  2. LpvoidLpcreateparams;// Create basic parameters for the window
  3. HandleHinstance;// Has a module instance handle for the window to be created
  4. HmenuHmenu;// Menu handle of the new window
  5. HwndHwndparent;// The parent window handle of the new window
  6. IntCy;// The height of the new window
  7. IntCX;// The width of the new window
  8. IntY;// Y coordinate in the upper left corner of the new window
  9. IntX;// X coordinate in the upper left corner of the new window
  10. LongStyle;// New window style
  11. LpcstrLpszname;// New window name
  12. LpcstrLpszclass;// Name of the new window class
  13. DWORDDwexstyle;// New window extension Parameters
  14. } 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 ?
  1. BoolCmainframe: precreatewindow (createstruct & CS)
  2. {
  3. // Todo: Modify the window class or styles here by modifying
  4. // The createstruct CS
  5. // Modify the main window style
  6. CS. Style & = ~ Fws_addtotitle;// Remove the document name from the title bar
  7. CS. Style & = ~ Ws_thickframe;// Remove the border that can be changed
  8. CS. Style | = ws_dlgframe;// Add a border that cannot be changed
  9. // Determine the size and initial position of the Main Window
  10. IntCxscreen =: getsystemmetrics (sm_cxscreen );// Obtain the screen width
  11. IntCyscreen =: getsystemmetrics (sm_cyscreen );// Obtain the screen height
  12. CS. x = 0;// The main window is located in the upper left corner.
  13. CS. Y = 0;
  14. CS. Cx = cxscreen/2;// The width of the main window is 1/2. The screen width
  15. CS. Cy = cxscreen/2;// The height of the main window is 1/2.
  16. ReturnCmdiframewnd: precreatewindow (CS );
  17. }
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 ?
  1. BoolCmainwindow: precreatewindow (createstruct & CS)
  2. {
  3. // Todo: Add dedicated code and/or call the base class here
  4. If(Cframewnd: precreatewindow (CS ))
  5. {
  6. // Change the window class
  7. Wndclass;
  8. : Getclassinfo (AfxGetInstanceHandle (), CS. lpszclass, & wndclass );
  9. // Wndclass. hbrbackground = (hbrush) (color_3dface + 1 );
  10. // Wndclass. hbrbackground = (hbrush) getstockobject (black_brush );
  11. Wndclass. hbrbackground = createsolidbrush (RGB (0,100,100 ));
  12. Wndclass. hbrbackground = m_bkbrush;// M_bkbrush cannot be a local variable of the Function
  13. Wndclass. hbrbackground = *(NewCbrush (RGB (25, 25, 0 )));// The most convenient method
  14. // Wndclass. hcursor = afxgetapp ()-> loadcursor (idc_cursor1 );
  15. Wndclass. lpszclassname = _ T ("Newviewclassname");
  16. Verify (afxregisterclass (& wndclass ));
  17. CS. lpszclass = wndclass. lpszclassname;
  18. ReturnTrue;
  19. }
  20. ReturnFalse;
  21. }
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 ?
  1. BoolCMFC _ single document view: precreatewindow (createstruct & CS)
  2. {
  3. Wndclass;
  4. // 1. Call the function of the same name of the base class to automatically generate a window class for modification.
  5. Cview: precreatewindow (CS );
  6. // 2. Obtain the window class
  7. : Getclassinfo (AfxGetInstanceHandle (), CS. lpszclass, & wndclass );
  8. // 3. Modify the window class
  9. Wndclass. hbrbackground = createsolidbrush (RGB (0,100,100 ));
  10. Outputdebugstring (wndclass. lpszclassname );
  11. // 4. Update window class
  12. // Unregister the window class automatically generated by MFC (because the names of the new and old window classes are the same)
  13. : Unregisterclass (CS. lpszclass, AfxGetInstanceHandle ());
  14. // Register the modified window class
  15. Afxregisterclass (& wndclass );
  16. // Because the names of the new and old window classes are the same, you do not need to execute the following code:
  17. // CS. lpszclass = wndclass. lpszclassname;
  18. // Note that true is returned.
  19. ReturnTrue;
  20. }

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.