Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525331
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-25 13:44:10

选择字体对话框是相当常用的, 我们先看一下程序的运行效果:

CHOOSEFONT结构:



cf.lStructSize = sizeof(CHOOSEFONT);
        cf.hwndOwner = hwnd;
        cf.hDC = NULL;
        cf.lpLogFont = &lf;
        cf.iPointSize = 0;
        cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_SCREENFONTS;
        cf.rgbColors = 0;
        cf.lCustData = 0;
        cf.lpfnHook = NULL;
        cf.lpTemplateName = NULL;
        cf.hInstance = NULL;
        cf.lpszStyle = NULL;
        cf.nFontType = 0;
        cf.nSizeMin = 0;
        cf.nSizeMax = 0;
        return 0;

上面是一般情况下的初始化。

我们没有必要记住每一个参数, 只有几个比较重要的参数需要了解, 我想写的时候最好查MSDN吧
源代码:


#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    static TCHAR szAppName[] = TEXT("XZZ");
    
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = szAppName;
    wndclass.lpszClassName = szAppName;
    
    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }
    
    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
    
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg, NULL, 0, 0)) {
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static CHOOSEFONT cf;
    static LOGFONT lf;
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch(message) {
    case WM_CREATE:
        GetObject(GetStockObject(SYSTEM_FONT), sizeof(lf), &lf);
        cf.lStructSize = sizeof(CHOOSEFONT);
        cf.hwndOwner = hwnd;
        cf.hDC = NULL;
        cf.lpLogFont = &lf;
        cf.iPointSize = 0;
        cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_SCREENFONTS;
        cf.rgbColors = 0;
        cf.lCustData = 0;
        cf.lpfnHook = NULL;
        cf.lpTemplateName = NULL;
        cf.hInstance = NULL;
        cf.lpszStyle = NULL;
        cf.nFontType = 0;
        cf.nSizeMin = 0;
        cf.nSizeMax = 0;
        return 0;
    case WM_COMMAND:
        switch(LOWORD(wParam)) {
        case IDM_FONT:
            if(ChooseFont(&cf))
                InvalidateRect(hwnd, NULL, TRUE);
            return 0;
        }
        return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            SelectObject(hdc, CreateFontIndirect(&lf));
            SetTextColor(hdc, cf.rgbColors);
            DrawText(hdc, "你好 世界", 9, &rect, DT_SINGLELINE |
                DT_VCENTER | DT_CENTER);
            EndPaint(hwnd, &ps);
            DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(4832) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~