字体设置对话框


#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值