public override System.Drawing.Size GetPreferredSize(System.Drawing.Size proposedSize) { System.Drawing.Size height; base.GetPreferredSize(proposedSize); using (Graphics graphic = base.CreateGraphics()) { string str = (this.Text.Length > 0 ? this.Text : "MeasureText"); proposedSize = new System.Drawing.Size(2147483647, 2147483647); height = TextRenderer.MeasureText(graphic, str, MetroFonts.DateTime(this.metroDateTimeSize, this.metroDateTimeWeight), proposedSize, TextFormatFlags.VerticalCenter | TextFormatFlags.LeftAndRightPadding); height.Height = height.Height + 10; } return(height); }
public override Size GetPreferredSize(Size proposedSize) { Size preferredSize; base.GetPreferredSize(proposedSize); using (var g = CreateGraphics()) { string measureText = Text.Length > 0 ? Text : "MeasureText"; proposedSize = new Size(int.MaxValue, int.MaxValue); preferredSize = TextRenderer.MeasureText(g, measureText, MetroFonts.DateTime(metroDateTimeSize, metroDateTimeWeight), proposedSize, TextFormatFlags.Left | TextFormatFlags.LeftAndRightPadding | TextFormatFlags.VerticalCenter); preferredSize.Height += 10; } return(preferredSize); }
protected virtual void OnPaintForeground(PaintEventArgs e) { this.MinimumSize = new Size(0, GetPreferredSize(Size.Empty).Height); Color borderColor, foreColor; if (isHovered && !isPressed && Enabled) { foreColor = MetroPaint.ForeColor.ComboBox.Hover(Theme); borderColor = MetroPaint.GetStyleColor(Style); } else if (isHovered && isPressed && Enabled) { foreColor = MetroPaint.ForeColor.ComboBox.Press(Theme); borderColor = MetroPaint.GetStyleColor(Style); } else if (!Enabled) { foreColor = MetroPaint.ForeColor.ComboBox.Disabled(Theme); borderColor = MetroPaint.BorderColor.ComboBox.Disabled(Theme); } else { foreColor = MetroPaint.ForeColor.ComboBox.Normal(Theme); borderColor = MetroPaint.BorderColor.ComboBox.Normal(Theme); } using (Pen p = new Pen(borderColor)) { int cornerRadius = 3; p.Width = 1; //边框工作区 Rectangle rect = this.ClientRectangle; GraphicsPath Rect = new GraphicsPath(); // 添加圆弧 Rect.AddArc(0, 0, cornerRadius * 2, cornerRadius * 2, 180, 90); Rect.AddArc(rect.Width - cornerRadius * 2 - 1, 0, cornerRadius * 2, cornerRadius * 2, 270, 90); Rect.AddArc(rect.Width - cornerRadius * 2 - 1, rect.Height - cornerRadius * 2 - 1, cornerRadius * 2, cornerRadius * 2, 0, 90); Rect.AddArc(0, rect.Height - cornerRadius * 2 - 1, cornerRadius * 2, cornerRadius * 2, 90, 90); Rect.CloseFigure(); e.Graphics.DrawPath(p, Rect); } using (SolidBrush b = new SolidBrush(foreColor)) { e.Graphics.FillPolygon(b, new Point[] { new Point(Width - 20, (Height / 2) - 2), new Point(Width - 9, (Height / 2) - 2), new Point(Width - 15, (Height / 2) + 4) }); //e.Graphics.FillPolygon(b, new Point[] { new Point(Width - 15, (Height / 2) - 5), new Point(Width - 21, (Height / 2) + 2), new Point(Width - 9, (Height / 2) + 2) }); } int _check = 0; if (this.ShowCheckBox) { _check = 15; using (Pen p = new Pen(borderColor)) { Rectangle boxRect = new Rectangle(3, Height / 2 - 6, 12, 12); e.Graphics.DrawRectangle(p, boxRect); } if (Checked) { Color fillColor = MetroPaint.GetStyleColor(Style); using (SolidBrush b = new SolidBrush(fillColor)) { Rectangle boxRect = new Rectangle(5, Height / 2 - 4, 9, 9); e.Graphics.FillRectangle(b, boxRect); } } else { foreColor = MetroPaint.ForeColor.ComboBox.Disabled(Theme); } } Rectangle textRect = new Rectangle(2 + _check, 2, Width - 20, Height - 4); TextRenderer.DrawText(e.Graphics, Text, MetroFonts.DateTime(metroDateTimeSize, metroDateTimeWeight), textRect, foreColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter); OnCustomPaintForeground(new MetroPaintEventArgs(Color.Empty, foreColor, e.Graphics)); if (displayFocusRectangle && isFocused) { ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle); } }
protected virtual void OnPaintForeground(PaintEventArgs e) { Color styleColor; Color color; System.Drawing.Size preferredSize = this.GetPreferredSize(System.Drawing.Size.Empty); this.MinimumSize = new System.Drawing.Size(0, preferredSize.Height); if (this.isHovered && !this.isPressed && base.Enabled) { color = MetroPaint.ForeColor.ComboBox.Hover(this.Theme); styleColor = MetroPaint.GetStyleColor(this.Style); } else if (this.isHovered && this.isPressed && base.Enabled) { color = MetroPaint.ForeColor.ComboBox.Press(this.Theme); styleColor = MetroPaint.GetStyleColor(this.Style); } else if (base.Enabled) { color = MetroPaint.ForeColor.ComboBox.Normal(this.Theme); styleColor = MetroPaint.BorderColor.ComboBox.Normal(this.Theme); } else { color = MetroPaint.ForeColor.ComboBox.Disabled(this.Theme); styleColor = MetroPaint.BorderColor.ComboBox.Disabled(this.Theme); } using (Pen pen = new Pen(styleColor)) { Rectangle rectangle = new Rectangle(0, 0, base.Width - 1, base.Height - 1); e.Graphics.DrawRectangle(pen, rectangle); } using (SolidBrush solidBrush = new SolidBrush(color)) { Graphics graphics = e.Graphics; Point[] point = new Point[] { new Point(base.Width - 20, base.Height / 2 - 2), new Point(base.Width - 9, base.Height / 2 - 2), new Point(base.Width - 15, base.Height / 2 + 4) }; graphics.FillPolygon(solidBrush, point); } int num = 0; if (base.ShowCheckBox) { num = 15; using (Pen pen1 = new Pen(styleColor)) { Rectangle rectangle1 = new Rectangle(3, base.Height / 2 - 6, 12, 12); e.Graphics.DrawRectangle(pen1, rectangle1); } if (!base.Checked) { color = MetroPaint.ForeColor.ComboBox.Disabled(this.Theme); } else { using (SolidBrush solidBrush1 = new SolidBrush(MetroPaint.GetStyleColor(this.Style))) { Rectangle rectangle2 = new Rectangle(5, base.Height / 2 - 4, 9, 9); e.Graphics.FillRectangle(solidBrush1, rectangle2); } } } Rectangle rectangle3 = new Rectangle(2 + num, 2, base.Width - 20, base.Height - 4); TextRenderer.DrawText(e.Graphics, this.Text, MetroFonts.DateTime(this.metroDateTimeSize, this.metroDateTimeWeight), rectangle3, color, TextFormatFlags.VerticalCenter); this.OnCustomPaintForeground(new MetroPaintEventArgs(Color.Empty, color, e.Graphics)); if (this.displayFocusRectangle && this.isFocused) { ControlPaint.DrawFocusRectangle(e.Graphics, base.ClientRectangle); } }