为什么鼠标事件不起作用? [英] why Mouse Events doesn't work?

查看:62
本文介绍了为什么鼠标事件不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么鼠标事件在此应用程序中不起作用!!!
当鼠标按下事件在图片框上处于骨骼状态时,则不做任何反应

why Mouse Events doesn''t work in this application!!!
when the mouse down event is bone on the picturebox , no reaction is done

using System.Drawing;
using System.Windows.Forms;


namespace test
{
    public partial class Form1 : Form
    {

        bool _selecting;
        Rectangle _selection;
        //---------------------------------------------------------------------
        public Form1()
        {
            InitializeComponent();
        }
        //---------------------------------------------------------------------
        private void Form1_Load(object sender, System.EventArgs e)
        {

        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseDown(object sender,MouseEventArgs e)
        {
            // Starting point of the selection:
            if (e.Button == MouseButtons.Left)
            {
                _selecting = true;
                _selection = new Rectangle(new Point(e.X, e.Y), new Size());
               
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            // Update the actual size of the selection:
            if (_selecting)
            {
                _selection.Width = e.X - _selection.X;
                _selection.Height = e.Y - _selection.Y;

                // Redraw the picturebox:
                pictureBox1.Refresh();
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && _selecting)
            {


                _selecting = false;
            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_selecting)
            {
                // Draw a rectangle displaying the current selection
                Pen pen = Pens.GreenYellow;
                e.Graphics.DrawRectangle(pen, _selection);
            }
        }
        //---------------------------------------------------------------------

    }
}

推荐答案

您确定已连接事件.您的For1.designer.cs中是否包含以下几行:
Are you sure you have wired the events. Do you have in your For1.designer.cs the following lines:
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);


除了Mika指出的问题:

您的问题不再只是表明您未使用调试器这一事实的征兆.如果您想认真地开发任何东西,则需要具备在遇到问题时使用调试器的基本技能,并且在提出类似问题之前必须至少使用该调试器.另外,您了解pictureBox1_MouseUp之类的方法不是事件吗?只有名称表明该方法的目的与将它们用作事件处理程序有关.此外,此类名称违反了(好的)Microsoft命名约定.是的,它们是由Microsoft Designer生成的,那又如何呢?谁告诉你应该保留他们?您需要将所有内容重命名为一些语义名称.更好的是,完全避免使用名称.您需要的只是匿名方法:

In addition to the problem pointed out by Mika:

Your question is no more that the indication of the fact you are not using debugger, it looks like. If you want to develop anything seriously, you need to have elementary skills of using the debugger whenever you have a problem, and at lease before asking a question like that. Also, do you understand that methods like pictureBox1_MouseUp are not events? Only the name suggests that the purpose of the method was related to using them as event handlers. Besides, such names violate (good) Microsoft naming conventions. Yes, they are generated by Microsoft Designer, so what? Who told you you are supposed to keep them? You need to renamed everything to some semantic names. Even better, avoid names at all. All you need is anonymous methods:

this.pictureBox1.MouseMove += (sender, eventArgs) => { /* ... */ }

//and this syntax can work even for C# v.2, where lambda was not yet introduced:
this.pictureBox1.MouseUp += delegate(object sender, MouseEventArgs eventArgs)  { /* ... */ }


现在,更重要的问题:

将类PictureBox用于您要实现的任何交互行为的整个想法是完全没有意义的.即使这是可能的,PictureBox功能也不会添加任何有用的东西,而只会造成不必要的麻烦并占用资源和额外的开发时间.相反,您需要使用基于System.Windows.Forms.Control的自定义控件.

我将解释这一点以及在过去的解决方案中该怎么做:


Now, more important problem:

The whole idea of using the class PictureBox for any interactive behavior you are trying to achieve is completely pointless. Even though this is possible, the PictureBox functionality does not add anything useful, but only creates unwanted hassles and eats up resources and extra development time. Instead, you need to use a custom control based on System.Windows.Forms.Control.

I explain this and what to do in my past solution: How do I clear a panel from old drawing[^].

—SA


这篇关于为什么鼠标事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆