C# Winform在图片上添加自定义螺丝控件,并调整位置

        在Winform项目中,我们经常遇到需要显示特殊形状的控件,但是常用基础控件又不满足要求时,我们通常需要自定义控件,并实现其需要的功能。本实例就是自定义圆形的螺丝控件,并对其进行编号和添加选择事件,并可动态调整其位置,支持鼠标框选多个控件。具体效果如下:

实例代码链接:https://download.csdn.net/download/lvxingzhe3/89436437

Winform鼠标框选控件代码:

  private void Form1_Load(object sender, EventArgs e)
        {

            panel1.MouseDown += new MouseEventHandler(MouseDown);
            panel1.MouseUp += new MouseEventHandler(MouseUp);
            panel1.MouseMove += new MouseEventHandler(MouseMove);

        }
        //定义两个变量 
        bool MouseIsDown = false;
        Rectangle MouseRect = Rectangle.Empty; //矩形(为鼠标画出矩形选区)
        #region mouseMove
        //定义三个方法
        private void ResizeToRectangle(object sender, Point p)
        {
            DrawRectangle(sender);
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle(sender);
        }
        private void DrawRectangle(object sender)
        {
            Rectangle rect = ((Panel)sender).RectangleToScreen(MouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
        }

        private void DrawStart(object sender, Point StartPoint)
        {
            ((Panel)sender).Capture = true;
            Cursor.Clip = ((Panel)sender).RectangleToScreen(((Panel)sender).Bounds);
            MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }

        private void MouseDown(object sender, MouseEventArgs e)
        {
            MouseIsDown = true;
            DrawStart(sender, e.Location);
        }

        private void MouseUp(object sender, MouseEventArgs e)
        {
            this.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            MouseIsDown = false;
            DrawRectangle(sender);
            MouseRect = Rectangle.Empty;
        }

        private void MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseIsDown)
                ResizeToRectangle(sender, e.Location);


            foreach (Control button in ((Panel)sender).Controls)
            {
                if (MouseRect.IntersectsWith(button.Bounds)) //相交( MouseRect.Contains  完全包含)
                {
                    button.BackColor = Color.Blue;
                }

            }

        }

实例代码链接:https://download.csdn.net/download/lvxingzhe3/89436437

参考:

https://www.cnblogs.com/guo-pc31/p/3652263.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值