Customized dialog window in VC ++

xiaoxiao2021-04-10  537

I believe that many people will use the dialog-based development when developing procedures under VC. I am no exception. Most small test programs do not need to develop document / view-based structures to test, just use some basic dialogs. The program can achieve this purpose.

However, when developing a dialog-based program, some problems will occur when using some SPY features. What's the problem? When I use Spy to detect a window class of a dialog, I want to get a window class of a dialog so that I can specify a dialog for HOOK when using the hook, but the result is unexpected, dialog Window categories are not the dialog window class name that I am registered. Its class name is "# 32770 (dialog)", which is an MFC automatically generated by the default window class generated by the dialog-based program. All MFC-generated dialog boxes use this default class name. That is, when I use a dialog-based program, no matter how many dialogs are generated, their class name will be "# 32770 (dialog) so that I can't specify me when I open the dialog. The handle of the dialog.

However, when you specify a window title of a dialog, the handle of this dialog can be found:

HWND HWND = MULL;

HWnd = FindWindow ("# 32770", lpszwindowname);

_Assert (hwnd! = Null);

// Where lpszWindowname is the window title of the dialog.

This method also has a certain disadvantage, what is the title of a dialog, or when the title of the dialog is to change during the running process? This is not guaranteed to ensure that the handle found is the handle of the required. The method I take is to specify a unique window class for the dialog in the production process of the dialog, so you can find the specified handle you want without confusion with other dialogs.

HWND HWND = MULL;

HWnd = FindWindow (LPSZCLASSNAME, NULL);

_Assert (hwnd! = Null);

// where lpszclassname is the window class name of the dialog.

How to achieve self-customized dialog box class! Readers who have seen "deep-in-depth MFC" will definitely make changes in the InitInstance () function overloading CWINAPP, really want to modify here.

// In the initintacece () 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 much 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 to be smooth, nor is it very difficult, but everything is as expected. It's not Xin, when you open the SPY observation window, it is still "# 32770 (dialog)". Ok, have you have other ways? MSDN is still most useful at this time, lacking MSDN as there is no paddle, two methods provide two ways to customize their own dialog window.

The first:

1. Open this dialog project file to open ResourceView.

2. Open Resource Editor, right-click the dialog, select Option Properties, in the property item of the dialog, the lowermost corner is a class name, but this option is forbidden, you can't enter the name of the name, because You are here to choose the support of the MFC class library. In order to enable this option to enter. As shown, in the top of the resource view, select the right button -> attribute, will pop up

A Resource File Properties dialog box, set the items of the Enable MFC Features to disable, the class name of the dialog box can be set in the resource editor. (In Visual C .net, set the MFC Mode Property property to false)

The second alternative method is to modify the RC file and source code! Modify in the initInstance function of the CWINAPP derived class:

// In the initintacece () 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 much mfc can use it.

AfXRegisterClass (& WC);

// ......

}

Among them :: getClassInfo guarantees that even if your resource file can also get the Hinstance follows if you want to modify the resource file, open the RC file with the text editor, plus the "class name" as shown below:

Note that 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 tip, I hope that for everyone's development, for example, in the development of dialog-based programs, it is very useful to register only one run instance. Or when a program is monitored, it can be quickly positioned.

转载请注明原文地址:https://www.9cbs.com/read-133320.html

New Post(0)