/// <summary> /// Retrieves the size of a rectangular area into which a control can be fitted. /// </summary> /// <param name="proposedSize">The custom-sized area for a control.</param> /// <returns> /// An ordered pair of type <see cref="T:System.Drawing.Size"/> representing the width and height of a rectangle. /// </returns> public override Size GetPreferredSize(Size proposedSize) { if (Text.Length <= 0 || rnd == null) { return(base.GetPreferredSize(proposedSize)); } using (var g = CreateGraphics()) { var tff = this.BuildTextFormatFlags(); Rectangle tRect, iRect; ExtensionMethods.CalcImageAndTextBounds(new Rectangle(Point.Empty, proposedSize), Text, Font, Image, TextAlign, ImageAlign, TextImageRelation, !AutoSize, 0, ref tff, out tRect, out iRect); return(System.Drawing.Size.Add(Rectangle.Union(tRect, iRect).Size, Padding.Size)); } }
/// <summary> /// Raises the Paint event. /// </summary> /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); try { // Setup variables var useVs = rnd != null && Application.RenderWithVisualStyles; var r = DeflateRect(ClientRectangle, Padding); var tff = this.BuildTextFormatFlags(); Rectangle tRect, iRect; ExtensionMethods.CalcImageAndTextBounds(r, Text, Font, Image, TextAlign, ImageAlign, TextImageRelation, !AutoSize, 0, ref tff, out tRect, out iRect); // Draw background if (SupportGlass && !this.IsDesignMode() && DesktopWindowManager.IsCompositionEnabled()) { e.Graphics.Clear(Color.Black); } else { if (useVs) { rnd.DrawBackground(e.Graphics, ClientRectangle, e.ClipRectangle); } else { e.Graphics.Clear(BackColor); } } // Draw image if (Image != null) { if (useVs) { rnd.DrawImage(e.Graphics, iRect, Image); } else { e.Graphics.DrawImage(Image, iRect); } } // Draw text if (Text.Length > 0) { var rtl = this.GetRightToLeftProperty(); if (useVs) { if (rtl == RightToLeft.Yes) { tff |= TextFormatFlags.RightToLeft; } if (GlowingText) { rnd.DrawGlowingText(e.Graphics, tRect, Text, Font, ForeColor, tff); } else { rnd.DrawText(e.Graphics, tRect, Text, !Enabled, tff); } } else { var br = DesktopWindowManager.IsCompositionEnabled() ? SystemBrushes.ActiveCaptionText : SystemBrushes.ControlText; var sf = new StringFormat(StringFormat.GenericDefault); if (rtl == RightToLeft.Yes) { sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } e.Graphics.DrawString(Text, Font, br, ClientRectangle, sf); } } } catch { } }