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); }
SizeF GDIPluseExt::GetBounds(CString strText,CString strFont,INT nfontsize) { StringFormat strformat; GraphicsPath path; #ifdef _UNICODE FontFamily fontFamily(strFont); path.AddString(strText,strText.GetLength(), &fontFamily, FontStyleRegular, (REAL)nfontsize, PointF(0,0), &strformat ); #else //字符转换 int font_len = strFont.GetLength(); WCHAR* pfont_w = new WCHAR[font_len]; MultiByteToWideChar(CP_ACP,0,strFont.GetBuffer(),-1,pfont_w,font_len); strFont.ReleaseBuffer(); //字符转换 int text_len = strText.GetLength(); WCHAR* ptext_w = new WCHAR[text_len]; MultiByteToWideChar(CP_ACP,0,strText.GetBuffer(),-1,ptext_w,text_len); strText.ReleaseBuffer(); FontFamily fontFamily(pfont_w); Font font(&fontFamily, (REAL)nfontsize, FontStyleRegular, UnitPixel); path.AddString(ptext_w,wcsnlen_s(ptext_w,text_len), &fontFamily, font.GetStyle(), font.GetSize(), PointF(0,0), &strformat ); DEL_P(ptext_w); DEL_P(pfont_w); #endif RectF rcBound; // 获取边界范围 path.GetBounds(&rcBound); TRACE("Round_Size:%d\r\n",rcBound.Width); // 返回文本的宽高 return SizeF(rcBound.Width,rcBound.Height); }
wxGraphicsPath OOPDesktopLyric::InitLineTextPath(wxGraphicsContext *gc) { wxASSERT(gc); // 文本轮廓 wxGraphicsPen pen = gc->CreatePen(wxPen(wxColour(0,0,0,255), 3)); const wxString textToDraw(IsCurrLineValid() ? (*m_currLine)->GetLyric() : GetInteractiveOutput()); wxGraphicsPath path = gc->CreatePath(); //======================================== // 添加文本 FontFamily fontFamily(m_style.fontFace); StringFormat stringFormat; wxCoord winHeight; GetSize(NULL, &winHeight); wxASSERT(size_t(winHeight) >= m_style.pxFontSize); GraphicsPath *nativePath = (GraphicsPath *) path.GetNativePath(); nativePath->AddString(textToDraw, -1, &fontFamily, m_style.bold ? FontStyleBold : FontStyleRegular, m_style.pxFontSize, Point(0, (winHeight - m_style.pxFontSize) / 2), &stringFormat); WXGDIPlusPenData *penImpl = (WXGDIPlusPenData *) pen.GetGraphicsData(); RectF bounds; nativePath->GetBounds(&bounds, NULL, penImpl->m_pen); m_textPathBounds.m_x = bounds.X; m_textPathBounds.m_y = bounds.Y; m_textPathBounds.m_width = bounds.Width; m_textPathBounds.m_height = bounds.Height; path.UnGetNativePath(nativePath); //======================================== // 添加到路径中 gc->SetPen(pen); gc->StrokePath(path); return path; }
int MusicUtils::TestStringCx(const wchar_t *szText) { //CDC tdc; //tdc.CreateCompatibleDC(CDC::FromHandle(::GetDC(0))); //HBITMAP hbitmap = CreateCompatibleBitmap(tdc.m_hDC,600,15); //tdc.SelectObject(hbitmap); //return tdc.GetOutputTextExtent(szText).cx; GraphicsPath path; FontFamily fontfamily; Font font(L"宋体",14); font.GetFamily(&fontfamily); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); path.AddString(szText,-1,&fontfamily,font.GetStyle(),font.GetSize(),PointF(0,0),&strFormat); RectF rcBound; path.GetBounds(&rcBound); return rcBound.Width; }
// 获取文字需要的显示区域 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)); }
int Window::drawLine(HDC hdc, int oy, std::wstring string, TextFont* fnt) { using namespace Gdiplus; int x = (showed ? 0 : leftMargin) - 5; int y = (showed ? 0 : topMargin) + oy; const wchar_t* text = string.c_str(); if (fnt == 0) fnt = font; Graphics graphics(hdc); graphics.SetSmoothingMode(SmoothingModeAntiAlias); FontFamily fontFamily; fnt->GetFamily(&fontFamily); RectF boundRect = fnt->getTextBounds(hdc, clientRect, text); x += ((clientRect.right - clientRect.left) - (int)(boundRect.GetRight() - boundRect.GetLeft()) - 2) / 2; y -= ((int)boundRect.GetBottom()) + 5; StringFormat strformat; GraphicsPath path; path.AddString(text, wcslen(text), &fontFamily, fnt->GetStyle(), graphics.GetDpiY() * fnt->GetSize() / 72, Gdiplus::Point(x, y), &strformat ); // Outline color + size Pen pen(Color(0, 0, 0), fnt->GetSize()/7); pen.SetLineJoin(LineJoinRound); graphics.DrawPath(&pen, &path); // Text color SolidBrush brush(Color(254, 254, 254)); graphics.FillPath(&brush, &path); Rect bounds; path.GetBounds(&bounds, 0, &pen); return (int)boundRect.GetBottom(); }