No text appears on the screen with CreateFont() and DrawFont()?
Hello, to learn about Windows GUI objects, I have written the below code. Another main reason I have written it is to hopefully be able to have access to the API to make it easier to draw text on my SDL GUI's client window, since it should work as long as I own the process. In any case, my code displays absolutely nothing. It also receives no error messages, so I'm not sure what I got wrong in my window procedure. It seems to also be a little misbehaved on Windows XP, because although it handles the escape key message, it doesn't exit right away on Windows XP. It doesn't have this problem on Windows 7. Currently, no text appears on the screen and it is just plain black. I hope someone knows more about how to format GDI objects than I do, because I based my setup of my text object off of Microsoft's example.
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.