在C#中是否有一种方法可以触发onclick()事件,并且仍然可以在按住鼠标单击的同时检测onDrag()事件

时间:2017-11-17 14:32:34

标签: c# events mouse drag piano

我尝试了dragOver事件处理程序,但它没有按预期工作。

我正在制作一架钢琴,我希望能够演奏该音符,即使该键上没有发生过mousedown。

这是否有事件处理程序?

下面是我正在制作的钢琴的照片。

Piano

3 个答案:

答案 0 :(得分:1)

您应该能够观看鼠标输入,然后检查鼠标状态以查看它是否已被点击

private void object_MouseEnter(object sender, EventArgs e) 
{
    if (Mouse.LeftButton == MouseButtonState.Pressed)
    {
        'Do Something
    }
}

答案 1 :(得分:1)

是的,您尝试点击并拖动like

private void MyMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
            {
               //Bla bla bla , do whatever you want 
            }
        }

答案 2 :(得分:0)

当MouseUp事件发生时,您可以检查鼠标是否在另一个按钮上。如果是,则执行单击该按钮。我从this问题中的答案中获取了GetCursorPosition代码。

必须将true和false放入按钮标签才能判断ButtonClick是否已经处理过该按钮。

// running as Console window
// output from...
// 1. clicking and releasing on button1
// 2. clicking and holding on button1 and then releasing not over button1 or button2,
// 3. clicking and holding on button1 and then 
// moving mouse to over button2 and releasing mouse.

// button1 Click
// button1 Ding!
// button1 MouseUp
// button1 MouseUp
// button1 Ding!
// button1 MouseUp
// button1 Ding!
// button2 Click
// button2 Ding!

private List<Button> _buttons { get; set; }

public Form1()
{
    InitializeComponent();

    button1.MouseUp += Button_MouseUp;
    button2.MouseUp += Button_MouseUp;

    button1.Click += Button_Click;
    button2.Click += Button_Click;

    button1.Tag = false;
    button2.Tag = false;

    _buttons = new List<Button>()
    {
        button1, button2
    };
}

private void Button_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    var button = sender as Button;
    Console.WriteLine($"{button.Text} MouseUp");
    if (!(bool)button.Tag)
        ButtonClick(button);
    else
        button.Tag = false;

    Func<Button, bool> ContainsMouse = (b) =>
    {
        var rect = b.RectangleToScreen(b.DisplayRectangle);
        var pos = GetCursorPosition();
        return rect.Contains(pos.X, pos.Y);
    };

    var hoverButton = _buttons.SingleOrDefault(b => ContainsMouse(b));
    if (hoverButton != null && !hoverButton.Equals(button))
        hoverButton.PerformClick();
}

private void Button_Click(object sender, EventArgs e)
{
    var button = sender as Button;
    Console.WriteLine($"{button.Text} Click");
    ButtonClick(button);
    button.Tag = true;
}

private void ButtonClick(Button button)
{
    Console.WriteLine($"{button.Text} Ding!");
}

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public static implicit operator Point(POINT point)
    {
        return new Point(point.X, point.Y);
    }
}

/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

public static Point GetCursorPosition()
{
    POINT lpPoint;
    GetCursorPos(out lpPoint);
    //bool success = User32.GetCursorPos(out lpPoint);
    // if (!success)

    return lpPoint;
}