Monday, December 7, 2009

Win32 Programming: working with formatted texts.




Above screenshot shows the use TextOut and DrawText functions. Here we’ll describe a segment of code that generates this kind of formatted text with different styles, fonts & related functions. Look at the code segment below that does this work.

case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      TextOut(hdc, 25, 20, inst, strlen(inst));

      // set font
      hFont = CreateFont(20,0,0,0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Verdana"));
      // Select font for the device context
      SelectObject(hdc, hFont);

      // Set drawing rectangle
      RECT rect;
      SetRect(&rect, 25,70, 300, 100);
     
      // Set color for the text
      SetTextColor(hdc, RGB(255, 0, 0));

      DrawText(hdc, "DrawText: Verdana [20]", -1,&rect, DT_LEFT);

      hFont = CreateFont(20,0,0,0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Arial"));
      SelectObject(hdc, hFont);
      SetRect(&rect, 25, 100, 320, 140);
      DrawText(hdc, "DrawText: Arial Bold [20]", -1,&rect, DT_LEFT);
      TextOut(hdc, 25, 140, "TextOut: Arial Bold [20]", strlen("TextOut: Arial Bold [20]"));

      EndPaint(hWnd, &ps);
      break;

hdc = BeginPaint(hWnd, &ps);

BeginPaint function prepares the specified window for painting and fills a PAINTSTRUCT structure with information about the painting. This function returns the handle to a display device context for the specified window (in failure returns null).

TextOut(hdc, 25, 20, inst, strlen(inst));

The TextOut function writes character string at specified location using the currently selected font, background color, and text color.

Syntax

BOOL TextOut(
         __in HDC hdc,
         __in int nXStart,
         __in int nYStart,
         __in LPCTSTR lpString,
         __in int cbString
);


hFont = CreateFont(20, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Verdana"));

CreateFont function creates a logical font with the specified characteristics. The logical font can subsequently be selected as the font for any device.

HFONT CreateFont(
       __in int nHeight,
       __in int nWidth,
       __in int nEscapement,
       __in int nOrientation,
       __in int fnWeight,
       __in DWORD fdwItalic,
       __in DWORD fdwUnderline,
       __in DWORD fdwStrikeOut,
       __in DWORD fdwCharSet,
       __in DWORD fdwOutputPrecision,
       __in DWORD fdwClipPrecision,
       __in DWORD fdwQuality,
       __in DWORD fdwPitchAndFamily,
       __in LPCTSTR lpszFace
);

For detail information on this function follow here.

SelectObject(hdc, hFont);

This function selects an object into the specified device context. The new object replaces the previous object.

RECT rect;
SetRect(&rect, 25,70, 300, 100);

Declares an instance of rectangle and sets coordinates.

SetTextColor(hdc, RGB(255, 0, 0));
SetTextColor function sets the text color for the specified device context to the specified color.

Or you can use this:

SetTextColor(hdc, 0x0000FF); // 0xBBRRGG

DrawText(hdc, "DrawText: Verdana [20]", -1,&rect, DT_LEFT);


This function draws formatted text in the specified rectangle. DrawText formats the text according to the specified format method.

The syntax of the function is:

int DrawText(
    HDC hDC,
    LPCTSTR lpString,
    int nCount,
    LPRECT lpRect,
    UNIT uFormat
);


View details on DrawText here.

Complete code of the program is given below.

/***************************************************
      Working with Texts,
       Change font
       Change text style Bold, Italic

      -- S A Win32 Tutorial
***************************************************/

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static TCHAR szWindowClass[] = "win32app";

static TCHAR szTitle[] = "SA OS Win32 Tutorial";

HINSTANCE hInst;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            "Call to RegisterClassEx failed!",
            "Win32 Tutorial",
            NULL);

        return 1;
    }

    hInst = hInstance;

    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        300, 280,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            "Call to CreateWindow failed!",
            "S A Win32 Tut",
            NULL);

        return 1;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    HFONT hFont;
    char inst[100] = "TexOut: use of TextOut & DrawText";

    switch (message) {
            case WM_PAINT:
                  hdc = BeginPaint(hWnd, &ps);

                  TextOut(hdc, 25, 20, inst, strlen(inst));

                  // set font
                  hFont = CreateFont(20,0,0,0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Verdana"));
                  // Select font for the device context
                  SelectObject(hdc, hFont);

                  // Set drawing rectangle
                  RECT rect;
                  SetRect(&rect, 25,70, 300, 100);
                 
                  // Set color for the text
                  SetTextColor(hdc, RGB(255, 0, 0));
                  // or you can use this
                  //SetTextColor(hdc, 0x0000FF);            // 0xBBRRGG
                  // or use this
                  DrawText(hdc, "DrawText: Verdana [20]", -1,&rect, DT_LEFT);

                  hFont = CreateFont(20,0,0,0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
                  SelectObject(hdc, hFont);
                  SetRect(&rect, 25, 100, 320, 140);
                  DrawText(hdc, "DrawText: Arial Bold [20]", -1,&rect, DT_LEFT);
                  TextOut(hdc, 25, 140, "TextOut: Arial Bold [20]", strlen("TextOut: Arial Bold [20]"));

                  EndPaint(hWnd, &ps);
                  break;

            case WM_DESTROY:
                  PostQuitMessage(0);
                  break;
            default:
                  return DefWindowProc(hWnd, message, wParam, lParam);
                  break;
    }

    return 0;
}

No comments:

Post a Comment