• 让程序使用自带的字体文件


    转载请说明原出处,谢谢~·

            我笔记本使用的是win7系统,开发的duilib程序一直都使用微软雅黑字体,昨天在xp系统下测试时发现由于xp下没有微软雅黑字体而导致duilib使用了默认的宋体字,瞬间感觉界面不协调了。解决这个问题的方法就是让程序自带自己使用的字体,并且在运行时加载这个字体。以前从来没关心过这个问题。

            比较简单的方法就是在程序运行时把字体文件拷贝到系统的Fonts目录下,让系统自动安装这个字体,但是这样就会修改系统的文件,这可能并不是用户需要的东西。翻了翻MSDN得知了AddFontResource和CreateScalableFontResource这两个API,使用他们可以让程序加载任意路径的字体而不必拷贝到系统路径。于是写了一个AddFont类来加载一个字体,代码如下:

    #pragma once
    
    #ifdef UNICODE
    typedef wstring tstring;
    #else
    typedef string	tstring;
    #endif
    
    class CAddFont
    {
    public:
    	CAddFont(void);
    	~CAddFont(void);	
           /*
            * @param filePath:字体文件相对本程序的路径,不需要加前缀\,和后缀名
    	* @param fontName:字体的名称,如宋体,微软雅黑
            */
    	bool AddFont(LPCTSTR szfilePath, LPCTSTR szFontName);
    
    	LPCTSTR GetFontName() const;
    private:
    	tstring	 m_strFilePath;
    	tstring	 m_strFontName;
    };
    

    #include "duilib.h"
    // duilib.h中已经包含如下头文件
    // #include "Windows.h"
    // #include "string"
    // #include "tchar.h"
    // using namespace std;
    #include "AddFont.h"
    
    
    CAddFont::CAddFont(void)
    {
    	TCHAR szCurrentPath[MAX_PATH] = {0};
    	GetCurrentDirectory(MAX_PATH, szCurrentPath);
    	m_strFilePath = szCurrentPath;
    	m_strFilePath += _T("\");
    }
    
    
    CAddFont::~CAddFont(void)
    {
    	wstring strFont = m_strFilePath;
    	wstring strFontPath = m_strFilePath;
    	strFont += _T(".FOT");
    	strFontPath +=  _T(".TTF");
    
    	RemoveFontResource(strFontPath.c_str());
    	DeleteFile(strFont.c_str());
    }
    
    bool CAddFont::AddFont(LPCTSTR szfilePath, LPCTSTR szFontName)
    {
    	m_strFilePath += szfilePath;
    
    	wstring strFont = m_strFilePath;
    	wstring strFontPath = m_strFilePath;
    	strFont += _T(".FOT");
    	strFontPath +=  _T(".TTF");
    
    	BOOL bResult = CreateScalableFontResource(0,  strFont.c_str(),  strFontPath.c_str(),  NULL);  
    	if (bResult == FALSE)
    	{
    		DUI__Trace(_T("错误码为:%d"), GetLastError()); //这里使用了duilib自带的调试函数
    		::MessageBox(NULL, _T("失败"), _T("提示"), 0);  //实际使用时建议注释此行代码
    		return false;
    	}
    
    	int nResult = AddFontResource(strFontPath.c_str());
    	if (nResult == 0)
    	{
    		::MessageBox(NULL, _T("失败"), _T("提示2"), 0);  //实际使用时建议注释此行代码
    		return false;
    	}
    
    	::SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
    	m_strFontName = szFontName;
    
    	return true;
    }
    
    LPCTSTR CAddFont::GetFontName() const
    {
    	return m_strFontName.c_str();
    }

           使用起来很简单,比如在duilib的窗体类中声明一个成员变量。
        CAddFont		m_addFont;
           同时把”华文新魏“字体文件“STXINWEI.ttf"放到了Font目录里,然后在主窗体初始化函数InitWindow里加入这行代码就可以了。



        m_addFont.AddFont(_T("Font\STXINWEI"), _T("华文新魏"));
           这时就可以直接在编写xml文件时使用这个字体了,效果如下:



    总结:

           我这里只是简单随手的封装了一个CAddFont类来做可行性测试,实际这个类的代码写的并不好,读者可以封装一个更好的,支持添加并管理多个字体。如果代码有错误,请联系我


    Redrain  QQ:491646717  2014.9.19


  • 相关阅读:
    在WebBrowser控件中获取鼠标在网页(不是浏览器窗口)上点击的位置,
    Sqlserver 2008 评估版本到期,Sqlserver 2008试用期已过,解决方案.
    C#防止WebBrowser在新窗口中打开链接页面
    c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
    屏蔽右键代码(防止网页恶意复制)
    SEO优化中的div+css命名规则
    photoshop CS不能打字,出现死机等现象的解决办法!!
    带关闭功能的漂浮图片代码
    VS2008连接SQL2005问题,老连接不上。
    WinForm中获取鼠标当前位置
  • 原文地址:https://www.cnblogs.com/redrainblog/p/4209730.html
Copyright © 2020-2023  润新知