public static void DrawText(IDeviceContext dc, string text, VisualStyleElement element, Font fallbackFont, ref Point location, bool measureOnly, int width) { // For Windows 2000, using Int32.MaxValue for the height doesn't seem to work, so we'll just pick another arbitrary large value // that does work. Rectangle textRect = new Rectangle(location.X, location.Y, width, NativeMethods.IsWindowsXPOrLater ? Int32.MaxValue : 100000); TextFormatFlags flags = TextFormatFlags.WordBreak; if (IsTaskDialogThemeSupported) { VisualStyleRenderer renderer = new VisualStyleRenderer(element); Rectangle textSize = renderer.GetTextExtent(dc, textRect, text, flags); location = location + new Size(0, textSize.Height); if (!measureOnly) { renderer.DrawText(dc, textSize, text, false, flags); } } else { if (!measureOnly) { TextRenderer.DrawText(dc, text, fallbackFont, textRect, SystemColors.WindowText, flags); } Size textSize = TextRenderer.MeasureText(dc, text, fallbackFont, new Size(textRect.Width, textRect.Height), flags); location = location + new Size(0, textSize.Height); } }
public static Rectangle GetTitleRect(IDeviceContext dc, Size minSize, Size maxSize, string title, ToolTipIcon icon, Padding padding) { Rectangle ret; if (title == null) { ret = new Rectangle(new Point(0, 0), minSize); } else { ret = new Rectangle(new Point(0, 0), maxSize); ret.Width -= padding.Horizontal; if (Application.RenderWithVisualStyles) { if (icon != ToolTipIcon.None) { throw new NotImplementedException(); } VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.ToolTip.BalloonTitle.Normal); Rectangle rect = renderer.GetTextExtent(dc, ret, title, TextFormatFlags.Left | TextFormatFlags.WordEllipsis | TextFormatFlags.VerticalCenter); if (rect.Width + padding.Horizontal > maxSize.Width) { ret.Width = maxSize.Width; } else if (rect.Width + padding.Horizontal < minSize.Width) { ret.Width = minSize.Width; } else { ret.Width = rect.Width; } if (rect.Height > maxSize.Height) { ret.Height = maxSize.Height; } else { ret.Height = rect.Height; } } else { throw new NotImplementedException(); } } return(ret); }
protected override Rectangle GetTitleTextRect(Graphics dc, string title, Rectangle rect) { if (IsDefined(VisualStyleElement.ToolTip.BalloonTitle.Normal)) { VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.ToolTip.BalloonTitle.Normal); return(renderer.GetTextExtent(dc, rect, title, TextFormatFlags.Left | TextFormatFlags.WordEllipsis | TextFormatFlags.VerticalCenter)); } else { SizeF size = dc.MeasureString(title, new Font(SystemFonts.DefaultFont, FontStyle.Bold), rect.Size.Width, new StringFormat(StringFormatFlags.NoWrap)); return(new Rectangle(new Point(0, 0), Size.Ceiling(size))); } }
protected override Rectangle GetBodyTextRect(Graphics dc, string text, Rectangle rect) { if (IsDefined(VisualStyleElement.ToolTip.Balloon.Normal)) { VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.ToolTip.Balloon.Normal); return(renderer.GetTextExtent(dc, rect, text, TextFormatFlags.Left | TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter)); } else { SizeF size = dc.MeasureString(text, SystemFonts.DefaultFont, rect.Size.Width, new StringFormat()); return(new Rectangle(new Point(0, 0), Size.Ceiling(size))); } }
private void PaintAccountGlyph(Graphics g, Rectangle bounds, Account account) { String fullAddress = account.FullAddress ?? "<account error>"; Bitmap image = account.Type == AccountTypes.Regular ? _iconStandard : _iconApps; int tileWidth = _userFrame.Width; int tileHeight = (int)(((float)tileWidth / (float)image.Width) * image.Height); RectangleF clipRect = new RectangleF(bounds.X + 14, bounds.Y + 13, _userFrame.Width - 30, _userFrame.Height - 30); g.SetClip(clipRect); g.DrawImage(image, bounds.X, bounds.Y + 10, tileWidth, tileHeight); g.ResetClip(); g.DrawImage(_userFrame, new Rectangle(bounds.X, bounds.Y, _userFrame.Width, _userFrame.Height)); Point addressPoint = new Point(bounds.X + _userFrame.Width, bounds.Y); Rectangle addressRect = new Rectangle(addressPoint.X, addressPoint.Y, bounds.Width - _userFrame.Width, bounds.Height); Rectangle textExtent = _rendererListSmall.GetTextExtent(g, addressRect, fullAddress, TextFormatFlags.Left); int center = (_userFrame.Height - textExtent.Height) / 2; if (account.Default) { Rectangle defaultExtent = _rendererListSmall.GetTextExtent(g, addressRect, Locale.Current.JumpList.DefaultAccount, TextFormatFlags.Left); center -= (defaultExtent.Height / 2) + 4; // 4 is the additional padding we're adding } addressRect.Offset(0, center); _rendererListSmall.DrawText(g, addressRect, fullAddress, false, TextFormatFlags.Left); if (account.Default) { addressRect.Offset(0, textExtent.Height + 4); _rendererListSmall.DrawText(g, addressRect, Locale.Current.JumpList.DefaultAccount, true, TextFormatFlags.Left); } }
public override void Layout(PaintEventArgs e) { // Get TaskDialog-MainInstructionPane var vs = new VisualStyleRenderer(taskDialogClass, 2, 0); var r = vs.GetTextExtent(e.Graphics, "W", TextFormatFlags.Default); headerHeight = (tbpadding * 2) + r.Height; // Get TaskDialog-ExpandoButton vs = new VisualStyleRenderer(taskDialogClass, 13, (int)Control.buttonState); imgSz = new Size(vs.GetInteger(IntegerProperty.Width), vs.GetInteger(IntegerProperty.Height)); buttonBounds = new Rectangle(e.ClipRectangle.Width - imgSz.Width - lrpadding, (headerHeight - imgSz.Height) / 2, imgSz.Width, imgSz.Height); textBounds = new Rectangle(lrpadding, tbpadding, e.ClipRectangle.Width - lrpadding - imgSz.Width, tbpadding + r.Height); Control.Padding = new Padding(lrpadding, headerHeight + tbpadding, lrpadding, tbpadding); System.Diagnostics.Debug.WriteLine($"Layout: hdrH={headerHeight}; imgSz={imgSz}; btnBnd={buttonBounds}; txtBnd={textBounds}"); }
private void UpdateTheme() { if (_themeElement == null) { return; } using (Graphics g = this.CreateGraphics()) { _renderer = new VisualStyleRenderer(_themeElement); Rectangle bounds = new Rectangle(0, 0, this.Width, this.Height); Rectangle textExtent = _renderer.GetTextExtent(g, bounds, this.Text, TextFormatFlags.Default); this.Size = this.MinimumSize = new Size(textExtent.Width, textExtent.Height); } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (treeView1.SelectedNode != null) { Graphics g = Graphics.FromHwnd(this.Handle); g.Clear(this.BackColor); TextY = 0; if (VisualStyleInformation.IsSupportedByOS) { DrawText(g, "VisualStyles supported by OS"); } else { DrawText(g, "VisualStyles not supported by OS."); return; } if (VisualStyleInformation.IsEnabledByUser) { DrawText(g, "VisualStyles enabled by user."); } else { DrawText(g, "VisualStyles not enabled by user."); return; } // Create the VisualStyleElement MethodInfo m = (MethodInfo)treeView1.SelectedNode.Tag; VisualStyleElement vse = (VisualStyleElement)m.Invoke(null, null); if (!VisualStyleRenderer.IsElementDefined(vse)) { DrawText(g, treeView1.SelectedNode.Text + " is not defined by the current style."); return; } try { // Create the VisualStyleRenderer if (vsr == null) { vsr = new VisualStyleRenderer(vse); } else { vsr.SetParameters(vse); } // Draw some pretty graphics TextY += 25; vsr.DrawBackground(g, new Rectangle(250, TextY, 25, 25)); vsr.DrawBackground(g, new Rectangle(350, TextY, 50, 50)); vsr.DrawBackground(g, new Rectangle(450, TextY, 50, 50), new Rectangle(450, TextY, 25, 25)); TextY += 75; // Test some other methods DrawText(g, "GetBackgroundContentRectangle: " + vsr.GetBackgroundContentRectangle(g, new Rectangle(300, 0, 300, 50)).ToString()); DrawText(g, "GetBackgroundExtent: " + vsr.GetBackgroundExtent(g, new Rectangle(300, 0, 300, 50)).ToString()); DrawText(g, "GetBoolean: " + vsr.GetBoolean(BooleanProperty.MirrorImage).ToString()); DrawText(g, "GetEnumValue: " + vsr.GetEnumValue(EnumProperty.VerticalAlignment).ToString()); DrawText(g, "GetFilename: " + vsr.GetFilename(FilenameProperty.ImageFile).ToString()); DrawText(g, "GetInteger: " + vsr.GetInteger(IntegerProperty.BorderSize).ToString()); DrawText(g, "GetMargins: " + vsr.GetMargins(g, MarginProperty.CaptionMargins).ToString()); DrawText(g, "GetPartSize: " + vsr.GetPartSize(g, ThemeSizeType.Draw).ToString()); DrawText(g, "GetPoint: " + vsr.GetPoint(PointProperty.MinSize).ToString()); DrawText(g, "GetString: " + vsr.GetString(StringProperty.Text).ToString()); DrawText(g, "GetTextExtent: " + vsr.GetTextExtent(g, "HeyThere!", TextFormatFlags.Default).ToString()); DrawText(g, "GetTextMetrics: " + vsr.GetTextMetrics(g).Ascent.ToString()); DrawText(g, "GetBackgroundRegion: " + vsr.GetBackgroundRegion(g, this.ClientRectangle).GetBounds(g).ToString()); DrawText(g, "HitTestBackground: " + vsr.HitTestBackground(g, this.ClientRectangle, new Point(300, 300), HitTestOptions.Caption).ToString()); DrawText(g, "Author: " + VisualStyleInformation.Author); DrawText(g, "ColorScheme: " + VisualStyleInformation.ColorScheme); DrawText(g, "Company: " + VisualStyleInformation.Company); DrawText(g, "ControlHighlightHot: " + VisualStyleInformation.ControlHighlightHot.ToString()); DrawText(g, "Copyright: " + VisualStyleInformation.Copyright); DrawText(g, "Description: " + VisualStyleInformation.Description); DrawText(g, "DisplayName: " + VisualStyleInformation.DisplayName); DrawText(g, "MinimumColorDepth: " + VisualStyleInformation.MinimumColorDepth.ToString()); DrawText(g, "Size: " + VisualStyleInformation.Size.ToString()); DrawText(g, "SupportsFlatMenus: " + VisualStyleInformation.SupportsFlatMenus.ToString()); DrawText(g, "TextControlBorder: " + VisualStyleInformation.TextControlBorder.ToString()); DrawText(g, "Url: " + VisualStyleInformation.Url); DrawText(g, "Version: " + VisualStyleInformation.Version.ToString()); } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); } } }