Compartir a través de


Configuring the ClearType Sample Application (Windows CE 5.0)

Send Feedback

To add the ClearType sample application to your OS design, you need to create a project in your OS design and add the source files to this project. This enables you to compile, link, and include the application in your OS design.

The sample application code in this section uses the CreateFontIndirect function. This creates a logical font with characteristics that are defined by the LOGFONT structure. The code also shows the use of SystemParametersInfo function. This is used to specify gamma values for the font.

To configure the ClearType sample application

  1. From the File menu, choose New Project or File.

  2. On the Projects tab, perform the following steps:

    1. Choose WCE Application.
    2. In the Project name box, type a name for the project without spaces. For this example, type ClearTypeProj.
    3. Choose OK.
  3. In the New Project Wizard, perform the following steps:

    1. In step 1 of 3, you would typically enter the appropriate information. For this example, leave these fields blank and choose Next.

    2. In step 2 of 3, choose An empty project, and then choose Next.

    3. In step 3 of 3, from the Release Type list, choose LOCAL, and then choose Finish.

      When project creation is complete, the project appears in the Projects node on the FileView tab. You can now add application code to your new project.

  4. From the File menu, choose New Project or File. On the Files tab, perform the following steps:

    1. Choose C++ Source File.
    2. In the File name box, type a name for the C++ file, for example, Cleartype_app.
    3. Ensure that the Add to project checkbox is selected, and the project ClearTypeProj appears in the text field below the checkbox.
    4. Choose OK.
  5. An empty window opens in the Platform Builder IDE. Cut and paste the following code into the window, and from the File menu, choose Save to save the file. From the File menu, choose Close to close the file.

    Note   To make the following code example easier to read, error checking is not included. This example code should not be used in a release configuration unless it has been modified to include secure error handling.

    /**********************************************************************
    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    PARTICULAR PURPOSE.
    
    Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
    
    MODULE: 
      cleartype_sample.cpp
    
    ABSTRACT: 
      This application code draws ClearType fonts with different settings.
    **********************************************************************/
    
    #include <windows.h>
    #include <tchar.h>
    
    LRESULT APIENTRY myWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdcPrimary;
        PAINTSTRUCT ps;
        static HDC hdc= NULL;
        static HBITMAP hbmp = NULL;
        static HBITMAP hbmpStock = NULL;
        int nWidth = 600;
        int nHeight = 300;
    
        switch(message)
        {
            // SystemParametersInfo with SPI_SETFONTSMOOTHINGCONTRAST triggers a system wide refresh (WM_PAINT).
            // if we draw the fonts in the WM_PAINT, then we get into a pattern of processing a WM_PAINT
            // triggers 3 WM_PAINTs.  To bypass this we cache off a bitmap holding the various fonts we want to show,
            // and then copy them to the primary on the WM_PAINT.
            case WM_CREATE:
            {
                int nSmooth, nOldSmooth;
                // A rectangle to hold the text.
                RECT rc = { 0, 0, nWidth, nHeight};
                LOGFONT lf;
                HFONT hFontNew, hFontOld;
                int nTextHeight;
    
                hdcPrimary = GetDC(hwnd);
    
                hdc = CreateCompatibleDC(hdcPrimary);
                hbmp = CreateCompatibleBitmap(hdcPrimary, nWidth, nHeight);
                SelectObject(hdc, hbmp);
                PatBlt(hdc, 0, 0, nWidth, nHeight, WHITENESS);
    
                // Clear out the lf structure to use when creating the font.
                memset(&lf, 0, sizeof(LOGFONT));
    
                // Retrieve the old Cleartype gamma.
                SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &nOldSmooth, FALSE);
    
                lf.lfQuality = DRAFT_QUALITY;
                hFontNew = CreateFontIndirect(&lf);
                hFontOld = (HFONT) SelectObject(hdc, hFontNew);
                // Draw text with the default system font. First calculate the size of the rectangle to use.
                DrawText(hdc, TEXT("This is the default system font."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
                DrawText(hdc, TEXT("This is the default system font."), -1, &rc, DT_LEFT | DT_TOP);
                SelectObject(hdc, hFontOld);
                DeleteObject(hFontNew);
    
                // Draw a Cleartype font using compatible widths. This has the same widths as the standard, but with cleartype smoothing.
                nTextHeight = rc.bottom - rc.top;
                rc.top += nTextHeight;
                rc.bottom += nTextHeight;
                lf.lfQuality = CLEARTYPE_COMPAT_QUALITY;
                hFontNew = CreateFontIndirect(&lf);
                hFontOld = (HFONT) SelectObject(hdc, hFontNew);
                DrawText(hdc, TEXT("This is a Cleartype font using compatible widths."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
                DrawText(hdc, TEXT("This is a Cleartype font using compatible widths."), -1, &rc, DT_LEFT | DT_TOP);
                SelectObject(hdc, hFontOld);
                DeleteObject(hFontNew);
    
                // Draw a Cleartype font with the standard font smoothing.
                nTextHeight = rc.bottom - rc.top;
                rc.top += nTextHeight;
                rc.bottom += nTextHeight;
                lf.lfQuality = CLEARTYPE_QUALITY;
                hFontNew = CreateFontIndirect(&lf);
                hFontOld = (HFONT) SelectObject(hdc, hFontNew);
                DrawText(hdc, TEXT("This is a Cleartype font."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
                DrawText(hdc, TEXT("This is a Cleartype font."), -1, &rc, DT_LEFT | DT_TOP);
                SelectObject(hdc, hFontOld);
                DeleteObject(hFontNew);
    
                // Draw a Cleartype font with a font smoothing of 1000.
                nSmooth = 1000;
                SystemParametersInfo(SPI_SETFONTSMOOTHINGCONTRAST, 0, &nSmooth, FALSE);
    
                nTextHeight = rc.bottom - rc.top;
                rc.top += nTextHeight;
                rc.bottom += nTextHeight;
                lf.lfQuality = CLEARTYPE_QUALITY;
                hFontNew = CreateFontIndirect(&lf);
                hFontOld = (HFONT) SelectObject(hdc, hFontNew);
                DrawText(hdc, TEXT("This is a ClearType font w/ 1000 font smoothing."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
                DrawText(hdc, TEXT("This is a ClearType font w/ 1000 font smoothing."), -1, &rc, DT_LEFT | DT_TOP);
                SelectObject(hdc, hFontOld);
                DeleteObject(hFontNew);
    
                // Draw a ClearType font with a font smoothing of 2200.
                nSmooth = 2200;
                SystemParametersInfo(SPI_SETFONTSMOOTHINGCONTRAST, 0, &nSmooth, FALSE);
    
                nTextHeight = rc.bottom - rc.top;
                rc.top += nTextHeight;
                rc.bottom += nTextHeight;
                lf.lfQuality = CLEARTYPE_QUALITY;
                hFontNew = CreateFontIndirect(&lf);
                hFontOld = (HFONT) SelectObject(hdc, hFontNew);
                DrawText(hdc, TEXT("This is a ClearType font w/ 2200 font smoothing."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
                DrawText(hdc, TEXT("This is a ClearType font w/ 2200 font smoothing."), -1, &rc, DT_LEFT | DT_TOP);
                SelectObject(hdc, hFontOld);
                DeleteObject(hFontNew);
    
                SystemParametersInfo(SPI_SETFONTSMOOTHINGCONTRAST, 0, &nOldSmooth, FALSE);
    
                ReleaseDC(hwnd, hdcPrimary);
                break;
            }
            case WM_PAINT:
                // copy over our text to the primary.  The prevents a loop of processing a WM_PAINT causing more WM_PAINT messages.
                hdcPrimary = BeginPaint(hwnd, &ps);
                BitBlt(hdcPrimary, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);
                EndPaint(hwnd, &ps);
                break;
            case WM_DESTROY:
                // cleanup the allocated GDI objects, and post the quit message.
                SelectObject(hdc, hbmpStock);
                DeleteObject(hbmp);
                DeleteDC(hdc);
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, message, wParam, lParam);    
        }
    
        return(0x00);
    }
    
    int WINAPI WinMain
    (
        HINSTANCE hInstance,
        HINSTANCE hInstPrev,
        LPWSTR    pszCmdLine,
        int       nCmdShow
    )
    {
        WNDCLASS wc;
        LPCTSTR myAtom;
        HWND myHwnd;
        MSG     msg;
        TCHAR *tszClassName = TEXT("GDI Cleartype test class");
    
        memset(&wc, 0, sizeof (WNDCLASS));
        wc.lpfnWndProc = (WNDPROC) myWndProc;   // Window Procedure
        wc.hInstance = hInstance;  // Owner of this class
        wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Default color
        wc.lpszClassName = tszClassName;
        wc.style = CS_HREDRAW | CS_VREDRAW;
    
        myAtom = (LPCTSTR) RegisterClass(&wc);
        myHwnd = CreateWindow(myAtom, TEXT("GDI Cleartype test"), WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_BORDER, 
                                              CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
        ShowWindow(myHwnd, SW_SHOW);
        UpdateWindow(myHwnd);
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    
        DestroyWindow(myHwnd);
        UnregisterClass(tszClassName, NULL);
    
        return msg.wParam;
    }
    

The ClearType sample application is now added to your OS design.

See Also

How to Implement ClearType | Working with ClearType Fonts

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.