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.?
lpfnWndProc = WindowProc; wc. hInstance = hInstance; wc. lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window.
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.
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.
A window class defines the attributes of a window, such as its style, icon, cursor, menu, and window procedure.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With