Tips for using findwindow; solution: the process cannot be obtained by the lpclassname of findwindow.

Source: Internet
Author: User

Http://www.vckbase.com/document/viewdoc? Id = 1178

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)", so that when I open the dialog box for testing, I cannot specify the handle of the required dialog box.
However, when the window title of a dialog box is specified, the name handle of the dialog box can be found:

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 = 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:
1. Open the project file in the dialog box and open resourceview.
2. Open the resource editor, right-click the dialog box, and select properties. In the attribute items of the dialog box, the most important option is a class name input item, but this option is not allowed, you cannot enter the class name here, because you have selected

Yes. To enable this option ., Right-click the top item of the resource view and choose Properties.
In a resource file Properties dialog box, set the Enable MFC features item to disabled, then 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)

The second 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.

Conclusion:
This is just a small trick and I hope it will be helpful for everyone's development. For example, in the development of a dialog box-based program, it is very useful to register a unique window class so that the application has only one running instance. You can also perform quick locating when performing program monitoring.

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.