c# capture = true

本文探讨了一个关于Windows Forms应用程序中控件鼠标输入捕获的问题。当控件捕获鼠标后,会导致MouseDown和MouseUp事件无法响应。通过示例代码展示了如何复现此问题,并提供了关闭鼠标捕获的方法来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这东西邪门的很~ 开了之后无法相应 MouseDown 和 MouseUp了

msdn: 当控件已捕获鼠标时,它接收鼠标输入,不论光标是否在其边框内。 通常只有在执行拖动操作时才捕获鼠标。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Point DownPoint = Point.Empty;
        private Pen MyPen;
        private Rectangle srcRect;
        private bool f = false;

        public Form1()
        {
            InitializeComponent();

        }
  
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (srcRect != Rectangle.Empty)
            {
                MyPen = new Pen(Color.Black, 1);
                e.Graphics.DrawRectangle(MyPen, srcRect);
            }
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            
            if (srcRect != Rectangle.Empty)
            {
                e.Graphics.DrawImage(this.pictureBox1.Image, new Rectangle(0, 0, srcRect.Width, srcRect.Height), srcRect, GraphicsUnit.Pixel);
            }
  
        }
        
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            if (f)
            {
                int w = Math.Abs(e.X - DownPoint.X);
                int h = Math.Abs(e.Y - DownPoint.Y);
                if (e.X > DownPoint.X)
                    srcRect = new Rectangle(DownPoint.X, DownPoint.Y, w, h);
                else
                    srcRect = new Rectangle(e.X, e.Y, w, h);
                
                this.pictureBox1.Invalidate();
                this.pictureBox1.Update();
                this.pictureBox2.Invalidate();
                this.pictureBox2.Update();
            }
           
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Capture = true;   //注释掉后一切正常

                if (DownPoint == Point.Empty)
                {
                    f = true;
                    DownPoint.X = e.X;
                    DownPoint.Y = e.Y;
                    
                 }
              }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (DownPoint != Point.Empty)
            {
                DownPoint = Point.Empty;
                srcRect = Rectangle.Empty;
                
                this.pictureBox1.Invalidate();
                this.pictureBox1.Update();
                 
            }
            MessageBox.Show("up");
            f = false;
        }
        
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值