How to create a window and paints a specified rectangle using selected brush with C++ Win32 API

1 Answer

0 votes
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int CALLBACK WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR     lpCmdLine,
	int       nCmdShow) {

	const wchar_t szClassName[] = L"directx11";

	WNDCLASSEX wc = { };
	wc.cbSize = sizeof(wc);
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.hInstance = hInstance;
	wc.lpszClassName = szClassName;

	if (!RegisterClassEx(&wc)) {
		MessageBox(NULL, L"RegisterClassEx Error", L"Title", MB_ICONEXCLAMATION | MB_OK);
		return 1;
	}

	HWND hWnd = CreateWindowEx(
		0, szClassName, L"Window Name",
		WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU | WS_MINIMIZEBOX,
		300, 300, 640, 480,
		NULL, NULL, hInstance, NULL);

	if (!hWnd) {
		MessageBox(NULL, L"hWnd Error", L"Title", MB_ICONEXCLAMATION | MB_OK);
		return 1;
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG msg = { 0 };
	BOOL result;
	while ((result = GetMessage(&msg, NULL, 0, 0))) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	if (result == -1)
		return -1;

	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
		case WM_CLOSE: {
			PostQuitMessage(69);
			break;
		}
		case WM_PAINT: {
			PAINTSTRUCT Paint;
			HDC hdc = BeginPaint(hWnd, &Paint);
			int x = Paint.rcPaint.left;
			int y = Paint.rcPaint.top;
			int width = Paint.rcPaint.right - Paint.rcPaint.left;
			int height = Paint.rcPaint.bottom - Paint.rcPaint.top;
			PatBlt(hdc, x, y, width, height, BLACKNESS);
			EndPaint(hWnd, &Paint);
			break;
		}
		default: {
			//OutputDebugStringA("default\n");
			break;
		}
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}






/*
run:


*/

 



answered Mar 10, 2023 by avibootz
...