createfontindirect

本文介绍了CreateFontIndirect函数的功能及其使用方法。该函数用于根据LOGFONT结构体定义的特性创建逻辑字体,并可通过GDI字体映射器与物理字体匹配。文章还提及了字体类型的系统限制及如何通过DeleteObject释放不再使用的字体。
  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数创建一种在指定结构定义其特性的逻辑字体。这种字体可在后面的应用中被任何设备环境选作字体。

    函数原型:HFONT CreateFontIndirect(CONST LOGFONT *lplf);

    参数:

    lplf:指向定义此逻辑字体特性的LOGFONT结构的指针。

    返回值:如果函数调用成功,返回值是逻辑字体的句柄;如果函数调用失败,返回值是NULL。

    Windows NT:若想获得更多的错误信息,请调用GetLastError函数。

    备注:函数CreateFontIndirect创建一种在结构LOGFONT中定义特性的逻辑字体。当这种字体被函数选择时,GDI的字体映射器会努力将此逻辑字体与现有物理字体相匹配,如果不能找到精确匹配,将会提供另一种选择,其特性与所要求的特性尽可能地匹配。

    当一种字体不再需要进,可调用DeleteObject删除它。

    Windows CE:1.0版本只支持光栅字体。Windows CE版本2.0支持使用TrueType字体和光栅字体其中之一的系统。字体类型(光栅或TrueType)是在系统设计时就已选择,不能被应用程序改变。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;头文件:wingdi.h;库文件:gdi32.lib;Unicode:在Windows NT环境下以Unicode和ANSI两种方式实现。

// MyColorButton.cpp #include <commctrl.h> #include "pch.h" #include "CMyColorButton.h" #include <iostream> #include <gdiplus.h> #pragma comment(lib, "gdiplus.lib") using namespace Gdiplus; ULONG_PTR g_gdiplusToken = 0; #ifdef _UNICODE #pragma message("Unicode 已启用") #else #pragma message("Unicode 未启用") #endif void InitGdiPlus() { GdiplusStartupInput input; GdiplusStartup(&g_gdiplusToken, &input, NULL); } void ShutdownGdiPlus() { if (g_gdiplusToken != 0) { GdiplusShutdown(g_gdiplusToken); g_gdiplusToken = 0; } } CMyColorButton::CMyColorButton() { // 默认构造函数留空 } CMyColorButton::CMyColorButton(COLORREF bkColor, const CString& text) : m_bkColor(bkColor), m_text(text) { // 创建字体 LOGFONT lf; memset(&lf, 0, sizeof(LOGFONT)); lf.lfHeight = -20; // 字体高度 _tcscpy_s(lf.lfFaceName, _T("微软雅黑")); // 字体名称 lf.lfWeight = FW_BOLD; // 粗体 m_font.CreateFontIndirect(&lf); } void CMyColorButton::Initialize(COLORREF bkColor, const CString& text) { m_bkColor = bkColor; m_text = text; LOGFONT lf = { 0 }; lf.lfHeight = -20; _tcscpy_s(lf.lfFaceName, _T("微软雅黑")); lf.lfWeight = FW_BOLD; m_font.CreateFontIndirect(&lf); } void CMyColorButton::SetNewState(COLORREF newColor, const CString& newText) { m_bkColor = newColor; m_text = newText; Invalidate(); // 强制重绘 } void CMyColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (pDC == nullptr) return; CRect rect = lpDrawItemStruct->rcItem; UINT state = lpDrawItemStruct->itemState; // 创建 GDI+ Graphics 对象 Graphics graphics(pDC->GetSafeHdc()); // 圆角半径 int cornerRadius = 10; // 调整矩形区域用于按下状态 if (state & ODS_SELECTED) { rect.DeflateRect(2, 2); } // 构造圆角矩形路径 RectF gpRect((REAL)rect.left, (REAL)rect.top, (REAL)rect.Width(), (REAL)rect.Height()); REAL radius = (REAL)cornerRadius; GraphicsPath path; path.AddArc(gpRect.GetRight() - 2 * radius, gpRect.GetTop(), 2 * radius, 2 * radius, 270, 90); // top-right path.AddArc(gpRect.GetRight() - 2 * radius, gpRect.GetBottom() - 2 * radius, 2 * radius, 2 * radius, 0, 90); // bottom-right path.AddArc(gpRect.GetLeft(), gpRect.GetBottom() - 2 * radius, 2 * radius, 2 * radius, 90, 90); // bottom-left path.AddArc(gpRect.GetLeft(), gpRect.GetTop(), 2 * radius, 2 * radius, 180, 90); // top-left path.CloseFigure(); // 设置背景颜色 Color bkColor; if (state & ODS_HOT) { bkColor = Color(255, 100, 150, 255); // 悬停颜色 } else if (state & ODS_SELECTED) { bkColor = Color(255, 80, 80, 80); // 按下颜色 } else { BYTE r = GetRValue(m_bkColor); BYTE g = GetGValue(m_bkColor); BYTE b = GetBValue(m_bkColor); // 构造 Gdiplus::Color bkColor = Color(255, r, g, b); //bkColor = Color(255, m_bkColor.GetR(), m_bkColor.GetG(), m_bkColor.GetB()); // 默认颜色 } SolidBrush brush(bkColor); graphics.FillPath(&brush, &path); // 边框绘制 Pen borderPen(Color(255, 64, 64, 64), 1.0f); graphics.DrawPath(&borderPen, &path); // 文字绘制(垂直居中) CString text; GetWindowText(text); Font font(pDC->GetSafeHdc(), &m_font); // 使用当前字体 RectF layoutRect(rect.left, rect.top, rect.Width(), rect.Height()); StringFormat format; format.SetAlignment(StringAlignmentCenter); format.SetLineAlignment(StringAlignmentCenter); SolidBrush textBrush(Color(255, 255, 255, 255)); // 白色字体 graphics.DrawString(text, -1, &font, layoutRect, &format, &textBrush); }
最新发布
07-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值