#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;
}
字体设置对话框
最新推荐文章于 2022-05-31 10:05:22 发布