2017-03-03 87 views
-1

我有一個問題。添加事件mouseDown和mouseMove到我自己的類

我需要添加一個MouseDown事件到我自己的類(Unidad.cs),但我不知道該怎麼做。我有一個擴展從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,但我無法放置正確的圖像或正確的位置。任何人都可以幫助我?

+0

您是否得到了這個工作? – Wheels73

回答

0

您是從PictureBox的繼承,因此您可以覆蓋鼠標事件

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

我添加了一個小組到我的形式,「Form1的」叫「picContainer」

public Form1() 
{ 
     InitializeComponent(); 

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

我從設置圖像圖像容器。

+0

你的權利,我沒有看到來自PictureBox的派生。 – Sammy

+0

我嘗試,但它不起作用。我將你的代碼複製到我的類中,然後放入一個MessageBox.Show(「OK」);之後你base.OnMouseDown但它不顯示消息框。你能幫我多一點嗎?謝謝! – Imrik

+0

我把一個斷點,它不輸入​​的方法。 – Imrik

0

首先,您需要創建一個將成爲事件處理程序的方法。它應該是這樣的:

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

希望它有幫助!

+0

然後呢?我需要做什麼?謝謝! :) – Imrik

0

試試這個:

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的派生。

+0

我不明白它是如何工作的。我的意思是,我必須調用someMethode()?什麼時候? – Imrik

相關問題