【问题标题】:Add event mouseDown and mouseMove to my own class将事件 mouseDown 和 mouseMove 添加到我自己的类
【发布时间】:2017-07-23 07:47:36
【问题描述】:

我有问题。

我需要在我自己的类 (Unidad.cs) 中添加一个 MouseDown 事件,但我不知道该怎么做。我有一个从pictureBox 扩展的类,我需要一个MouseDown 和MouseMove 事件来移动pictureBox,我该怎么做?而且...它总是准确的pictureBox?我的意思是,我将有 20 个(例如)“Unidad”对象,我只想移动用鼠标单击的 Unidad 对象。

我的班级:

public class Unidad : PictureBox
{
    //Constructor
    public Unidad(string nombre, string tipo, int movimiento, int ha, int hp, int fuerza, int resistencia, int heridas, int iniciativa, int ataques, int liderazgo, int coste, string rutaImagen)
    {
        tipoUnidad = tipo;
        movimientoUnidad = movimiento;
        nombreUnidad = nombre;
        costeUnidad = coste;
        haUnidad = ha;
        hpUnidad = hp;
        fuerzaUnidad = fuerza;
        resistenciaUnidad = resistencia;
        iniciativaUnidad = iniciativa;
        ataquesUnidad = ataques;
        liderazgoUnidad = liderazgo;

        object O = Resources.ResourceManager.GetObject(rutaImagen); 
        dibujoUnidad = new PictureBox();
        dibujoUnidad.SizeMode = PictureBoxSizeMode.StretchImage;
        dibujoUnidad.ClientSize = new Size(145, 67);
        dibujoUnidad.Image = (Image)O;
    }

    //Propiedades
    public string nombreUnidad { get; set; }
    public string tipoUnidad { get; set; }
    public int movimientoUnidad { get; set; }
    public int costeUnidad { get; set; }
    public int haUnidad { get; set; }
    public int hpUnidad { get; set; }
    public int fuerzaUnidad { get; set; }
    public int resistenciaUnidad { get; set; }
    public int heridasUnidad { get; set; }
    public int iniciativaUnidad { get; set; }
    public int ataquesUnidad { get; set; }
    public int liderazgoUnidad { get; set; }
    public PictureBox dibujoUnidad { get; set; }

    //Método para dibujar unidad
    public void Colocar(Point p, Control control, Image imagen)
    {
        using (Graphics g = control.CreateGraphics())
        {
            using (Pen pen = new Pen(Color.Red, 2))
            {
                g.DrawImage(imagen, p);
            }
        }
    }
}

谢谢!

编辑 1:

我可以在我的“Unidad.cs”结尾处这样做:

public void Colocar(Control control, Unidad unidad, Point p)//(Point p, Control control, Image imagen)
    {
                control.Controls.Add(unidad);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        //Do Stuff here
        base.OnMouseDown(e);
        MessageBox.Show("HOLA");
    }

我现在的问题是我没有图像,如果我不使用 BackColor 命令,我看到一个黑色矩形或灰色矩形,当我单击它时,它会显示一个 MessageBox ,但我无法放置正确的图像或正确的位置。谁能帮帮我?

【问题讨论】:

  • 你搞定了吗?

标签: c# winforms events picturebox mousemove


【解决方案1】:

首先,您需要创建一个作为事件处理程序的方法。应该是这样的:

dibujoUnidad.MouseDown.MouseDown += new MouseEventHandler(mouseDownEvent_method);

希望对你有帮助!

【讨论】:

    【解决方案2】:

    试试这个:

    public class Unidad {
        public event EventHandler MouseDown;
    
        public void someMethode()
        {
            // Raise the event
            OnMouseDown(EventArgs.Empty);
        }
    
        protected void OnMouseDown(EventArgs e)
        {
            if (MouseDown != null)
                MouseDown(this, e);
        }
    }
    

    你必须对 MouseUp 做同样的事情

    您也可以使用自己的 EventArgs 对象。 这样做,使用这条线: public event EventHandler<MyEventArgs> MouseDown

    确保您的 MyEventArgs 类派生自 EventArgs。

    【讨论】:

    • 我不明白它是如何工作的。我的意思是,我必须调用 someMethode()?什么时候?
    【解决方案3】:

    您从 Picturebox 继承,因此您可以覆盖鼠标事件

     public class Unidad : PictureBox
        {
            protected override void OnMouseDown(MouseEventArgs e)
            {
                //Do Stuff here
                base.OnMouseDown(e);
            }
        }
    

    我在表单中添加了一个面板,名为“picContainer”的“Form1”

    public Form1()
    {
          InitializeComponent();  
    
          picContainer.Controls.Add(new MyPictureClass()
          {
                Image = imageList1.Images[0],
          }
         );
    }
    

    我从图像容器中设置图像。

    【讨论】:

    • 你说得对,我没有看到 PictureBox 的推导。
    • 我尝试了,但它不起作用。我把你的代码复制到我的课堂上,然后放了 MessageBox.Show("OK");在你 base.OnMouseDown 但它不显示消息框之后。你能帮我多一点吗?谢谢!
    • 你能看到图片框的边界吗?起初我看不到我的消息框,所以不得不在我的表单上单击,直到我真正单击边界本身。它适用于我。
    • 不,它不显示消息框。我还有一个新问题可能与我们的问题有关。我最小化我的表单,当我最大化时,图片框消失了。
    • 实例化 Unidad 时。尝试将背景颜色设置为黑色,以便您可以看到它。新 Unidad(){BackColor = Color.Black});如果您看不到该框,那么这是您要排序的第一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多