Text Formatting Example

//Font Formatting Example//
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("Text Output"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 200, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT Ps;
HDC hdc;
SIZE size;
TCHAR str[100]=TEXT("\0");
HFONT font;
HBRUSH hBrush;
TEXTMETRIC tm;
int y=0;
int x=0;
int textlength;
switch (msg)
{
case WM_PAINT://traps paint message

//output text using default font
hdc = BeginPaint(hwnd, &Ps);
int hDCLast;
hDCLast=SaveDC(hdc);//save current device context
textlength=wsprintf(str,TEXT("Stock font"));
TextOut(hdc,0,y,str,textlength);

//select ANSI_FIXED_FONT and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;//calculate new vertical coordinate using text metric
hBrush=(HBRUSH)GetStockObject(ANSI_FIXED_FONT);
SelectObject(hdc,hBrush);
textlength=wsprintf(str,TEXT("ANSI_FIXED_FONT") );
TextOut(hdc,0,y,str,textlength);

//set text to colour red and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
COLORREF newtextcolour;
newtextcolour=COLORREF RGB(255, 0, 0);
SetTextColor(hdc,newtextcolour);
textlength=wsprintf(str,TEXT("ANSI_FIXED_FONT, colour red") );
TextOut(hdc,x,y,str,textlength);

//set text background to colour blue and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
COLORREF newbkcolour;
newbkcolour=COLORREF RGB(0, 0, 255);
SetBkColor(hdc,newbkcolour);
textlength=wsprintf(str, TEXT("ANSI_FIXED_FONT, colour red with blue background") );
TextOut(hdc,x,y,str,textlength);

//set text background to transparent and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
SetBkMode(hdc,TRANSPARENT);
SelectObject(hdc,hBrush);
textlength=wsprintf(str,TEXT("ANSI_FIXED_FONT,colour red transparent background") );
TextOut(hdc,0,y,str,textlength);

//select font Ariel 20 and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
font = CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Ariel"));
SelectObject(hdc,font);
textlength=wsprintf(str,TEXT("ANSI_FIXED_FONT, colour red, transparent background,arial 20") );
TextOut(hdc,0,y,str,textlength);

//restore default font using saved value and output text
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
RestoreDC(hdc,hDCLast);
textlength=wsprintf(str,TEXT("stock font restored using SaveDC/RestoreDC") );
TextOut(hdc,x,y,str,textlength);
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;//calculate new vertical coordinate using text metric

//select font times new romans 30 and output text
int baseline;//used to caculate font baseline
font = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Times New Roman"));
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baseline=tm.tmAscent;
textlength=wsprintf(str,TEXT("times new roman 30 statement 1") );
TextOut(hdc,x,y,str,textlength);
GetTextExtentPoint32(hdc,str,lstrlen(str),&size);
x=x+size.cx;

//select font courier new 20 and output text
int baselinecourier;
font = CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Courier New"));
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baselinecourier=baseline-tm.tmAscent;
textlength=wsprintf(str, TEXT("Courier statement ") );
TextOut(hdc,x,y+baselinecourier,str,textlength);
GetTextExtentPoint32(hdc,str,lstrlen(str),&size);
x=x+size.cx;

//select font ariel 20 and output text
int baselineariel;
font = CreateFont(10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,TEXT( "Ariel"));
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baselineariel=baseline-tm.tmAscent;
textlength=wsprintf(str, TEXT("ariel 10 statement 3 ") );
TextOut(hdc,x,y+baselineariel,str,textlength);

EndPaint(hwnd, &Ps);
DeleteDC(hdc);
DeleteObject(font);
DeleteObject(hBrush);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}