Question

So, I am a total beginner in any kind of Windows related programming. I have been playing around with the Windows API and came across a couple of examples on how to initialize create windows and such.

One example creates a regular window (I abbreviated some of the code):

int WINAPI WinMain( [...] )
{

    [...]

    // Windows Class setup
    wndClass.cbSize = sizeof( wndClass );
    wndClass.style  = CS_HREDRAW | CS_VREDRAW;
    [...]    

    // Register class
    RegisterClassEx( &wndClass );

    // Create window
    hWnd = CreateWindow( szAppName, "Win32 App",
                         WS_OVERLAPPEDWINDOW,
                         0, 0, 512, 384,
                         NULL, NULL, hInstance, NULL );
    [...]
}

The second example creates a dialog box (no abbreviations except the WinMain arguments):

int WINAPI WinMain( [...] )
{
    // Create dialog box
    DialogBox(hInstance, 
              MAKEINTRESOURCE(IDD_MAIN_DLG), 
              NULL, 
              (DLGPROC)DialogProc);
}

The second example does not contain any call to the register function. It just creates the DialogBox with its DialogProc process attached.

This works fine, but I am wondering if there is a benefit of registering the window class and then creating the dialog box (if this is at all possible).

Was it helpful?

Solution

You do not have to register a dialog box.

Dialog boxes are predefined so (as you noted) there is no reference to a window class when you create a dialog. If you want more control of a dialog (like you get when you create your own window class) you would subclass the dialog which is a method by which you replace the dialogs window procedure with your own. When your procedure is called you modify the behavior of the dialog window; you then might or might not call the original window procedure depending upon what you're trying to do.

OTHER TIPS

It's been a while since I've done this, but IIRC, the first case is for creating a dialog dynamically, from an in-memory template. The second example is for the far more common case of creating a dialog using a resource. The dynamic dialog stuff in Win32 was fairly complex, but it allowed you to create a true data-driven interface, and avoid issues with bundling resources with DLLs.

As for why use Win32 - if you need a windows app and you don't want to depend on MFC or the .NET runtime, then that's what you use.

This is only tangentially related to the question, but if you are new to Windows programming, why are you using Win32? Unless there's a lot of low-end code (which should be separate to the GUI anyway), it probably makes more sense to use .NET, which should cause far less head injury as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top