Change the default window class name in the MFC dialog box

Source: Internet
Author: User
Tags custom name

Windows has a concept: window class. The window class is abstract. It can have many window instances, namely, hwnd/cwnd. In a program, you can customize and register a window class, and then use it to create a window. You can also use the registered window class to create a window.

To reduce the programming burden and hide specific implementations, MFC has already customized and registered many window classes for us. We can use them directly. For example, the window class of the dialog box is "#32770", the window class of the button is "button", and the window class of the list box is "ListBox "...... And so on.

Most of the time, we need to use the findwindow function to find an existing window and send a message to it. Or check whether a specified window (process) exists in the query system. If it exists, we will not create a new process of the program, but simply activate it. The findwindow function is declared as follows:

  1. Hwnd findwindow (
  2. Maid,
  3. Lptstr lpwindowname
  4. );

We can use the window class name (LpclassnameYou can also use the window title text (Lpwindowname. The title text of a window is not always very reliable, and often changes dynamically. Therefore, it is necessary to search by window class name.

Then, how can we change the default window class name "#32770" in the dialog box provided by MFC to our program? Follow these steps:

1. Open the project resource view

2. Change the classname attribute of the resources in the dialog box. There are two methods:

Method 1: Right-click the resource in the dialog box for Class Name Change and select "properties". In the displayed Properties dialog box, a property named "Class Name" is displayed, enter a custom name (for example, "myprivateclassname. However, the default setting of this attribute option in MFC is unavailable in gray, so we need to enable it first. Switch to the resource view, right-click the root node (for example, "xxxxxx. RC"), select properties, and select"Enable MFC features"(If it is vs. net, set"MFC mode property"To false ). In this way, you can modify the class name attribute of the dialog box.

Another method is to directly open the resource file in a text editor, modify the dialog box to define code, and insert class items, such:

  1. Idd_limitdlginstance_dialog dialogex 0, 0,195, 44
  2. Style ds_modalframe | ws_popup | ws_visible | ws_caption | ws_sysmenu
  3. Exstyle ws_ex_appwindow
  4. Caption "limitdlginstance"
  5. Class "myprivateclassname" // Add your class name here!
  6. Font 8, "Ms sans serif"
  7. Begin
  8. Defpushbutton "OK", idok, 138, 7, 50, 14
  9. Pushbutton "cancel", idcancel, 138-23, 50,14
  10. Pushbutton "& test! ", Idc_button1, 48, 14, 49,15
  11. End

3. Customize and register a new window class. Add the following code to the initinstance () function of the application class to register the new window class used in the resource file:

  1. Wndclass WC;
  2. // Obtain window class information. The name of the window class for all the default dialogs in MFC is #32770
  3. : Getclassinfo (AfxGetInstanceHandle (), _ T ("#32770"), & WC );
  4. // Change the window class name
  5. WC. lpszclassname = _ T ("Myprivateclassname");
  6. // Register a new window class so that the program can use it
  7. Afxregisterclass (& WC );

Note:

1. When calling the function: getclassinfo (), make sure that the first hinstance that is passed in contains the DLL or EXE process instance of the resources in your dialog box.

2. Make sure that the class name specified in the resource is the same as the window class name specified in initinstance (). Otherwise, the program cannot run.

 

The change is complete. Please re-build the project, run the program, and use Spy ++ to view the final result!

 

Reference: how to provide your own window class name for an MFC dialog box

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.