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:
Hwnd findwindow (
Maid,
Lptstr lpwindowname
);
We can search by the window class name (lpclassname), or by the window title text (lpwindowname), or at the same time. 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 and right-click the root node (for example, "xxxxxx. RC), select properties, and check "enable MFC features" (if it is. net, change the "MFC mode property" item 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:
Idd_limitdlginstance_dialog dialogex 0, 0,195, 44
Style ds_modalframe | ws_popup | ws_visible | ws_caption | ws_sysmenu
Exstyle ws_ex_appwindow
Caption "limitdlginstance"
Class "myprivateclassname" // Add your class name here!
Font 8, "Ms sans serif"
Begin
Defpushbutton "OK", idok, 138, 7, 50, 14
Pushbutton "cancel", idcancel, 138-23, 50,14
Pushbutton "& test! ", Idc_button1, 48, 14, 49,15
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:
Wndclass WC;
// Obtain window class information. The name of the window class for all the default dialogs in MFC is #32770
: Getclassinfo (AfxGetInstanceHandle (), _ T ("#32770"), & WC );
// Change the window class name
WC. lpszclassname = _ T ("myprivateclassname ");
// Register a new window class so that the program can use it
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!
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/flyoxs/archive/2008/08/22/2812625.aspx