Customized dialog box window

Source: Internet
Author: User

I believe that many developers mostly use dialog box-based development when developing programs under VC. I am no exception, most small test programs do not need to develop a document/View-based structure for testing. You only need to use some basic dialog box programs to achieve this purpose.

However, when developing a dialog box-based program, some Spy ++ functions should be used for detection, and some problems may occur. What's the problem? When I use Spy ++ to check the window class of a dialog box, I want to get the window class of a dialog box so that I can specify a dialog box for hook when using the hook, unexpectedly, the dialog box window class is not the name of the dialog box window class that I specified at registration. Its class name is "#32770 (Dialog)", which is a default window class automatically generated by MFC for Dialog box-based programs, all dialog programs generated based on MFC use this default class name. That is to say, when I use a Dialog box-based program, no matter how many dialogs are generated, their class names will be "#32770 (Dialog )", in this way, when I open the dialog box for testing, the handle of the dialog box that I need cannot be specified.

However, when the window title of a dialog box is specified, the name handle of the dialog box can be found:

HWND hWnd = MULL; hWnd = FindWindow ("#32770", lpszWindowName); _ ASSERT (hWnd! = NULL); // The lpszWindowName is the title of the dialog box.

This method also has some disadvantages, that is, what will happen when the title of a dialog box is uncertain, or the title of the dialog box needs to be dynamically changed during running? In this way, it cannot be ensured that the obtained handle is the required handle. The method I used is to specify a unique window class for the dialog box during the creation of the dialog box, so that you can find the desired specified handle without confusion with other dialog boxes.

HWND hWnd = MULL; hWnd = FindWindow (lpszClassName, NULL); _ ASSERT (hWnd! = NULL); // The lpszClassName is the name of the window class in the dialog box.

Then how can we implement the custom dialog box class! Readers who have read "let alone MFC" will surely think that it is good to modify it in the InitInstance () function of the reload CWinApp. It must be modified here.

// In the InitIntace () of the derived class, BOOL CLimitDlgInstanceApp: InitInstance () {WNDCLASS wc; // Get the info for this class. // #32770 is the default class name for dialogs boxes. : GetClassInfo (AfxGetInstanceHandle (), "#32770", & wc); // Change the name of the class. Wc. LpszClassName = "MyPrivateClassName"; // Register this class so that MFC can use it. AfxRegisterClass (& wc );//......}

The method used here is to modify the window class name of the registration window when the registration window is generated. Re-register the window class. Everything seems smooth and difficult, but is everything as expected. Very Uncomfortable. When you open the Spy ++ observation window, it is still "#32770 (Dialog )".

Okay. Do you have any other solutions? MSDN is the most useful at this time. If MSDN is missing, it is like a boat without a propeller. MSDN provides two methods for us to customize our own dialog box window class.

First
  • Open the project file in this dialog box and open resourceview.
  • Open resource editor, right-click the dialog box, and select properties. In the attribute items of the dialog box, the most important option is the input of a class name, but this option is not allowed, you cannot enter the class names, because you have selected the support of the MFC class library here. To enable this option .,

    Right-click the top item of the resource view and choose Properties. A resource file Properties dialog box is displayed, where enable MFC features is set to disabled, the Class Name of the dialog box can be set in the resource editor. (In Visual C ++. Net, set the MFC mode property to false)

Second

This method is to modify the RC file and source code! Modify it in the Initinstance function of the CWinApp derived class:

// In the initintace () of the derived class, bool climitdlginstanceapp: initinstance () {wndclass WC; // get the info for this class. // #32770 is the default class name for dialogs boxes. : Getclassinfo (AfxGetInstanceHandle (), "#32770", & WC); // change the name of the class. WC. Lpszclassname = "myprivateclassname"; // register this class so that MFC can use it. Afxregisterclass (& WC );//......}

Specifically: getclassinfo ensures that the hinstance is followed by modifying the resource file even if your resource files are in different DLL files. You can use a text editor to open the RC file, add the "Class Name" as shown in:

Note: If the class name in your RC file is inconsistent with the class name in initinstance, the program will not run, which is very important. Remember.

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.