字體設置對話框


#include <windows.h>
#include <commdlg.h>
#include <stdio.h>

typedef struct
{
    int bold;
    int italic;
    int size;
    char name[32];
} FontStyle;

int FontMsg(FontStyle *fs)
{
    static char *fontstr[128];
    CHOOSEFONT cf;
    LOGFONT logfont = {0};
    memset(fontstr, 0, sizeof(fontstr));
    memset(&cf, 0, sizeof(cf));
    // 設置默認數值
    logfont.lfItalic = (fs->italic) ? 1 : 0;
    logfont.lfWeight = (fs->bold) ? FW_BOLD : FW_NORMAL;
    {
        HDC hdc = GetDC(NULL);
        logfont.lfHeight = -MulDiv(fs->size, GetDeviceCaps(hdc, LOGPIXELSY), 72);
        ReleaseDC(NULL, hdc);
    }
    strncpy(logfont.lfFaceName, fs->name,
            sizeof(logfont.lfFaceName));
    cf.Flags = CF_INITTOLOGFONTSTRUCT |
               CF_SCREENFONTS;
    cf.lpLogFont = &logfont;
    cf.lStructSize = sizeof(cf);
    if (!ChooseFont(&cf)) return 0;
    // 獲取數值
    fs->italic = !!logfont.lfItalic;
    fs->bold = logfont.lfWeight > FW_NORMAL;
    fs->size = cf.iPointSize / 10;
    strncpy(fs->name, logfont.lfFaceName,
            sizeof(logfont.lfFaceName));
    return 1;
}

int main()
{
    FontStyle fs;
    fs.bold = 0;
    fs.italic = 0;
    fs.size = 12;
    sprintf(fs.name, "宋體");
    if(FontMsg(&fs))
        printf("名稱:%s, 尺寸:%d, 粗體:%d, 斜體:%d\n",
               fs.name, fs.size, fs.bold, fs.italic);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章