c Copy code
#include <windows.h>
/* Function declarations */
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void CreateControls(HWND hwnd);
/* Control ID constants */
#define IDC_HELLO_BUTTON 101
/* WinMain - application entry point */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char CLASS_NAME[] = "HelloWindowClass";
WNDCLASS wc;
HWND hwnd;
MSG msg;
/* Fill in the window class structure */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
if (!RegisterClass(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
/* Create the main window */
hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Hello World Button",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
/* Message loop */
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/* Window procedure */
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
CreateControls(hwnd);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_HELLO_BUTTON)
{
MessageBox(hwnd, "Hello World!", "Greeting", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
/* Create controls (button) */
void CreateControls(HWND hwnd)
{
CreateWindow("button", "Hello World!",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
90, 70, 120, 30,
hwnd,
(HMENU)IDC_HELLO_BUTTON,
GetModuleHandle(NULL),
NULL);
}
Let me know if you want a version that includes a menu, icon, or other retro-style elements!