简体   繁体   English

C#鼠标事件序列

[英]c# mouse event sequence

Question... why does this code only produce a message box "Down"? 问题...为什么此代码仅产生一个消息框“ Down”? I'm not getting the Up. 我没有起来。 If I block down code, the up works. 如果我阻止代码,向上工作。

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

namespace Mouse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }   
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
           MessageBox.Show("Up");                     
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
           MessageBox.Show("Down");
        }     
    }
}

Because you are showing a MessageBox 因为您正在显示一个MessageBox

Whenever a mouse down event happened a MessageBox will popup. 每当发生鼠标按下事件时,都会弹出一个MessageBox The MessageBox Will be in foreground and the up event will be on the MessageBox instead of the form. MessageBox将位于前台,而up事件将位于MessageBox而不是窗体上。 Thus the up event in the form doesn't fire 因此,表单中的up事件不会触发

Just do a Console.WriteLine instead of MessageBox and it should work as expected 只需做一个Console.WriteLine而不是MessageBox ,它应该可以按预期工作

They will both fire if you remove the message box call. 如果您删除消息框调用,它们都会触发。 this is interferring with the mouse events because your loosing focus on the form. 这会干扰鼠标事件,因为您将注意力集中在表单上。 Instead of using message box try using... 而不是使用消息框,请尝试使用...

  Console.WriteLine("MouseUp"); 

This will display in the ouput window and not interfere with the events 这将显示在输出窗口中,并且不会干扰事件

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

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