123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace OTSIncAMeasureApp
- {
- public class GraphicsPanel : System.Windows.Forms.Panel
- {
- private Color _borderColor = Color.Black;
- private int _radius = 50;
- private RoundStyle _roundeStyle;
- private const int WM_PAINT = 0xF;
- public GraphicsPanel()
- {
- this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- this.SetStyle(ControlStyles.UserPaint, true);
- }
- /// <summary>
- /// 建立圆角路径的样式。
- /// </summary>
- public enum RoundStyle
- {
- /// <summary>
- /// 四个角都不是圆角。
- /// </summary>
- None = 0,
- /// <summary>
- /// 四个角都为圆角。
- /// </summary>
- All = 1,
- /// <summary>
- /// 左边两个角为圆角。
- /// </summary>
- Left = 2,
- /// <summary>
- /// 右边两个角为圆角。
- /// </summary>
- Right = 3,
- /// <summary>
- /// 上边两个角为圆角。
- /// </summary>
- Top = 4,
- /// <summary>
- /// 下边两个角为圆角。
- /// </summary>
- Bottom = 5,
- }
- /// <summary>
- /// 建立带有圆角样式的路径。
- /// </summary>
- /// <param name="rect">用来建立路径的矩形。</param>
- /// <param name="_radius">圆角的大小。</param>
- /// <param name="style">圆角的样式。</param>
- /// <param name="correction">是否把矩形长宽减 1,以便画出边框。</param>
- /// <returns>建立的路径。</returns>
- GraphicsPath CreatePath(Rectangle rect, int radius, RoundStyle style, bool correction)
- {
- GraphicsPath path = new GraphicsPath();
- int radiusCorrection = correction ? 1 : 0;
- switch (style)
- {
- case RoundStyle.None:
- path.AddRectangle(rect);
- break;
- case RoundStyle.All:
- path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
- path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
- break;
- case RoundStyle.Left:
- path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
- path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
- path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
- break;
- case RoundStyle.Right:
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
- path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
- break;
- case RoundStyle.Top:
- path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
- path.AddLine(rect.Right - radiusCorrection, rect.Bottom - radiusCorrection, rect.X, rect.Bottom - radiusCorrection);
- break;
- case RoundStyle.Bottom:
- path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
- path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
- path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
- break;
- }
- path.CloseFigure(); //这句很关键,缺少会没有左边线。
- return path;
- }
- [DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]
- public Color BorderColor
- {
- get { return _borderColor; }
- set
- {
- _borderColor = value;
- base.Invalidate();
- }
- }
- [DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]
- public int Radius
- {
- get { return _radius; }
- set
- {
- _radius = value;
- base.Invalidate();
- }
- }
- public RoundStyle RoundeStyle
- {
- get { return _roundeStyle; }
- set
- {
- _roundeStyle = value;
- base.Invalidate();
- }
- }
- protected override void WndProc(ref Message m)
- {
- try
- {
- base.WndProc(ref m);
- if (m.Msg == WM_PAINT)
- {
- if (this.Radius > 0)
- {
- using (Graphics g = Graphics.FromHwnd(this.Handle))
- {
- Rectangle r = new Rectangle();
- r.Width = this.Width;
- r.Height = this.Height;
- DrawBorder(g, r, this.RoundeStyle, this.Radius);
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void DrawBorder(Graphics g, Rectangle rect, RoundStyle roundStyle, int radius)
- {
- rect.Width -= 1;
- rect.Height -= 1;
- using (GraphicsPath path = CreatePath(rect, radius, roundStyle, false))
- {
- using (Pen pen = new Pen(this.BorderColor))
- {
- Color myColor = ColorTranslator.FromHtml("#c8c8c8");
- System.Drawing.SolidBrush brushBG = new System.Drawing.SolidBrush(myColor);
- g.FillPath(brushBG, path);
- //消除锯齿
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.DrawPath(pen, path);
- }
- }
- }
- /// <summary>
- /// 绘制外框方法
- /// </summary>
- /// <param name="CenterPonit">中心点</param>
- /// <param name="RectSize">尺寸大小 宽高</param>
- /// <param name="GraphicsType">图形类型 0:矩形 1:椭圆</param>
- /// <param name="IsRound">是否有圆角</param>
- /// <param name="Radius">半径</param>
- public static GraphicsPanel CreateOutLineBorder(Point CenterPonit, Rectangle RectSize,int GraphicsType, bool IsRound, int Radius)
- {
- GraphicsPanel grPanel = new GraphicsPanel();
- //工作区中心位置
- grPanel.Location = RectSize.Location;
- //外框尺寸
- grPanel.Size = RectSize.Size;
- //是否有圆角
- if (IsRound)
- {
- //圆角半径
- grPanel.Radius = Radius;
- //圆角类型
- grPanel.RoundeStyle = RoundStyle.All;
-
- }
- Graphics GrRect = grPanel.CreateGraphics();
- return grPanel;
- }
- /// <summary>
- /// 绘制图形方法
- /// </summary>
- /// <param name="GObject">图形对象</param>
- /// <param name="RectSize">尺寸大小 宽高</param>
- /// <param name="CenterPonit">中心点</param>
- /// <param name="GraphicsType">图形类型 0:矩形 1:椭圆</param>
- /// <param name="IsRound">是否有圆角</param>
- /// <param name="Radius">半径</param>
- public static void CreateGraphics(Graphics GObject, Rectangle RectSize, Point CenterPonit, int GraphicsType, bool IsRound, int Radius)
- {
- //工作区中心位置
- //外框尺寸
- //是否有圆角
- }
- #region 画圆角矩形
- internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
- {
- GraphicsPath roundedRect = new GraphicsPath();
- roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
- roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
- roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
- roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
- roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
- roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
- roundedRect.CloseFigure();
- return roundedRect;
- }
- #endregion
- }
- }
|