Skip to content

Instantly share code, notes, and snippets.

@doccaico
Created July 22, 2024 04:20
Show Gist options
  • Save doccaico/61414050059591539299a756a1684166 to your computer and use it in GitHub Desktop.
Save doccaico/61414050059591539299a756a1684166 to your computer and use it in GitHub Desktop.
Create a window (Windows API)
#ifndef UNICODE
#define UNICODE
#endif
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// types
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long long s64;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HPEN hpen;
static HBRUSH hbr;
static HBRUSH hbr_bg;
switch (uMsg)
{
case WM_CREATE:
// hpen = (HPEN) GetStockObject(BLACK_PEN);
hpen = (HPEN) GetStockObject(NULL_PEN);
hbr = (HBRUSH) GetStockObject(GRAY_BRUSH);
// hbr_bg = CreateSolidBrush(RGB(255, 255, 255));
hbr_bg = CreateSolidBrush(RGB(0, 0, 0));
return 0;
case WM_DESTROY:
DeleteObject(hpen);
DeleteObject(hbr);
DeleteObject(hbr_bg);
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
RECT rect;
GetClientRect(hwnd, &rect);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SaveDC(hdc);
SelectObject(hdc, hpen);
SelectObject(hdc, hbr);
// FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+4));
// FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_BLACK));
FillRect(hdc, &ps.rcPaint, hbr_bg);
// All painting occurs here, between BeginPaint and EndPaint.
Rectangle(hdc,
0, 0,
10, 10);
// Rectangle(hdc,
// 0, 20,
// 20, 20);
// Rectangle(hdc,
// rect.right / 4 - 10, rect.bottom / 4 - 10,
// rect.right / 4 + 10, rect.bottom / 4 + 10);
RestoreDC(hdc, -1);
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
#!/bin/sh
# ./make.sh [debug, release]
if [ $# -lt 1 ]; then
echo "need a parameter [debug, release]"
exit 0
fi
if [ $1 = "debug" ]; then
OPTIMIZATION="-O0"
HIDE_CONSOLE=""
elif [ $1 = "release" ]; then
OPTIMIZATION="-O2"
HIDE_CONSOLE="-mwindows"
else
echo "unknown parameter: $1"
exit 0
fi
# clear
C_FLAGS="-std=c99 ${OPTIMIZATION} -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-missing-field-initializers"
COMPILER='gcc'
COMMAND="${COMPILER} ${C_FLAGS} main.c -o main.exe -Wl,-subsystem,windows -municode -lgdi32"
${COMMAND}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment