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); }
//************************************************************************* // Method: MeasureDisplayString // Description: Gets the size of a string in pixels // // Parameters: // graphics - the graphics object the string will be measured on // test - the string to measure // font - the font to measure the string in // // Return Value: the size of the string //************************************************************************* SizeF StringTools::MeasureDisplayString(Graphics *graphics, String *text, Font *font) { const int width = 32; Bitmap *bitmap = new Bitmap(width, 1, graphics); SizeF size = graphics->MeasureString(text, font); Graphics *g = Graphics::FromImage(bitmap); int measuredWidth = (int)size.Width; if (g) { g->Clear(Color::White); g->DrawString(String::Concat(text, "|"), font, Brushes::Black, (float)(width - measuredWidth), (float)(0 - (font->Height / 2))); for (int i = width - 1; i >= 0; i--) { measuredWidth--; if (bitmap->GetPixel(i, 0).R == 0) { break; } } } return SizeF((float)measuredWidth, size.Height); }
static void drawoptions_func_diffall(const ldraw::DrawOptions& options) { CHECK(options.draw_colour == Colour(1,2,3,4)); CHECK(options.draw_origin == ldraw::CENTER); CHECK(options.draw_region == BBoxF(1,2,3,4)); CHECK(options.draw_scale == SizeF(1,2)); CHECK(options.draw_angle == 1.0f); CHECK(options.draw_frame == 1.0f); }
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); }
void SonarMonitor::draw() { Point point = m_hud->project(m_point); int radius = (m_hud->project(m_point + Point(0, m_radius)) - point).y; float scale = static_cast<float>(radius)/m_scale; PointF center = PointF(point) - PointF(1.5f); RectF rect = RectF(center, SizeF(4, 4)); Matrix m(1); m = glm::scale(m, Vector3D(1, -1, 0)); m = glm::rotate(m, m_hud->scenario()->yaw(), Vector3D(0, 0, 1)); m = glm::translate(m, -m_hud->scenario()->position()); m_hud->fontGreen().draw("T", point + Point(-2, -radius)); m_hud->fontGreen().draw(QString("%1M").arg(m_scale), Rect(point + Point(-100, radius - 8), SizeF(200, -1)), true, false); m_center.draw(rect); for (fight::NavPoint *navPoint : m_hud->scenario()->navPoints()) if (navPoint->isEnabled()) { Vector2D dir = Vector2D(m * Vector4D(navPoint->position(), 1)); float distance = glm::length(dir); if (distance < m_scale) { rect.setPos(center - dir*scale); m_nav.draw(rect); } } for (const auto &entry : m_hud->scenario()->sonar()) { Vector2D dir = Vector2D(m * Vector4D(entry.object->position(), 1)); float distance = glm::length(dir); if (distance < m_scale) { rect.setPos(center - dir*scale); (entry.isFriend ? m_friend : m_enemy).draw(rect); } } fight::Target &target = m_hud->scenario()->target(); if (target.isLocked()) { Vector2D dir = Vector2D(m * Vector4D(target.position(), 1)); float distance = glm::length(dir); if (distance < m_scale) { rect.setPos(center - dir*scale); m_target.draw(rect); } } }
SizeF Font::MeasureText(std::string const& text) const { float width = 0; float maxHeight = 0; for (int i = 0; i < text.size(); i++) { char character = text[i]; if (!this->ContainsCharacter(character)) { character = '?'; } if (i != 0) { width += this->GetKerning(text[i - 1], character).X; } auto& characterDefinition = this->GetCharacterDefinition(character); width += characterDefinition.Advance.X; maxHeight = FlaiMath::Max(maxHeight, float(characterDefinition.TextureUV.Height)); // TODO: characterDefinition.OffsetFromTop is not being accounted for ATM!! } return SizeF(width, maxHeight); }
void CUISliderctrl::DoPaint(HDC hDC, const RECT& rcPaint) { // Draw button UINT uState = m_uButtonState; if( IsFocused() ) uState |= UISTATE_FOCUSED; if( !IsEnabled() ) uState |= UISTATE_DISABLED; // Ìî³ä±³¾°ÑÕÉ« if (m_colorBk != INVALID__UICOLOR) { CUIBlueRenderEngine::DoFillRect(hDC, m_pManager, m_rcItem, m_colorBk); } // »¿Ì¶È int nPageCount = (m_nPosMax - m_nPosMin) / m_nPageSize; int nButtonWidth = 0; if (m_pImageSlider != NULL) { nButtonWidth = m_pImageSlider->GetWidth() / 4; } int nItemWidth = m_rcItem.right - m_rcItem.left - nButtonWidth; // »¿Ì¶È if (m_colorScale != INVALID__UICOLOR) { for (int i = 0; i <= nPageCount; i++) { POINT pt1 = {m_rcItem.left + i * ((double)nItemWidth / nPageCount) + nButtonWidth / 2, m_rcItem.top}; POINT pt2 = {m_rcItem.left + i * ((double)nItemWidth / nPageCount) + nButtonWidth / 2, m_rcItem.top + m_nScaleHeight}; CUIBlueRenderEngine::DoPaintLine(hDC, m_pManager, pt1, pt2, m_colorScale); } } // »Öá if (m_pImageShaft != NULL) { int nShaftHeight = m_pImageShaft->GetHeight(); int nTop = m_rcItem.top + (m_rcItem.bottom - m_rcItem.top - nShaftHeight - m_nScaleHeight) / 2 + m_nScaleHeight; RECT rcPadding = {m_rcItem.left + nButtonWidth / 2, nTop, m_rcItem.right - nButtonWidth / 2, nTop + nShaftHeight}; RECT rcImage= {0, 0, m_pImageShaft->GetWidth(), m_pImageShaft->GetHeight()}; CUIBlueRenderEngine::DoFillRect(hDC, m_pManager, rcPadding, rcImage, m_pImageShaft); } if (m_pImageSlider != NULL) { int nWidth = m_pImageSlider->GetWidth() / 4; int nHeight = m_pImageSlider->GetHeight(); int nIndex = 0; // Draw frame and body if( (uState & UISTATE_DISABLED) != 0 ) { nIndex = 3;; } else if( (uState & UISTATE_PUSHED) != 0 ) { nIndex = 2; } else if( (uState & UISTATE_HOT) != 0 ) { nIndex = 1; } else { nIndex = 0; } Graphics graph(hDC); RECT rcImage = {nWidth *nIndex, 0, nWidth * (nIndex + 1), nHeight}; double dblPos = (double)(m_nPos - m_nPosMin) / (double)(m_nPosMax - m_nPosMin); int nButtonPosX = m_rcItem.left + (m_rcItem.right - m_rcItem.left - nWidth) * dblPos; if (nButtonPosX < m_rcItem.left) { nButtonPosX = m_rcItem.left; } if (nButtonPosX > m_rcItem.right - nWidth) { nButtonPosX = m_rcItem.right - nWidth; } int nButtonPosY = m_rcItem.top + ((m_rcItem.bottom - m_rcItem.top) - nHeight - m_nScaleHeight)/ 2 + m_nScaleHeight; m_rcButton.left = nButtonPosX; m_rcButton.right = m_rcButton.left + nWidth; m_rcButton.top = nButtonPosY; m_rcButton.bottom = m_rcButton.top + nHeight; RectF rfDest(PointF((REAL)nButtonPosX, (REAL)nButtonPosY), SizeF((REAL)nWidth, (REAL)nHeight)); graph.DrawImage(m_pImageSlider, rfDest, (REAL)rcImage.left, (REAL)rcImage.top, (REAL)nWidth, (REAL)nHeight, UnitPixel, NULL); } }
void CUILabel::DoPaint(HDC hDC, const RECT& rcPaint) { if (m_BackColor != INVALID__UICOLOR) { CUIBlueRenderEngine::DoFillRect(hDC, m_pManager, m_rcItem, m_BackColor); } if (m_BorderColorOutside != INVALID__UICOLOR) { RECT rect = {m_rcItem.left + 1, m_rcItem.top + 1, m_rcItem.right - 1, m_rcItem.bottom - 1}; CUIBlueRenderEngine::DoPaintBorder(hDC, m_pManager, m_rcItem, m_BorderColorOutside); } if (m_BorderColorInside != INVALID__UICOLOR) { RECT rect = {m_rcItem.left + 1, m_rcItem.top + 1, m_rcItem.right - 1, m_rcItem.bottom - 1}; CUIBlueRenderEngine::DoPaintBorder(hDC, m_pManager, rect, m_BorderColorInside); } if (m_pImageOwner != NULL && m_nIndex >= 0) { RECT rc = m_rcItem; Graphics graph(hDC); int nStartX = rc.left; int nStartY = rc.top; int nImageWidth = m_pImageOwner->GetWidth() / m_nImageCount; int nImageHeight = m_pImageOwner->GetHeight(); double dblScal = (double)nImageWidth / nImageHeight; int nRectWidth = nImageWidth; int nRectHeight = nImageHeight; if (m_sizeAnimate.cx != 0 && m_sizeAnimate.cy != 0) { nRectWidth = m_sizeAnimate.cx; nRectHeight = m_sizeAnimate.cy; } if ((m_rcItem.bottom - m_rcItem.top) * dblScal < (m_rcItem.right - m_rcItem.left)) { nRectWidth = (int)(nRectHeight * dblScal); } else { nRectHeight = (int)(nRectWidth / dblScal); } nStartX = rc.left + (rc.right - rc.left - nRectWidth) / 2; if (nStartX < rc.left) { nStartX = rc.left; nRectWidth = rc.right - rc.left; } nStartY = rc.top + (rc.bottom - rc.top - nRectHeight) / 2; if (nStartY < rc.top) { nStartY = rc.top; nRectHeight = rc.bottom - rc.top; } RectF rfDest(PointF((REAL)nStartX, (REAL)nStartY), SizeF((REAL)nRectWidth, (REAL)nRectHeight)); if (m_fAlpha <= 1.0) { ColorMatrix colorMatrix = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, m_fAlpha, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; ImageAttributes imageAtt; imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); graph.DrawImage(m_pImageOwner, rfDest, (REAL)nImageWidth * m_nIndex, (REAL)0, (REAL)nImageWidth, (REAL)nImageHeight, UnitPixel, &imageAtt); } else { graph.DrawImage(m_pImageOwner, rfDest, (REAL)nImageWidth * m_nIndex, (REAL)0, (REAL)nImageWidth, (REAL)nImageHeight, UnitPixel, NULL); } if (m_bImageAsLinker && m_sText.GetLength() == 0) { m_nLinks = 1; m_rcLinks[0] = CUIRect(nStartX, nStartY, nStartX + nRectWidth, nStartY + nRectHeight); } } if (m_sText.GetLength() > 0) { RECT rcText = m_rcItem; m_nLinks = lengthof(m_rcLinks); COLORREF color = m_TextColor; if (!IsEnabled()) { color = m_TextColor_Disable; } CUIBlueRenderEngine::DoPaintPrettyText(hDC, m_pManager, rcText, m_sText, color, INVALID__UICOLOR, m_rcLinks, m_nLinks, m_uTextStyle); if (m_nLinks == 1 && m_sText.GetLength() > 4) { if ((m_sText.GetAt(0) == _T('<') && m_sText.GetAt(1) == _T('a')) && (m_sText.GetAt(m_sText.GetLength() - 1) == _T('>') && m_sText.GetAt(m_sText.GetLength() - 2) == _T('a')) ) { if (m_rcLinks[0].right - m_rcLinks[0].left < 20) { m_rcLinks[0].left = m_rcItem.left; m_rcLinks[0].right = m_rcItem.right; } } } } }
inline SizeF Size::toSizeF() const { return SizeF( float(x), float(y) ); }
SizeF operator+(const SizeF& other) const { return SizeF( x + other.x, y + other.y ); }