1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <stdio.h>
#include <windows.h>
void PaintWindow( HWND hwnd )
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps );
SetPixel( hdc, 10, 10, RGB( 0, 0xFF, 0 ) ); // green
EndPaint( hwnd, &ps );
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch (msg)
{
case WM_KEYDOWN: // same as pressing the X button:
case WM_CLOSE: DestroyWindow( hwnd ); return 0;
case WM_DESTROY: PostQuitMessage( 0 ); return 0;
case WM_PAINT: PaintWindow( hwnd ); return 0;
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}
int main()
{
// Register window class
WNDCLASSA wc =
{
0, WndProc, 0, 0, 0,
LoadIcon( NULL, IDI_APPLICATION ),
LoadCursor( NULL, IDC_ARROW ),
GetStockObject( BLACK_BRUSH ), // background color == black
NULL, // no menu
"ExampleWindowClass"
};
ATOM wClass = RegisterClassA( &wc );
if (!wClass)
{
fprintf( stderr, "%s\n", "Couldn’t create Window Class" );
return 1;
}
// Create the window
HWND hwnd = CreateWindowA(
MAKEINTATOM( wClass ),
"Window sample", // window title
WS_OVERLAPPEDWINDOW, // title bar, thick borders, etc.
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL, // no parent window
NULL, // no menu
GetModuleHandle( NULL ), // EXE's HINSTANCE
NULL // no magic user data
);
if (!hwnd)
{
fprintf( stderr, "%ld\n", GetLastError() );
fprintf( stderr, "%s\n", "Failed to create Window" );
return 1;
}
// Make window visible
ShowWindow( hwnd, SW_SHOWNORMAL );
// Event loop
MSG msg;
while (GetMessage( &msg, NULL, 0, 0 ) > 0)
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
|