Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#include <stdint.h>
#define GUI_WIDTH 500
#define GUI_HEIGHT 750
#define BACKGROUND_COLOR RGB( 0, 0, 0 )
void win_error( char * message, BOOL exit )
{
char buffer[BUFSIZ] = { 0 };
DWORD error_code = GetLastError( );
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) buffer,
BUFSIZ,
NULL
);
MessageBox(NULL, buffer, "Error from System :", MB_ICONWARNING | MB_OK );
if ( exit ) ExitProcess( error_code );
return;
}
LRESULT CALLBACK window_procedure( HWND window_handle, UINT message, WPARAM wParam, LPARAM lParam )
{
static HDC window_device_context = NULL;
HDC drawing_context = NULL;
PAINTSTRUCT paint_struct = { 0 };
RECT window_region = { 0 };
RECT text_rect = { 0 };
HBRUSH brush_object = CreateSolidBrush( BACKGROUND_COLOR );
HFONT h_font = NULL;
switch( message )
{
case WM_CREATE :
if ( !( window_device_context = GetDC( window_handle ) ) )
win_error( "GetDC( )", TRUE );
GetWindowRect( window_handle, &window_region );
break;
case WM_PAINT :
drawing_context = BeginPaint( window_handle, &paint_struct );
h_font = CreateFont(
0,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
VARIABLE_PITCH | FF_DECORATIVE,
TEXT("Rockwell Ultra Bold")
);
if ( !h_font )
{
win_error( "CreateFont( )", FALSE );
PostQuitMessage( 0 );
}
text_rect = window_region;
SelectObject( drawing_context, h_font );
SetTextColor( drawing_context, RGB( 255, 255, 255 ) );
if ( !DrawText( drawing_context, TEXT("Hello Windows!"), -1, &text_rect, DT_CENTER ) )
{
win_error( "DrawText( )", FALSE );
PostQuitMessage( 0 );
}
DeleteObject( h_font );
h_font = NULL;
EndPaint( window_handle, &paint_struct );
break;
case WM_MOVE :
GetWindowRect( window_handle, &window_region );
UpdateWindow( window_handle );
break;
case WM_CLOSE :
DestroyWindow( window_handle );
break;
case WM_DESTROY :
ReleaseDC( window_handle, window_device_context );
DeleteObject( brush_object );
break;
case WM_KEYDOWN :
switch ( wParam )
{
case VK_ESCAPE :
PostQuitMessage( 0 );
break;
default :
printf( "Unhandled key message : %X\n", ( unsigned )wParam );
printf( "Character : %c\n\n", ( char )wParam );
break;
}
break;
case WM_LBUTTONDOWN :
printf( "Left mouse click.\n" );
break;
case WM_RBUTTONDOWN :
printf( "Right mouse click.\n" );
break;
default:
return DefWindowProc( window_handle, message, wParam, lParam );
break;
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdArgs, int nCmdShow )
{
WNDCLASSEX window_class;
HWND window_handle;
MSG message;
HBRUSH window_color = CreateSolidBrush( BACKGROUND_COLOR );
window_class.cbSize = sizeof( WNDCLASSEX );
window_class.style = 0;
window_class.lpfnWndProc = window_procedure;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = hInstance;
window_class.hIcon = LoadIcon( NULL, IDI_APPLICATION );
window_class.hCursor = LoadCursor( NULL, IDC_ARROW );
window_class.hbrBackground = window_color;
window_class.lpszMenuName = NULL;
window_class.lpszClassName = "NewClass";
window_class.hIconSm = NULL;
if( !RegisterClassEx( &window_class ) )
win_error( "RegisterClassEx( )", TRUE );
window_handle = CreateWindowEx
(
WS_EX_CLIENTEDGE,
"NewClass",
"Hello",
WS_TILEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
GUI_HEIGHT,
GUI_WIDTH,
NULL,
NULL,
hInstance,
NULL
);
if( !window_handle )
{
UnregisterClass( "NewClass", hInstance );
win_error( "CreateWindowEx( )", TRUE );
}
ShowWindow( window_handle, SW_SHOW );
UpdateWindow( window_handle );
while( GetMessage( &message, window_handle, 0, 0 ) > 0 )
{
TranslateMessage( &message );
DispatchMessage( &message );
}
UnregisterClass( "NewClass", hInstance );
return 0;
}
The desired effect is for the text "Hello Windows" to be displayed in white in the Rockwell Ultra Bold font on the center of the screen, with the optional escape key exiting immediately.