Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Window without Registering a WNDCLASS?

Tags:

c

windows

winapi

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 some prebuilt class name we can use for a main window, like there is "Button" and "Edit" for buttons and text-boxes etc.?

like image 444
ApprenticeHacker Avatar asked Apr 19 '12 16:04

ApprenticeHacker


People also ask

How do I create a new window in C++?

lpfnWndProc = WindowProc; wc. hInstance = hInstance; wc. lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window.

What is Hwnd C++?

A Windows window is identified by a "window handle" ( HWND ) and is created after the CWnd object is created by a call to the Create member function of class CWnd . The window may be destroyed either by a program call or by a user's action.

What is hInstance?

hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions—for example, to load icons or bitmaps.

What is created by window class?

A window class defines the attributes of a window, such as its style, icon, cursor, menu, and window procedure.


2 Answers

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.

like image 84
Hans Passant Avatar answered Oct 10 '22 11:10

Hans Passant


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.

like image 32
David Heffernan Avatar answered Oct 10 '22 11:10

David Heffernan