Text Formatting Example

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"WindowClass";////in modern C and C++ declare string literals as const

WNDCLASSEXW wc = {0};
MSG msg;

//Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEXW);
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 = GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassExW(&wc);

//Creating the Window
CreateWindowExW(WS_EX_CLIENTEDGE, CLASS_NAME, L"Textmetric Demo", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 220, NULL, NULL, hInstance, NULL);

//The Message Loop
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}


//WndProc procedure. Application acts on messages

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

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

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

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

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

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

//select font Ariel 20 and output L
GetTextMetrics(hdc,&tm);
y=y+tm.tmHeight;
font = CreateFontW(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Ariel");
SelectObject(hdc,font);
textlength=wsprintfW(str,L"ANSI_FIXED_FONT, colour red, transparent background,arial 20");
TextOutW(hdc,0,y,str,textlength);

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

//select font times new romans 30 and output L
int baseline;//used to caculate font baseline
font = CreateFontW(30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Times New Roman");
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baseline=tm.tmAscent;
textlength=wsprintfW(str,L"times new roman 30 statement 1");
TextOutW(hdc,x,y,str,textlength);
GetTextExtentPoint32W(hdc,str,lstrlenW(str),&size);
x=x+size.cx;

//select font courier new 20 and output L
int baselinecourier;
font = CreateFontW(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Courier New");
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baselinecourier=baseline-tm.tmAscent;
textlength=wsprintfW(str, L"Courier statement ");
TextOutW(hdc,x,y+baselinecourier,str,textlength);
GetTextExtentPoint32W(hdc,str,lstrlenW(str),&size);
x=x+size.cx;

//select font ariel 20 and output L
int baselineariel;
font = CreateFontW(10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,L"Ariel");
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
baselineariel=baseline-tm.tmAscent;
textlength=wsprintfW(str, L"ariel 10 statement 3 ");
TextOutW(hdc,x,y+baselineariel,str,textlength);

EndPaint(hwnd, &Ps);
RestoreDC(hdc,hDCLast);
DeleteDC(hdc);
DeleteObject(font);
DeleteObject(hBrush);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}