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

2 篇文章 0 订阅
2 篇文章 0 订阅

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

下一篇畸形窗体

整个分两块:
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下载地址:点击下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值