/// <summary> /// Paints text for given style. /// </summary> /// <param name="e">Display information.</param> /// <param name="text"><Text to paint./param> /// <param name="defaultFont">Default font if no font by style is specified.</param> /// <param name="useDefaultFont">Specifies whether to use default font for painting regardless of style settings.</param> public static void PaintText(ElementStyleDisplayInfo e, string text, Font defaultFont, bool useDefaultFont, eTextFormat textFormat) { Rectangle textBounds = e.Bounds; ElementStyle style = GetElementStyle(e.Style); if (text == "" || textBounds.IsEmpty || style.TextColor.IsEmpty) return; Font font = style.Font; if (font == null || useDefaultFont) font = defaultFont; textBounds.X += style.MarginLeft; textBounds.Width -= (style.MarginLeft + style.MarginRight); textBounds.Y += style.MarginTop; textBounds.Height -= (style.MarginTop + style.MarginBottom); if (!style.TextShadowColor.IsEmpty && Math.Abs(style.TextShadowColor.GetBrightness() - style.TextColor.GetBrightness()) > .2) { using (SolidBrush brush = new SolidBrush(style.TextShadowColor)) { Rectangle r = textBounds; r.Offset(style.TextShadowOffset); TextDrawing.DrawString(e.Graphics, text, font, style.TextShadowColor, r, textFormat); //e.Graphics.DrawString(text,font,brush,r,style.StringFormat); } } if (!style.TextColor.IsEmpty) TextDrawing.DrawString(e.Graphics, text, font, style.TextColor, textBounds, textFormat); }
protected override Rectangle GetTextRectangle(ButtonItem button, ItemPaintArgs pa, eTextFormat stringFormat, CompositeImage image) { Rectangle r = base.GetTextRectangle(button, pa, stringFormat, image); if (image == null && string.IsNullOrEmpty(button.Symbol)) r.Inflate(-3, 0); return r; }
protected override Rectangle GetTextRectangle(ButtonItem button, ItemPaintArgs pa, eTextFormat stringFormat, CompositeImage image) { Rectangle r = base.GetTextRectangle(button, pa, stringFormat, image); r.Offset(0, 12); //r.Height -= 18; return r; }
public static void DrawString(Graphics g, string text, Font font, Color color, Rectangle bounds, eTextFormat format) { if (UseTextRenderer && (format & eTextFormat.Vertical) == 0) TextRenderer.DrawText(g, text, font, bounds, color, GetTextFormatFlags(format)); else DrawStringLegacy(g, text, font, color, bounds, format); }
public static void DrawStringLegacy(Graphics g, string text, Font font, Color color, Rectangle bounds, eTextFormat format) { if (color.IsEmpty || !TextDrawingEnabled) return; using (SolidBrush brush = new SolidBrush(color)) { using (StringFormat sf = GetStringFormat(format)) g.DrawString(text, font, brush, bounds, sf); } }
/// <summary> /// MeasureText always adds about 1/2 em width of white space on the right, /// even when NoPadding is specified. It returns zero for an empty string. /// To get the precise string width, measure the width of a string containing a /// single period and subtract that from the width of our original string plus a period. /// </summary> public static Size MeasureText(Graphics g, string s, Font font, Size csize, eTextFormat tf) { return (TextDrawing.MeasureString(g, s, font, csize, tf)); if (font.Italic == true) return (TextDrawing.MeasureString(g, s, font, csize, tf)); Size sz1 = TextDrawing.MeasureString(g, ".", font, csize, tf); Size sz2 = TextDrawing.MeasureString(g, s + ".", font, csize, tf); return (new Size(sz2.Width - sz1.Width, sz2.Height)); }
public static Size MeasureString(Graphics g, string text, Font font, Size proposedSize, eTextFormat format) { if (UseTextRenderer && (format & eTextFormat.Vertical) == 0) { format = format & ~(format & eTextFormat.VerticalCenter); // Bug in .NET Framework 2.0 format = format & ~(format & eTextFormat.Bottom); // Bug in .NET Framework 2.0 format = format & ~(format & eTextFormat.HorizontalCenter); // Bug in .NET Framework 2.0 format = format & ~(format & eTextFormat.Right); // Bug in .NET Framework 2.0 format = format & ~(format & eTextFormat.EndEllipsis); // Bug in .NET Framework 2.0 return (Size.Ceiling(TextRenderer.MeasureText(g, text, font, proposedSize, GetTextFormatFlags(format)))); } using (StringFormat sf = GetStringFormat(format)) return Size.Ceiling(g.MeasureString(text, font, proposedSize, sf)); }
private static TextFormatFlags GetTextFormatFlags(eTextFormat format) { format |= eTextFormat.PreserveGraphicsTranslateTransform | eTextFormat.PreserveGraphicsClipping; if ((format & eTextFormat.SingleLine) == eTextFormat.SingleLine && (format & eTextFormat.WordBreak) == eTextFormat.WordBreak) { format = format & ~(format & eTextFormat.SingleLine); } return (TextFormatFlags)format; }
public static Size MeasureStringLegacy(Graphics g, string text, Font font, Size proposedSize, eTextFormat format) { using (StringFormat sf = GetStringFormat(format)) return (g.MeasureString(text, font, proposedSize, sf).ToSize()); }
public static Size MeasureString(Graphics g, string text, Font font, int proposedWidth, eTextFormat format) { return MeasureString(g, text, font, new Size(proposedWidth, 0), format); }
public static void DrawString(Graphics g, string text, Font font, Color color, int x, int y, eTextFormat format) { DrawString(g, text, font, color, new Rectangle(x, y, 0, 0), format); }
public static Size MeasureItemText(BaseItem item, Graphics g, int containerWidth, Font font, eTextFormat stringFormat, bool rightToLeft) { return MeasureItemText(item, g, containerWidth, font, stringFormat, rightToLeft, false, eImagePosition.Left); }
private void CreateStringFormat() { eTextFormat sfmt = eTextFormat.Default; if (this.ContainerControl is ItemControl && !(this.Owner!=null && this.Owner.AlwaysDisplayKeyAccelerators)) sfmt |= eTextFormat.HidePrefix; else if(!((this.Owner!=null && this.Owner.AlwaysDisplayKeyAccelerators) || NativeFunctions.ShowKeyboardCues || this.IsOnMenu)) { Bar bar=this.ContainerControl as Bar; if (!((this.ContainerControl != null && this.ContainerControl.Focused) || (bar != null && bar.MenuFocus) || (bar != null && bar.AlwaysDisplayKeyAccelerators) || this.ContainerControl is NavigationBar)) sfmt |= eTextFormat.HidePrefix; } if(this.ContainerControl is RibbonBar) sfmt |= eTextFormat.VerticalCenter; else sfmt |= eTextFormat.SingleLine | eTextFormat.VerticalCenter; if (!TextDrawing.UseTextRenderer) sfmt |= eTextFormat.EndEllipsis; this.ButtonStringFormat=sfmt; }
public static ThemeTextFormat GetTextFormat(eTextFormat format) { ThemeTextFormat ttf = ThemeTextFormat.Left; if (format == eTextFormat.Default) return ttf; if ((format & eTextFormat.HorizontalCenter) == eTextFormat.HorizontalCenter) ttf |= ThemeTextFormat.Center; else if ((format & eTextFormat.Right) == eTextFormat.Right) ttf|= ThemeTextFormat.Right; if ((format & eTextFormat.VerticalCenter) == eTextFormat.VerticalCenter) ttf |= ThemeTextFormat.VCenter; else if ((format & eTextFormat.Bottom) == eTextFormat.Bottom) ttf |= ThemeTextFormat.Bottom; if ((format & eTextFormat.EndEllipsis) == eTextFormat.EndEllipsis) ttf |= ThemeTextFormat.EndEllipsis; if ((format & eTextFormat.HidePrefix) == eTextFormat.HidePrefix) ttf |= ThemeTextFormat.HidePrefix; else if ((format & eTextFormat.NoPrefix) == eTextFormat.NoPrefix) ttf |= ThemeTextFormat.NoPrefix; if ((format & eTextFormat.WordBreak) == eTextFormat.WordBreak) ttf |= ThemeTextFormat.WordBreak; else ttf |= ThemeTextFormat.SingleLine; if ((format & eTextFormat.RightToLeft) == eTextFormat.RightToLeft) ttf |= ThemeTextFormat.RtlReading; if ((format & eTextFormat.NoClipping) == eTextFormat.NoClipping) ttf |= ThemeTextFormat.NoClip; return ttf; }
private Rectangle AlignText(Rectangle r, out eTextFormat strFormat) { strFormat = eTextFormat.Default | eTextFormat.SingleLine | eTextFormat.EndEllipsis | eTextFormat.VerticalCenter; eItemAlignment alignment = TabItem.TextAlignment.HasValue ? TabItem.TextAlignment.Value : TabItem.TabStripItem.TextAlignment; switch (alignment) { case eItemAlignment.Center: strFormat |= eTextFormat.HorizontalCenter; if (TabItem.IsVertical == true) { r.X -= TabStripItem.TabHorizontalSpacing; r.Width += TabStripItem.TabHorizontalSpacing; } break; case eItemAlignment.Far: strFormat |= eTextFormat.Right; if (TabItem.IsVertical == true) { r.X -= 3; r.Width -= 3; } break; } return (r); }
protected virtual Rectangle GetTextRectangle(ButtonItem button, ItemPaintArgs pa, eTextFormat stringFormat, CompositeImage image) { Graphics g = pa.Graphics; bool isOnMenu = IsOnMenu(button, pa); bool isOnMenuBar = pa.IsOnMenuBar; Rectangle itemRect = button.DisplayRectangle; Rectangle textRect = button.TextDrawRect; Rectangle imageRect = button.ImageDrawRect; bool rightToLeft = pa.RightToLeft; // Draw menu item text if (isOnMenu || button.ButtonStyle != eButtonStyle.Default || image == null && string.IsNullOrEmpty(button.Symbol) || (!isOnMenu && (button.ImagePosition == eImagePosition.Top || button.ImagePosition == eImagePosition.Bottom))) { if (isOnMenu) { if (rightToLeft) { //rect = new Rectangle(26, button.TextDrawRect.Y, itemRect.Width - button.ImageDrawRect.Width - 28, button.TextDrawRect.Height); // This seems to give a better alignment match with the corresponding LTR alignment if(button.IsOnCustomizeMenu) textRect = new Rectangle(itemRect.Height, textRect.Y, itemRect.Width - imageRect.Width - 11 - itemRect.Height, textRect.Height); else textRect = new Rectangle(17, textRect.Y, itemRect.Width - imageRect.Width - 28, textRect.Height); } else textRect = new Rectangle(textRect.X, textRect.Y, itemRect.Width - imageRect.Right - 26, textRect.Height); } else { //rect = button.TextDrawRect; if (button.ImagePosition == eImagePosition.Top || button.ImagePosition == eImagePosition.Bottom) { if (button.Orientation != eOrientation.Vertical) { textRect = new Rectangle(1, textRect.Y + 2, itemRect.Width - 2, textRect.Height); if (button.SplitButton || pa.ContainerControl is RibbonBar) textRect.Y += 2; } } } if (image == null && string.IsNullOrEmpty(button.Symbol) && (stringFormat & eTextFormat.HorizontalCenter) != eTextFormat.HorizontalCenter && !isOnMenu && !isOnMenuBar && textRect.X == 0 && !pa.RightToLeft) textRect.X = 3; textRect.Offset(itemRect.Left, itemRect.Top); if (button.Orientation == eOrientation.Vertical && !isOnMenu) { if (textRect.Bottom > itemRect.Bottom) textRect.Height = itemRect.Bottom - textRect.Y; } else { if (textRect.Right > itemRect.Right) textRect.Width = itemRect.Right - textRect.Left; } } return textRect; }
private void DrawThemedTab(Graphics g, Rectangle tabRect, TabItem tab, eTextFormat strFormat, Rectangle rClip, bool drawBack, bool drawInner) { TabColors tabColor=this.GetTabColors(tab); ThemeTabStates tabState=ThemeTabStates.Normal; if(m_HotTab==tab) tabState=ThemeTabStates.Hot; else if(tab==m_SelectedTab) tabState=ThemeTabStates.Selected; if(drawBack) { if(tab==m_SelectedTab) m_ThemeTab.DrawBackground(g,ThemeTabParts.TabItem,tabState,new Rectangle(tabRect.X,tabRect.Y,tabRect.Width,tabRect.Height+1)); else m_ThemeTab.DrawBackground(g,ThemeTabParts.TabItem,tabState,new Rectangle(tabRect.X,tabRect.Y,tabRect.Width,tabRect.Height+1)); } tabRect.Offset(0,1); if(drawInner) { if (m_CloseButtonOnTabs && tab.CloseButtonVisible) { Rectangle rc = PaintTabItemCloseButton(g, (m_Alignment == eTabStripAlignment.Left || m_Alignment == eTabStripAlignment.Right), tab.CloseButtonMouseOver, tab == m_HotTab || tab == this.SelectedTab, ref tabRect); rc.Offset(tab.DisplayRectangle.Location); tab.CloseButtonBounds = rc; } else tab.CloseButtonBounds = Rectangle.Empty; Image tabImage=tab.GetImage(); Icon icon=tab.Icon; if(tabImage!=null && tabImage.Width+4<=tabRect.Width || icon!=null && tab.IconSize.Width+4<=tabRect.Width) { if(icon!=null) { Rectangle rIcon=new Rectangle(tabRect.X+4,tabRect.Y+(tabRect.Height-tab.IconSize.Height)/2,tab.IconSize.Width,tab.IconSize.Height); if(rClip.Contains(rIcon)) g.DrawIcon(icon,rIcon); tabRect.X+=(tab.IconSize.Width+2); tabRect.Width-=(tab.IconSize.Width+2); } else if(tabImage!=null) { g.DrawImage(tabImage,tabRect.X+4,tabRect.Y+(tabRect.Height-tabImage.Height)/2,tabImage.Width,tabImage.Height); tabRect.X+=(tabImage.Width+2); tabRect.Width-=(tabImage.Width+2); } tabRect.Inflate(0,-1); tabRect.Width-=4; tabRect.X+=3; } if(!m_DisplaySelectedTextOnly || tab==m_SelectedTab) { Font font=this.Font; if(tab==m_SelectedTab && m_SelectedTabFont!=null) font=m_SelectedTabFont; if(tabRect.Width>MIN_TEXT_WIDTH) TextDrawing.DrawString(g,tab.Text,font,tabColor.TextColor,tabRect,strFormat); if(m_ShowFocusRectangle && this.Focused && tab==m_SelectedTab) ControlPaint.DrawFocusRectangle(g,GetFocusRectangle(tabRect)); } } }
public static Size MeasureItemText(BaseItem item, Graphics g, int containerWidth, Font font, eTextFormat stringFormat, bool rightToLeft, bool ribbonBarButton, eImagePosition imagePosition) { if (item.Text == "" && item.TextMarkupBody == null) return Size.Empty; Size textSize = Size.Empty; if (item.TextMarkupBody == null) { textSize = TextDrawing.MeasureString(g, ButtonItemPainter.GetDrawText(item.Text), font, containerWidth, stringFormat); } else { Size availSize = new Size(containerWidth, 1); if (containerWidth == 0) availSize.Width = 1600; TextMarkup.MarkupDrawContext d = new TextMarkup.MarkupDrawContext(g, font, Color.Empty, false); item.TextMarkupBody.Measure(availSize, d); availSize = item.TextMarkupBody.Bounds.Size; if (containerWidth != 0 && !(ribbonBarButton && imagePosition == eImagePosition.Top)) availSize.Width = containerWidth; d.RightToLeft = rightToLeft; item.TextMarkupBody.Arrange(new Rectangle(0, 0, availSize.Width, availSize.Height), d); textSize = item.TextMarkupBody.Bounds.Size; } return textSize; }
public static StringFormat GetStringFormat(eTextFormat format) { StringFormat sf = new StringFormat(UseGenericDefault ? StringFormat.GenericDefault : StringFormat.GenericTypographic); if (format == eTextFormat.Default) return sf; if ((format & eTextFormat.HorizontalCenter) == eTextFormat.HorizontalCenter) sf.Alignment = StringAlignment.Center; else if ((format & eTextFormat.Right) == eTextFormat.Right) sf.Alignment = StringAlignment.Far; if ((format & eTextFormat.VerticalCenter) == eTextFormat.VerticalCenter) sf.LineAlignment = StringAlignment.Center; else if ((format & eTextFormat.Bottom) == eTextFormat.Bottom) sf.LineAlignment = StringAlignment.Far; if ((format & eTextFormat.EndEllipsis) == eTextFormat.EndEllipsis) sf.Trimming = StringTrimming.EllipsisCharacter; else sf.Trimming = StringTrimming.Character; if ((format & eTextFormat.HidePrefix) == eTextFormat.HidePrefix) sf.HotkeyPrefix = HotkeyPrefix.Hide; else if ((format & eTextFormat.NoPrefix) == eTextFormat.NoPrefix) sf.HotkeyPrefix = HotkeyPrefix.None; else sf.HotkeyPrefix = HotkeyPrefix.Show; if ((format & eTextFormat.WordBreak) == eTextFormat.WordBreak) sf.FormatFlags = sf.FormatFlags & ~(sf.FormatFlags & StringFormatFlags.NoWrap); else sf.FormatFlags |= StringFormatFlags.NoWrap; if ((format & eTextFormat.LeftAndRightPadding) == eTextFormat.LeftAndRightPadding) sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; if ((format & eTextFormat.RightToLeft) == eTextFormat.RightToLeft) sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft; if ((format & eTextFormat.Vertical) == eTextFormat.Vertical) sf.FormatFlags |= StringFormatFlags.DirectionVertical; if ((format & eTextFormat.NoClipping) == eTextFormat.NoClipping) sf.FormatFlags |= StringFormatFlags.NoClip; return (sf); }
internal static Size MeasureText(MetroTileFrame item, Graphics g, int containerWidth, Font font, eTextFormat stringFormat, bool rightToLeft) { if (item.Text == "" && item.TextMarkupBody == null) return Size.Empty; Size textSize = Size.Empty; if (item.TextMarkupBody == null) { textSize = TextDrawing.MeasureString(g, ButtonItemPainter.GetDrawText(item.Text), font, containerWidth, stringFormat); } else { Size availSize = new Size(containerWidth, 1); if (containerWidth == 0) availSize.Width = 1600; TextMarkup.MarkupDrawContext d = new TextMarkup.MarkupDrawContext(g, font, Color.Empty, false); item.TextMarkupBody.Measure(availSize, d); availSize = item.TextMarkupBody.Bounds.Size; if (containerWidth != 0) availSize.Width = containerWidth; d.RightToLeft = rightToLeft; item.TextMarkupBody.Arrange(new Rectangle(0, 0, availSize.Width, availSize.Height), d); textSize = item.TextMarkupBody.Bounds.Size; } return textSize; }