📄 instgdifont.cpp
字号:
//instGdiFont.cpp
#ifndef INST_GDIFONT_CPP
#define INST_GDIFONT_CPP
#include "instGdiFont.h"
namespace inst
{
void CGdiFont::GenerateGdiFont()
{
HFONT old=m_h;
m_h=::CreateFontIndirectW(&m_lf);
if(m_hdc.IsInited())::SelectObject(m_hdc.GetValue(),m_h);
if(old)::DeleteObject((HGDIOBJ)old);
}
CGdiFont::CGdiFont()
{
ZeroMemory(&m_lf,sizeof(m_lf));
m_lf.lfHeight = 15;
m_lf.lfWeight = 400;
GenerateGdiFont();
}
CGdiFont::CGdiFont(const IFont *font)
{
if( this == font->GetAddr() )return;
if(!font)return;
ZeroMemory(&m_lf,sizeof(m_lf));
memcpy(m_lf.lfFaceName,font->GetName().ReadW(),Min(LF_FACESIZE,font->GetName().GetLen()));
m_lf.lfHeight = font->GetHeight();
m_lf.lfWeight = font->IsBold()?700:400;
m_lf.lfItalic = font->IsItalic()?1:0;
m_lf.lfUnderline = font->IsUnderline()?1:0;
m_lf.lfCharSet = font->GetCharSet();
GenerateGdiFont();
}
CGdiFont::CGdiFont(const CStr &n,POS h,Bool b,Bool i,Bool un,UByte cs)
{
ZeroMemory(&m_lf,sizeof(m_lf));
memcpy(m_lf.lfFaceName,n.ReadW(),Min(LF_FACESIZE,n.GetLen()));
m_lf.lfHeight = h;
m_lf.lfWeight = b?700:400;
m_lf.lfItalic = i?1:0;
m_lf.lfUnderline = un?1:0;
m_lf.lfCharSet = cs;
GenerateGdiFont();
}
CGdiFont::~CGdiFont()
{
if(m_hdc.IsInited())if(m_hdc.GetValue())::DeleteDC(m_hdc.GetValue());
if(m_h)::DeleteObject((HGDIOBJ)m_h);
}
POS CGdiFont::GetCharWidth(WChar wch)const
{
if(False==m_hdc.IsInited())
{
const_cast<CGdiFont *>(this)->m_hdc=::CreateCompatibleDC(0);
::SelectObject(m_hdc.GetValue(),m_h);
}
SIZE size={0,0};
::GetTextExtentPoint32W(m_hdc.GetValue(),&wch,1,&size);
return size.cx;
}
POS CGdiFont::GetStrWidth(const CStr &str)const
{
if(False==m_hdc.IsInited())
{
const_cast<CGdiFont *>(this)->m_hdc=::CreateCompatibleDC(0);
::SelectObject(m_hdc.GetValue(),m_h);
}
SIZE size={0,0};
::GetTextExtentPoint32W(m_hdc.GetValue(),str.ReadW(),str.GetLen(),&size);
return size.cx;
}
LOGFONTA CGdiFont::GetLogFontA()const
{
LOGFONTA lfa;
ZeroMemory(&lfa,sizeof(lfa));
CStr tmp=m_lf.lfFaceName;
memcpy(lfa.lfFaceName,TmpCharArrayA(tmp),Min(LF_FACESIZE,tmp.GetLen()*2));
lfa.lfHeight = m_lf.lfHeight;
lfa.lfWeight = m_lf.lfWeight;
lfa.lfItalic = m_lf.lfItalic;
lfa.lfUnderline = m_lf.lfUnderline;
lfa.lfCharSet = m_lf.lfCharSet;
return lfa;
}
}// end of namespace inst
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -