Create Window without Registering a WNDCLASS?

前端 未结 2 1297
走了就别回头了
走了就别回头了 2020-12-11 06:39

Is it absolutely necessary to always build and register a new WNDCLASS(EX) for your application? And then use the lpszClassName for the main window?

Isn\'t there som

相关标签:
2条回答
  • 2020-12-11 07:08

    There are no pre-defined window classes for top-level application windows. You must register a window class for your application, or use a dialog.

    0 讨论(0)
  • 2020-12-11 07:17

    You can create a mini app out of a dialog resource, you use CreateDialog() instead of CreateWindow(). Boilerplate code could look like this, minus the required error checking:

    #include "stdafx.h"
    #include "resource.h"
    
    INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_INITDIALOG: 
            return (INT_PTR)TRUE;
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
                DestroyWindow(hDlg);
                PostQuitMessage(LOWORD(wParam)-1);
                return (INT_PTR)TRUE;
            }
            break;
        }
        return (INT_PTR)FALSE;
    }
    
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
        HWND hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
        if (hWnd == NULL) DebugBreak();
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int) msg.wParam;
    }
    

    Which assumes you created a dialog with the resource editor using id IDD_DIALOG1.

    0 讨论(0)
提交回复
热议问题