欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Winform:GDI+篇幅1之-- 畸形自定义控件

程序员文章站 2022-06-07 13:17:04
...

效果:黄色背景区域验证畸形控件可见区域可操作下方控件区域,类似web的悬浮效果

Winform:GDI+篇幅1之-- 畸形自定义控件

下一篇畸形窗体

整个分两块:
1.单独的进度条自定义控件
2.上边超出悬浮的标记框

进度条自定义控件主要代码:

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //1.X坐标 即进度条的起始坐标
            //2.值区域大小 
            Graphics g = e.Graphics;
            g.SetGDIHigh();

            Brush sb = new SolidBrush(m_valueBGColor);
            g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 2));
            GraphicsPath path1 = CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + 1, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 4), 2);
            g.DrawPath(new Pen(m_borderColor, 1), path1);
            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(0, base.ClientRectangle.Height - 3), m_valueColor, Color.FromArgb(250, m_valueColor.R, m_valueColor.G, m_valueColor.B));
            g.FillPath(lgb, CreateRoundedRectanglePath(new Rectangle(StartIndex, (base.ClientRectangle.Height - (base.ClientRectangle.Height - 3)) / 2, (base.ClientRectangle.Width - 3) * CurrentWidth / m_maxValue, base.ClientRectangle.Height - 4), 2));
        }
		
		 public GraphicsPath CreateRoundedRectanglePath(RectangleF 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;
        }

点击进度条的事件代码:

protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var data = ((double)m_maxValue / (double)this.Width) * (double)e.X;
                OnMouseLeftClick?.Invoke(data);
            }
            base.OnMouseDown(e);
        }

上边超出悬浮的标记框自定义控件主要代码: 

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (dic_Remark.Count() == 0)
                return;
            e.Graphics.SetGDIHigh();
            GraphicsPath path = new GraphicsPath();
            foreach (var item in dic_Remark)
            {
                //    float fltIndex = (float)item.Key / (float)this.gaProcessLine1.MaxValue;
                //    int x = (int)(fltIndex * this.gaProcessLine1.Width + this.gaProcessLine1.Location.X - 15) - 2;
                int semple = 15;
                if (item.Key.Width / 30 == 1.2)
                    semple = 18;

                RectangleF rect = item.Key;
                int cornerRadius = 2;
                path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);//左上角
                path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);//上
                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);//右上角
                path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);//右
                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);//右下角
                path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//下

                path.AddLine(rect.Right - cornerRadius * 2 - 5, rect.Bottom, rect.X + semple, gaProcessLine1.Location.Y);
                path.AddLine(rect.X + semple, gaProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, rect.Bottom);

                path.AddLine(rect.X + cornerRadius * 2 + 5, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);//下
                path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);//左下角
                path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//左
                path.CloseFigure();

            }
            //进度条背景
            Rectangle rectangle = new Rectangle(gaProcessLine1.Location.X, gaProcessLine1.Location.Y + 1, gaProcessLine1.Width - 3, gaProcessLine1.Height - 4);
            Region region0 = new Region(rectangle);
            Region region1 = new Region(path);
            region0.Union(region1);

            this.Region = region0;
            using (SolidBrush b = new SolidBrush(Color.FromArgb(218, 218, 218)))
            {
                e.Graphics.FillPath(b, path);
                foreach (var item in dic_Remark)
                {
                    RectangleF rect = item.Key;
                    string strValue = item.Value;
                    System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
                    e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.FromArgb(229, 150, 88)), new PointF(rect.X + (rect.Width - sizeF.Width) / 2 + 1, (rect.Height - sizeF.Height) / 2 + 1));
                }
            }

        }

根据坐标计算鼠标点击标记事件

/// <summary>
        /// 鼠标点击 判断是否点击标记,并计算标记的key
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (e.Button == MouseButtons.Left)
            {
                var mDic = dic_Remark.DeepCloneModel<Dictionary<RectangleF, string>>();
                dic_Remark.Clear();
                foreach (var item in mDic)
                {
                    if (item.Key.Contains(e.Location))
                    {
                        RectangleF rectNew = new RectangleF(item.Key.X, 1, (float)(30 * 1.2), (float)(20 * 1.2));
                        dic_Remark[rectNew] = item.Value;
                    }
                    else
                    {
                        RectangleF rectNew = new RectangleF(item.Key.X, 1, 30, 20);
                        dic_Remark[rectNew] = item.Value;
                    }
                }
                Refresh();
            }
        }

关键代码是 Region 函数 
MSDN上解释:https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.region?view=net-5.0

demo下载地址:点击下载