SizeF GDIPluseExt::GetTextBounds(const Font& font,const StringFormat& strFormat,CString szText) { #ifndef _UNICODE //字符转换 int str_len = szText.GetLength(); WCHAR* pstr_w = new WCHAR[str_len]; MultiByteToWideChar(CP_ACP,0,szText.GetBuffer(),-1,pstr_w,str_len); szText.ReleaseBuffer(); GraphicsPath graphicsPathObj; FontFamily fontfamily; font.GetFamily(&fontfamily); graphicsPathObj.AddString(pstr_w,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); DEL_P(pstr_w); #else GraphicsPath graphicsPathObj; FontFamily fontfamily; font.GetFamily(&fontfamily); graphicsPathObj.AddString(szText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); #endif RectF rcBound; // 获取边界范围 graphicsPathObj.GetBounds(&rcBound); // 返回文本的宽高 return SizeF(rcBound.Width,rcBound.Height); }
void GlyphInfoPanel::UpdateInfo() { if (m_entry.IsOK()) { Font font = FontManager::GetFont(m_entry.GetFamily(), m_entry.GetStyle(), m_entry.GetSize()); font.SelectEncoding(m_entry.GetEncodingID()); wxUint32 glyph = font.GetGlyphIndex(m_entry.GetCode()); m_codeLabel->SetLabel(wxString::Format(_("%s, 0x%x"), m_entry.GetEncoding(), m_entry.GetCode())); m_fontLabel->SetLabel(wxString::Format(_("%s, %s, %g"), font.GetFamily(), font.GetStyle(), font.GetSize())); wxSize size = font.GetGlyphSize(glyph); int width, height; int dataSize = font.GetGlyphBitmap(glyph, NULL, &width, &height); m_sizeLabel->SetLabel(wxString::Format(_("Size: %dpx x %dpx"), width, height)); m_dataSizeLabel->SetLabel(wxString::Format(_("Data size: %d bytes"), dataSize)); } else { m_codeLabel->SetLabel(_("")); m_fontLabel->SetLabel(_("")); m_sizeLabel->SetLabel(_("Size: ")); m_dataSizeLabel->SetLabel(_("Data size: ")); } }
// 获取文字需要的显示区域 Size GetTextBounds(const Font& font,const StringFormat& strFormat,const CString& strText) { GraphicsPath path; FontFamily fontfamily; font.GetFamily(&fontfamily); BSTR bsText = strText.AllocSysString(); path.AddString(bsText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); ::SysFreeString(bsText); RectF rcBound; path.GetBounds(&rcBound); REAL rHeight = font.GetHeight(0.0f); return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), (int)(rHeight > (int)rHeight ? rHeight + 4 : rHeight + 1)); //return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), // (int)(rcBound.Height > (int)rcBound.Height ? rcBound.Height + 2 : rcBound.Height + 1)); }
// 获取文字需要的显示区域(使用非换行的默认格式) Size GetTextBounds(const Font& font,const CString& strText) { StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); GraphicsPath path; FontFamily fontfamily; font.GetFamily(&fontfamily); BSTR bsText = strText.AllocSysString(); path.AddString(bsText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); ::SysFreeString(bsText); RectF rcBound; path.GetBounds(&rcBound); REAL rHeight = font.GetHeight(0.0f); return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), (int)(rHeight > (int)rHeight ? rHeight + 4 : rHeight + 1)); //return Size((int)(rcBound.Width > (int)rcBound.Width ? rcBound.Width + 1 : rcBound.Width), // (int)(rcBound.Height > (int)rcBound.Height ? rcBound.Height + 2 : rcBound.Height + 1)); }