Customizing the Personalized dialog box window class

Source: Internet
Author: User

Believe that a lot of people in VC development program when most of the development based on the dialog box, I am no exception, most of the small test program does not need to develop a document/view based structure to test, as long as the use of some Basic dialog box program can achieve this goal.

However, when developing dialogs based programs, some problems will arise when using the function detection of some Spy + +. What's the problem? When I use Spy + + to detect the window Class of a dialog box, and want a dialog window class so that I can specify a dialog box to hook when I use the hook, but the result is unexpected, the window Class of the dialog box is not the dialog window class name I specified when I registered. Its class name is "#32770 (Dialog)", which is an MFC automatically generated by the dialog box based on the default window class, all MFC based on the resulting dialog box program uses this default class name. In other words, when I use a dialog based program, no matter how many dialog boxes are generated, their class name will be "#32770 (Dialog)" So that I cannot specify the handle to the dialog box I need when I open the dialog box to test.

However, when you specify a window caption for a dialog box, the name handle of the dialog box can be found:

HWND hWnd = MULL;
hWnd = FindWindow( "#32770",lpszWindowName );
_ASSERT( hWnd != NULL );
//其中 lpszWindowName 是对话框的窗口标题目。

This method also has a certain disadvantage, is a dialog box of the title is uncertain, or the title of the dialog box in the running process to dynamically change it? There is no guarantee that the found handle is the handle that is required. My approach is to specify a unique window class for the dialog box when the dialog box is generated, so that you can find the specified handle you want without confusing the dialog box with it.

HWND hWnd = MULL;
hWnd = FindWindow( lpszClassName, NULL );
_ASSERT( hWnd != NULL );
//其中 lpszClassName 是对话框的窗口类名。

So how do you implement your own custom dialog box classes! Read the "Simple and simple MFC" readers will think that in the overloaded CWinApp InitInstance () function to modify, yes, it is true to modify here.

// 在派生类的 InitIntace() 中
  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 registered window when the registration window is generated. Re-register the window class, everything seems to be smooth, not very difficult operation, but everything is as you expected. It's not easy, when you open Spy + + observation window, still "#32770 (Dialog)".

Well, do you have any other options? MSDN is also the most useful at this time, with the lack of MSDN as in a boat without oars, there are two ways in MSDN that allow us to customize our dialog window classes.

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.