基于Visual C++2010与windows7 sdk开发Windows7应用(4) 获取windows7字体并设置字体

Windows7新增了很多字体,并改变了字体接口,如何获取windows7字体并设置字体呢,

ClearType是Windows 7字体渲染方式。ClearType是Windows 7字体渲染方式。由于用户80%的时间几乎都要花在阅读上,微软特别看重用户在字体方面的体验,而ClearType得出现正是为了通过特殊的渲染方式给予LCD和CRT显示器以出色的显示效果的。
为了适应不同显示器的特性,Windows 7内置了一项名为ClearType Tuner的功能用于在不同环境下选择合适的ClearType样式以获取最佳的视觉效果。

针对安装字体过多拖慢系统速度的问题,Windows 7还能按照条件将特定字体隐藏起来,查看起来就方便多了,而且不会白白占用更多内存。Windows 7会自动按照用户语言设置隐藏字体,但也允许用户手动设置。

Windows 7终于去掉了安装新字体对话框,并在其他方面做出了很多改进。

Windows 7预览版还增加了一个名为“Gabriola”的新字体,是一种很漂亮的Script字体,也支持大量先进的OpenType功能。

最后还有“DirectWrite API”,能更好地支持文字渲染,包括非像素边缘字体和Y轴反锯齿等。

基于Visual C++2010与windows7 sdk开发Windows7应用(4) 获取windows7字体并设置字体

 

基于Visual C++2010与windows7 sdk开发Windows7应用(4) 获取windows7字体并设置字体

 

基于Visual C++2010与windows7 sdk开发Windows7应用(4) 获取windows7字体并设置字体

 

 

下列代码在windows7 中VS2010调试通过,详情键代码注释

#include <windows.h>

#pragma comment(linker,"/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='7.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/"")

#define ID_BUTTON   100
#define ID_CHECKBOX 200
#define ID_LABEL    300

HINSTANCE   g_hInst;     
HWND        g_hwndApp;   // 创建窗口
HWND        g_hwndLabel; // 字体显示控件

// 主程序初始化
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void InitDefaultLF(LOGFONT *lf);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR pszCmdLine, int iCmdShow)
{
    g_hInst = hInstance; // 保存窗口句柄

    MSG         msg;
    WCHAR const szWindowName[] = L"选择字体Sample";
    WCHAR const szWindowClass[] = L"选择字体SampleWClass";

    WNDCLASS    wc = {}; 
    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = WndProc;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName    = szWindowClass;

    RegisterClass(&wc);

    g_hwndApp = CreateWindow(szWindowClass, szWindowName,
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        490, 120, NULL, NULL, hInstance, NULL);
    if (g_hwndApp)
    {
        ShowWindow(g_hwndApp, iCmdShow);
        UpdateWindow(g_hwndApp);
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);    
        }
    }

    return (int)msg.wParam; 
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static LOGFONT lf = {};

    switch(uMsg)
    {
    case WM_CREATE:
        {
            // 创建选择字体按钮
            CreateWindow(L"button",
                L"选择字体",
                BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
                20, 20,
                100, 20,
                hwnd, (HMENU)ID_BUTTON,
                g_hInst, NULL);

            // 创建check box

            CreateWindow(L"button",
                L"显示所有字体?",
                BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE,
                20, 45,
                120, 20,
                hwnd, (HMENU)ID_CHECKBOX,
                g_hInst, NULL);

            // 创建文本显示

            g_hwndLabel =  CreateWindow(L"static",
                L"CSDN专家尹成的杰作.",
                SS_CENTER | WS_CHILD | WS_VISIBLE,
                150, 10,
                300, 40,
                hwnd, (HMENU)ID_LABEL,
                g_hInst, NULL);
            InitDefaultLF(&lf);
            break;
        }
    case WM_COMMAND:
        {
            if (LOWORD(wParam) == ID_BUTTON)
            {
                CHOOSEFONT cf = { sizeof(cf) };
                cf.hwndOwner = hwnd;
                cf.lpLogFont = &lf;
                if (BST_CHECKED == IsDlgButtonChecked(hwnd, ID_CHECKBOX))
                {
                    // 显示所有字体
                    cf.Flags |= CF_INACTIVEFONTS;
                }

                if (ChooseFont(&cf) == TRUE)
                {
                    HFONT hfont = CreateFontIndirect(&lf);
                    if (hfont)
                    {
                        // 删除旧字体应用

                        HFONT hfontOld = (HFONT)SendMessage(g_hwndLabel, WM_GETFONT, 0, 0);
                        if (hfontOld)
                        {
                            DeleteObject(hfontOld);
                        }
                        SendMessage(g_hwndLabel, WM_SETFONT, (WPARAM)hfont,  MAKELPARAM(TRUE, 0));
                    }
                }
            }
            break;
        }
    case WM_DESTROY:
        {
            // 清理资源
            HFONT hfontOld = (HFONT)SendMessage(g_hwndLabel, WM_GETFONT, 0, 0);
            if (hfontOld)
            {
                DeleteObject(hfontOld);
            }
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void InitDefaultLF(LOGFONT *plf)
{
    HDC hdc = GetDC(NULL);
    ZeroMemory(plf, sizeof(*plf));
    plf->lfCharSet = (BYTE) GetTextCharset(hdc);
    plf->lfOutPrecision = OUT_DEFAULT_PRECIS;
    plf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
    plf->lfQuality = DEFAULT_QUALITY;
    plf->lfPitchAndFamily = DEFAULT_PITCH;
    plf->lfWeight = FW_NORMAL;
    plf->lfHeight = -MulDiv(10, GetDeviceCaps(hdc, LOGPIXELSY), 2);

    ReleaseDC(NULL, hdc);
}

 

 

 


原文链接: http://blog.csdn.net/yincheng01/article/details/5508384

你可能感兴趣的:(基于Visual C++2010与windows7 sdk开发Windows7应用(4) 获取windows7字体并设置字体)