protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) { // 这里调用父类,主要用来处理透明色(圆角之后的问题) base.OnPaintBackground(pevent); if (this.RadiusMode != RadiusMode.None) { RadiusDrawable.DrawRadius(pevent.Graphics, this.ClientRectangle, RadiusMode, this.Radius, BackColor, BackColorGradint, this.LinearGradientMode, this.BorderColor, this.BorderWidth); } else { System.Drawing.Drawing2D.LinearGradientBrush brush; if (this.LinearGradientMode == GradientMode.None) { brush = new System.Drawing.Drawing2D.LinearGradientBrush(this.ClientRectangle, this.BackColor, this.BackColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical); } else { brush = new System.Drawing.Drawing2D.LinearGradientBrush(this.ClientRectangle, this.BackColor, this.BackColorGradint, ((System.Drawing.Drawing2D.LinearGradientMode)LinearGradientMode)); } pevent.Graphics.FillRectangle(brush, pevent.ClipRectangle); brush.Dispose(); } }
/// <summary> /// 绘制一个带有圆角背景颜色的文字块 /// </summary> /// <param name="g"></param> /// <param name="rect"></param> /// <param name="font"></param> /// <param name="text"></param> /// <param name="foreColor"></param> /// <param name="backColor"></param> /// <param name="raidusMode"></param> /// <param name="radius"></param> public static void DrawString(Graphics g, Rectangle rect, Font font, string text, Color foreColor, Color backColor, TextAlignment align, RadiusMode raidusMode, int radius) { // 先绘制圆角 RadiusDrawable.DrawRadius(g, rect, raidusMode, radius, backColor, Color.Empty, GradientMode.None, Color.Empty, 0); DrawString(g, rect, font, text, foreColor, align); }
protected override void OnPaintBackground(PaintEventArgs pevent) { // 这里调用父类,主要用来处理透明色(圆角之后的问题) base.OnPaintBackground(pevent); if (circleButton) { this.Height = this.Width; this.radius = this.Height / 2; this.radiusMode = Forms.RadiusMode.All; } Color c1 = Color.Empty; if (this.status == ButtonStatus.Default) { c1 = this.BorderColor; } else if (this.status == ButtonStatus.Hover) { c1 = this.BorderHoverColor; } else if (this.status == ButtonStatus.Press) { c1 = this.BorderPressColor; } if (this.status == ButtonStatus.Default || this.HoverBackColor == null || this.HoverBackColor.FromColor.IsEmpty) { RadiusDrawable.DrawRadius(pevent.Graphics, this.ClientRectangle, RadiusMode, this.Radius, this.BackgroundColor.FromColor, this.BackgroundColor.ToColor, this.BackgroundColor.GradientMode, c1, this.borderWidth); } else { RadiusDrawable.DrawRadius(pevent.Graphics, this.ClientRectangle, RadiusMode, this.Radius, this.HoverBackColor.FromColor, this.HoverBackColor.ToColor, this.HoverBackColor.GradientMode, c1, this.borderWidth); } }
protected virtual void DrawBackground(Graphics graphics) { var color = this.Hover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, this.Rectangle, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); //using (var brush = new SolidBrush(this.Hover ? this.ControlActivedColor : this.ControlBackColor)) //{ // graphics.FillRectangle(brush, this.Rectangle); //} }
public override void Draw(Graphics graphics) { var fontSize = Size.Ceiling(graphics.MeasureString(this.Text, this.Font)); var x = ParentPalette.Rectangle.X + ParentPalette.Width - fontSize.Width - 2; var y = ParentPalette.Rectangle.Y + 2; this.Rectangle = new Rectangle(x, y, fontSize.Width + 2, fontSize.Height + 2); // 画圆角背景 RadiusDrawable.DrawRadius(graphics, this.Rectangle, RadiusMode.All, (fontSize.Height + 2) / 2, this.BackColor, this.BackColor, GradientMode.None, Color.Empty, 0); using (var brush = new SolidBrush(this.ForeColor)) { x += 1; y += 1; graphics.DrawString(this.Text, this.Font, brush, x, y); } }
protected override void OnPaint(PaintEventArgs pevent) { base.OnPaint(pevent); if (this.RadiusMode != RadiusMode.None) { RadiusDrawable.DrawRadius(pevent.Graphics, captionRect, this.RadiusMode != Forms.RadiusMode.None ? Forms.RadiusMode.TopLeft_TopRight : Forms.RadiusMode.None, this.Radius, CaptionBackColor, CaptionBackColorGradint, this.CaptionLinearGradientMode, Color.Empty, 0); } else { System.Drawing.Drawing2D.LinearGradientBrush brush; if (this.CaptionLinearGradientMode == GradientMode.None) { brush = new System.Drawing.Drawing2D.LinearGradientBrush(this.captionRect, this.CaptionBackColor, this.CaptionBackColorGradint, System.Drawing.Drawing2D.LinearGradientMode.Vertical); } else { brush = new System.Drawing.Drawing2D.LinearGradientBrush(this.captionRect, this.CaptionBackColor, this.CaptionBackColorGradint, ((System.Drawing.Drawing2D.LinearGradientMode)CaptionLinearGradientMode)); } pevent.Graphics.FillRectangle(brush, captionRect); brush.Dispose(); } var font = this.captionFont == null ? this.Font : this.captionFont; if (this.CaptionLocation == CaptionLocation.Top || this.CaptionLocation == CaptionLocation.Bottom) { var fontSize = Size.Ceiling(pevent.Graphics.MeasureString(this.Title, font)); var x = this.Radius > 0 && this.RadiusMode != Forms.RadiusMode.None ? this.Radius / 2 + 6 : 6; x += this.BorderColor.IsEmpty ? 0 : this.BorderWidth; var y = (this.CaptionHeight - fontSize.Height) / 2; if (this.TitleLocation == CaptionTitleLocation.Center) { x = (this.Width - fontSize.Width) / 2; } else if (this.TitleLocation == CaptionTitleLocation.Right) { x = this.Width - fontSize.Width - x; } if (this.CaptionLocation == CaptionLocation.Bottom) { y += this.captionRect.Y; } else { y += this.BorderWidth / 2; } using (var brush = new SolidBrush(this.CaptionForeColor)) { pevent.Graphics.DrawString(this.Title, font, brush, x, y); } } else { Size fontSize = Size.Empty; var height = 0;//总高度 var length = this.Title.Length; for (var i = 0; i < length; i++) { fontSize = Size.Ceiling(pevent.Graphics.MeasureString(this.Title.Substring(i, 1), font)); height += fontSize.Height + 3; } height -= 3; var y = this.Radius > 0 && this.RadiusMode != Forms.RadiusMode.None ? this.Radius / 2 + 6 : 6; y += this.BorderColor.IsEmpty ? 0 : this.BorderWidth / 2; var x = (this.CaptionHeight - fontSize.Width) / 2 + this.BorderWidth / 2; if (this.CaptionLocation == CaptionLocation.Right) { x += this.captionRect.X - this.BorderWidth / 2; } if (this.TitleLocation == CaptionTitleLocation.Center) { y = (this.Height - height) / 2; } else if (this.TitleLocation == CaptionTitleLocation.Right) { y = this.Height - height - y; } for (var i = 0; i < length; i++) { using (var brush = new SolidBrush(this.CaptionForeColor)) { fontSize = Size.Ceiling(pevent.Graphics.MeasureString(this.Title.Substring(i, 1), font)); //height += fontSize.Height + font.Height; pevent.Graphics.DrawString(this.Title.Substring(i, 1), font, brush, x, y); y += 3 + fontSize.Height; } } } }
internal void DrawElement(MHtmlElement element, Graphics graphics) { var rect = new Rectangle(element.RelativePosition, element.Rectangle.Size); if (rect.IsEmpty) { return; } #region 边框和背景 // 圆角模式,忽略边框样式 if (element.RadiusMode != RadiusMode.None && element.Radius > 0) { var color1 = element.BackColor.IsEmpty ? Color.Transparent : element.BackColor; var color2 = element.BackColorGradient; RadiusDrawable.DrawRadius(graphics, rect, element.RadiusMode, element.Radius, element.BackColor, element.BackColorGradient, element.Gradient, element.Border.Color, element.Border.Width); } // 有边框 else if (element.Border != null && !element.Border.IsEmpty) { if (!element.BackColor.IsEmpty) { using (var brush = new SolidBrush(element.BackColor)) { graphics.FillRectangle(brush, element.Rectangle); } } var bl = element.Border.Left == 0 ? element.Border.Width : element.Border.Left; var bt = element.Border.Top == 0 ? element.Border.Width : element.Border.Top; var br = element.Border.Right == 0 ? element.Border.Width : element.Border.Right; var bb = element.Border.Bottom == 0 ? element.Border.Width : element.Border.Bottom; using (var pen = new Pen(element.Border.Color)) { pen.DashStyle = element.Border.GetDashStyle(); if (bl > 0) { pen.Width = bl; graphics.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Y + rect.Height); } if (bt > 0) { pen.Width = bt; graphics.DrawLine(pen, rect.X, rect.Y, rect.X + rect.Width, rect.Y); } if (br > 0) { pen.Width = br; graphics.DrawLine(pen, rect.X + rect.Width, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); } if (bb > 0) { pen.Width = bb; graphics.DrawLine(pen, rect.X, rect.Y + rect.Height, rect.X + rect.Width, rect.Y + rect.Height); } } } else { // 没有边框,直接处理背景 if (!element.BackColor.IsEmpty) { using (var brush = new SolidBrush(element.BackColor)) { graphics.FillRectangle(brush, rect); } } } #endregion if (!string.IsNullOrEmpty(element.Text)) { var textRect = new Rectangle(rect.X + element.Padding.Left, rect.Y + element.Padding.Top, rect.Width - element.Padding.Left - element.Padding.Right, rect.Height - element.Padding.Top - element.Padding.Bottom); GDIHelper.DrawString(graphics, textRect, element.Font, element.Text, element.ForeColor, element.Align); } if (element.HasHover) { Hovers.Add(element); } if (element.HasClick) { Clicks.Add(element); } if (element.HasLeave) { Leaves.Add(element); } if (element.Items != null) { foreach (var item in element.Items) { this.DrawElement(item, graphics); } } }
private void DrawControlBox(Graphics graphics) { var count = 0; #region 钮数量 if (this.ControlBox) { count = 1; if (this.MaximizeBox) { count++; } if (this.MinimizeBox) { count++; } if (this.HelpButton) { count++; } } else { return; } #endregion if (this.ControlBoxies != null) { count += this.controlBoxies.Count; } if (count == 0) { return; } var x = this.Width - this.Padding.Right - this.controlBoxSize.Width; var y = this.Padding.Top;//(this.Height - this.Padding.Top - this.Padding.Bottom - this.controlBoxSize.Height) / 2 + this.Padding.Top; if (this.centerControlBox) { y = (this.Height - y - this.ControlBoxSize.Height) / 2; } ImageAttributes ImgAtt = new ImageAttributes(); ImgAtt.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Clamp); if (this.CloseButtonImage != null) { CloseRect = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); var color = CloseHover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, CloseRect, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); graphics.DrawImage(this.CloseButtonImage, CloseRect, 0, 0, this.CloseButtonImage.Width, this.CloseButtonImage.Height, GraphicsUnit.Pixel, ImgAtt); x -= this.ControlBoxSize.Width; } if (this.MaximizeBox && this.WindowState == FormWindowState.Maximized && this.NormalBoxImage != null) { MaxRect = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); var color = MaxHover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, MaxRect, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); graphics.DrawImage(this.NormalBoxImage, MaxRect, 0, 0, this.NormalBoxImage.Width, this.NormalBoxImage.Height, GraphicsUnit.Pixel, ImgAtt); x -= this.ControlBoxSize.Width; } else if (this.MaximizeBox && this.WindowState != FormWindowState.Maximized && this.MaximizeBoxImage != null) { MaxRect = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); var color = MaxHover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, MaxRect, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); graphics.DrawImage(this.MaximizeBoxImage, MaxRect, 0, 0, this.MaximizeBoxImage.Width, this.MaximizeBoxImage.Height, GraphicsUnit.Pixel, ImgAtt); x -= this.ControlBoxSize.Width; } if (this.MinimizeBox && this.MinimizeBoxImage != null) { MinRect = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); var color = MinHover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, MinRect, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); graphics.DrawImage(this.MinimizeBoxImage, MinRect, 0, 0, this.MinimizeBoxImage.Width, this.MinimizeBoxImage.Height, GraphicsUnit.Pixel, ImgAtt); x -= this.ControlBoxSize.Width; } #region 帮助按钮 if (this.HelpButton && this.HelpButtonImage != null) { HelpRect = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); var color = HelpHover ? this.ControlActivedColor : this.ControlBackColor; RadiusDrawable.DrawRadius(graphics, HelpRect, RadiusMode.None, 0, color.FromColor, color.ToColor, color.GradientMode, Color.Empty, 0); graphics.DrawImage(this.HelpButtonImage, HelpRect, 0, 0, this.HelpButtonImage.Width, this.HelpButtonImage.Height, GraphicsUnit.Pixel, ImgAtt); x -= this.ControlBoxSize.Width; } #endregion #region 其他按钮 if (this.ControlBoxies != null && this.ImageList != null && this.ImageList.Images.Count > 0) { foreach (var item in this.ControlBoxies) { item.Click -= ControlBoxItem_Click; item.Click += ControlBoxItem_Click; item.ControlActivedColor = this.ControlActivedColor; item.ControlBackColor = this.ControlBackColor; item.Rectangle = new Rectangle(x, y, this.controlBoxSize.Width, this.controlBoxSize.Height); item.Image = this.ImageList.Images[item.ImageIndex]; item.Draw(graphics); x -= this.ControlBoxSize.Width; } } #endregion }