简体   繁体   English

MouseUp 和 MouseDown 始终具有相同的对象发送者

[英]MouseUp and MouseDown always have the same object sender

I'm trying to make a minesweeper clone and i'm having trouble with the mouse events.我正在尝试制作一个扫雷艇克隆,但我在处理鼠标事件时遇到了问题。 If i press a mousebutton on a picturebox and then move the mouse to another box, the mouseup event will still have the same object sender even though it happens on another control.如果我在图片框上按下鼠标按钮,然后将鼠标移动到另一个框,即使它发生在另一个控件上,mouseup 事件仍将具有相同的对象发送者。

I need the mousedown event so that i can see if both mouse buttons have been pressed, unfortunately the mouseup event doesn't seem to care where the cursor is when i release the button.我需要 mousedown 事件,以便我可以查看是否按下了两个鼠标按钮,不幸的是,mouseup 事件似乎并不关心当我释放按钮时光标的位置。

If i'm not expressing myself clearly, think minesweeper.如果我没有清楚地表达自己,想想扫雷艇。 I want to be able to leftclick, rightclick, click both together and also be able to move the cursor after pressing down and open the tile where i release it.我希望能够左键单击,右键单击,同时单击两者,并且还能够在按下并打开我释放它的图块后移动光标。

Does anyone have any ideas?有没有人有任何想法?

I managed to figure something out that will do the trick, gonna leave it here if someone else needs it in the future.我设法想出了一些可以解决问题的方法,如果将来有人需要它,我会把它留在这里。

private void MouseUp(object sender, MouseEventArgs e)
    {
        Point ptCursor = Cursor.Position;
        ptCursor = PointToClient(ptCursor);
        PictureBox pBox = (PictureBox)GetChildAtPoint(ptCursor);
        pBox.BackColor = System.Drawing.Color.Blue;
    } 

I set up a simple form with some pictureboxes and when the mouseup-event happens it checks for the control under the cursors position and you can then use that for your needs.我设置了一个带有一些图片框的简单表单,当 mouseup 事件发生时,它会检查光标位置下的控件,然后您可以根据需要使用它。 The problem i was having was that the mouseup event was tied to the control where the mousedown-event happened, and thus i couldn't access the control the cursor was in when the mouseup-event happened.我遇到的问题是 mouseup 事件与发生 mousedown 事件的控件相关联,因此我无法访问发生 mouseup 事件时光标所在的控件。

I have only been programming for about 4 weeks so this solution might be very flawed so if there are any problems with it some feedback would be great.我只编程了大约 4 周,所以这个解决方案可能非常有缺陷,所以如果它有任何问题,一些反馈会很好。

I would do like this:我会这样做:

  1. Create subclassed buttons to contain game assets as theire picture.创建子类按钮以包含游戏资产作为他们的图片。
  2. Create a custom control and add as many as buttons necessary创建自定义控件并添加尽可能多的按钮
  3. On mouse click I would check agains Contains method of the Bounds to see if the click occured on that specific button, or another way handle the button click inside the custom controll单击鼠标时,我会再次检查Bounds Contains方法,以查看单击是否发生在该特定按钮上,或者以其他方式处理自定义控件内的按钮单击

It would help more if we see some of your code and propably a screenshot.如果我们看到您的一些代码和可能的屏幕截图,这将更有帮助。

Update:更新:

This is an example of how to handle clicks, using delegates.这是一个如何使用委托处理点击的示例。

public partial class MineSweeper : UserControl
{
    public MineSweeper()
    {
        InitializeComponent();
    }

    private void MineSweeper_Load(object sender, EventArgs e)
    {
        this.BackColor = Color.Red;
        for (int i = 0; i < 16; i++)
        {
            var button = new Button();
            button.Name = "Button" + i;
            button.Size = new Size(50, 50);
            button.ForeColor = Color.Yellow;

            button.ForeColor = Color.Black;
            button.Click += delegate
            {
                MessageBox.Show("You have clicked me! I am " + button.Name);
            };
            flowLayoutPanel1.Controls.Add(button);
            flowLayoutPanel1.Invalidate();
        }

        this.Invalidate();
    }
}

Just add a new UserControl from the designer, add a flowLayoutPanel to it and set its dock property to full.只需从设计器中添加一个新的 UserControl,向其中添加一个 flowLayoutPanel 并将其停靠属性设置为 full。

Personally, I do this by having a form level Point variable that I update on every MouseMove event.就我个人而言,我通过在每个 MouseMove 事件上更新一个表单级别的 Point 变量来做到这一点。 Then, when the other mouse events happen, they have the latest available.然后,当其他鼠标事件发生时,它们有最新可用的。

[VS 2017 c#] Using MouseEventArgs for mouse position at mouse down/up works only when the point is captured inside left mouse button as shown below. [VS 2017 c#] 使用MouseEventArgs在鼠标按下/抬起时的鼠标位置仅在鼠标左键内捕获点时才有效,如下所示。

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
   //??? this changes at UP time and becomes same as MouseArgsUpPoint 
   //MouseArgsDownPoint = new Point(e.X, e.Y);
   //same as above, MouseArgsDownPoint = PointToClient(MousePosition);

   if (e.Button == MouseButtons.Left)
   {
       //inside here it works fine and Up/Down points are different and correct
       MouseArgsDownPoint = new Point(e.X, e.Y);
   }
}


private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    MouseArgsUpPoint = new Point(e.X, e.Y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM