注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

mie

 
 
 

日志

 
 

CHARFORMAT和LOGFONT互相转换(根据微软自带的代码修改而成)  

2011-09-04 17:40:02|  分类: 源码区 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

CHARFORMATLOGFONT互相转换(根据微软自带的代码修改而成)

   

//声明一下函数原型

//CHARFORMATLOGFONT

DWORD g_FillInLogFont(const CHARFORMAT& cf, LOGFONT & logfont);

   

//LOGFONTCHARFORMAT

void g_GetCharFormat(

    CHARFORMAT& cf,

    LOGFONT & logfont,

    COLORREF text_color=RGB(0, 0, 0),

    DWORD Flags=CF_EFFECTS/*CF_NOSTYLESEL | CF_NOSIZESEL | CF_NOFACESEL | CF_EFFECTS | CF_EFFECTS | CF_NOSCRIPTSEL*/

    ); 

//实现这些函数

   

/*************************************************************************

作者:章永辉

日期: 2011-9-4 17:22:12

最后修改日期: 2011-9-4 17:22:12

函数原型:

功能描述: CHARFORMATLOGFONT

函数参数:

返回值:

注意事项:

*************************************************************************/

   

DWORD g_FillInLogFont(const CHARFORMAT& cf, LOGFONT & logfont)

{

    DWORD dwFlags = 0;

    BOOL bIsFormat2 = (cf.cbSize >= sizeof(CHARFORMAT2));

   

    if (cf.dwMask & CFM_SIZE)

    {

        CDC dc;

        BOOL bSuccess=dc.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

        ENSURE(bSuccess);

        LONG yPerInch = dc.GetDeviceCaps(LOGPIXELSY);

        logfont.lfHeight = -(int) ((cf.yHeight * yPerInch) / 1440);

    }

    else

    {

        logfont.lfHeight = 0;

    }

   

    logfont.lfWidth = 0;

    logfont.lfEscapement = 0;

    logfont.lfOrientation = 0;

   

    if ((cf.dwMask & (CFM_ITALIC|CFM_BOLD)) == (CFM_ITALIC|CFM_BOLD))

    {

        logfont.lfWeight = (cf.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;

        logfont.lfItalic = (BYTE)((cf.dwEffects & CFE_ITALIC) ? TRUE : FALSE);

    }

    else

    {

        dwFlags |= CF_NOSTYLESEL;

        logfont.lfWeight = FW_DONTCARE;

        logfont.lfItalic = FALSE;

    }

   

    if ((cf.dwMask & (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR)) ==

        (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR))

    {

        dwFlags |= CF_EFFECTS;

        logfont.lfUnderline = (BYTE)((cf.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE);

        logfont.lfStrikeOut = (BYTE)((cf.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE);

    }

    else

    {

        logfont.lfUnderline = (BYTE)FALSE;

        logfont.lfStrikeOut = (BYTE)FALSE;

    }

   

    if (cf.dwMask & CFM_CHARSET)

        logfont.lfCharSet = cf.bCharSet;

    else

        dwFlags |= CF_NOSCRIPTSEL;

    logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;

    logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;

    logfont.lfQuality = DEFAULT_QUALITY;

   

    if (bIsFormat2)

    {

        const CHARFORMAT2* pCF2 = (const CHARFORMAT2*) &cf;

        if (pCF2->dwMask & CFM_WEIGHT)

            logfont.lfWeight = pCF2->wWeight;

   

        // CHAFORMAT2 has UNICODE face name, CHARFORMAT doesn't

   

        if (cf.dwMask & CFM_FACE)

        {

            logfont.lfPitchAndFamily = pCF2->bPitchAndFamily;

            Checked::tcscpy_s(logfont.lfFaceName, _countof(logfont.lfFaceName), (LPTSTR)pCF2->szFaceName);

        }

    }

    else

    {

        logfont.lfPitchAndFamily = cf.bPitchAndFamily;

        CString strFaceName(cf.szFaceName);

        Checked::tcsncpy_s(logfont.lfFaceName, _countof(logfont.lfFaceName), strFaceName.GetString(), _TRUNCATE);

    }

   

    if (!(cf.dwMask & CFM_FACE))

    {

        logfont.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;

        logfont.lfFaceName[0] = (TCHAR)0;

    }

   

    return dwFlags;

}

   

/*************************************************************************

作者:章永辉

日期: 2011-9-4 17:24:35

最后修改日期: 2011-9-4 17:24:35

函数原型:

功能描述: LOGFONTCHARFORMAT

函数参数:

返回值:

注意事项:

*************************************************************************/

   

void g_GetCharFormat(

    CHARFORMAT& cf,

    LOGFONT & logfont,

    COLORREF text_color/*=RGB(0, 0, 0)*/,

    DWORD Flags/*=CF_NOSTYLESEL | CF_NOSIZESEL | CF_NOFACESEL | CF_EFFECTS | CF_EFFECTS | CF_NOSCRIPTSEL*/

    )

{

    cf.dwEffects = 0;

    cf.dwMask = 0;

    if ((Flags & CF_NOSTYLESEL) == 0)

    {

        cf.dwMask |= CFM_BOLD | CFM_ITALIC;

        cf.dwEffects |= (logfont.lfWeight == FW_BOLD ? TRUE : FALSE) ? CFE_BOLD : 0;

        cf.dwEffects |= (logfont.lfItalic ? TRUE : FALSE) ? CFE_ITALIC : 0;

    }

    if ((Flags & CF_NOSIZESEL) == 0)

    {

        cf.dwMask |= CFM_SIZE;

 

        CDC dc;

        BOOL bSuccess=dc.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

        ENSURE(bSuccess);

        LONG yPerInch = dc.GetDeviceCaps(LOGPIXELSY);

        cf.yHeight = -(int) (logfont.lfHeight*1440/yPerInch);

    }

 

    if ((Flags & CF_NOFACESEL) == 0)

    {

        cf.dwMask |= CFM_FACE;

        cf.bPitchAndFamily = logfont.lfPitchAndFamily;

        Checked::tcsncpy_s(cf.szFaceName, _countof(cf.szFaceName), logfont.lfFaceName, _TRUNCATE);

    }

 

    if (Flags & CF_EFFECTS)

    {

        cf.dwMask |= CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;

        cf.dwEffects |= (logfont.lfUnderline ? TRUE : FALSE) ? CFE_UNDERLINE : 0;

        cf.dwEffects |= (logfont.lfStrikeOut ? TRUE : FALSE) ? CFE_STRIKEOUT : 0;

        cf.crTextColor = text_color;

    }

    if ((Flags & CF_NOSCRIPTSEL) == 0)

    {

        cf.bCharSet = logfont.lfCharSet;

        cf.dwMask |= CFM_CHARSET;

    }

    cf.yOffset = 0;

}

   

By:zhanyonhu

  评论这张
 
阅读(1894)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016