如何在按住鼠标左键时检测鼠标离开事件?

问题描述 投票:0回答:1

我尝试过这个,但它不起作用。支票从来都不是真的,也永远进不去。

private void pictureBoxImageToCrop_MouseLeave(object sender, EventArgs e)
{
    // Check if the right mouse button is being held down
    if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
    {
        // Perform the desired action when the right mouse button is held down
        isDrawing = false;
        // Additional actions can be added here if needed
    }
    isDrawing = false;
}
c# winforms
1个回答
0
投票
using System;
using System.Drawing;
using System.Windows.Forms;

public class MainForm : Form
{
    private bool _isMouseDown = false;

    public MainForm()
    {
        // Others initialize
        
        // Subscribe to mouse events
        this.MouseDown += pictureBoxImageToCrop_MouseDown;
        this.MouseMove += pictureBoxImageToCrop_MouseMove;
        this.MouseUp += pictureBoxImageToCrop_MouseUp;
    }

    private void pictureBoxImageToCrop_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _isMouseDown = true;
        }
    }

    private void pictureBoxImageToCrop_MouseMove(object sender, MouseEventArgs e)
    {
        if (_isMouseDown)
        {
            // Check if the mouse pointer is outside the form's bounds
            if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
            {
                // Mouse leave event
                Console.WriteLine("Mouse left while left button down.");                    
            }
        }
    }

    private void pictureBoxImageToCrop_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _isMouseDown = false;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.