2014-02-07 176 views
0

我有這樣的代碼:如何在鼠標輸入事件中觸發鼠標中鍵?

public void globalPbsMouseEnterEvent(object sender, System.EventArgs e) 
     { 
      if (e.Button == MouseButtons.Middle) 
      { 
       MessageBox.Show("hi"); 
      } 
      PictureBox p = sender as PictureBox; 
      pb.SizeMode = PictureBoxSizeMode.StretchImage; 
      if (file_array != null) 
      { 
       if (file_array.Length > 0) 
       { 
        for (int i = 0; i < file_array.Length; i++) 
        { 
         if (p == pbs[i]) 
          pb.Animate(file_array[i]); 
        } 
       } 
      } 
      pb.Visible = true; 
      pb.BringToFront(); 
      leave = true; 

     } 

但e.Button不存在按鈕沒有物業存在爲電子

這是在Form1的構造函數的代碼,我做的實例,以使全球進入事件。變量PBS是爲AnimatedPictureBoxs的數組:

pbs = new AnimatedPictureBox.AnimatedPictureBoxs[8]; 
      progressbars = new ProgressBar[8]; 
      for (int i = 0; i < pbs.Length; i++) 
      { 
       progressbars[i] = new ProgressBar(); 
       progressbars[i].Size = new Size(100, 10); 
       progressbars[i].Margin = new Padding(0, 0, 0, 70); 
       progressbars[i].Dock = DockStyle.Top; 
       pbs[i] = new AnimatedPictureBox.AnimatedPictureBoxs(); 
       pbs[i].MouseEnter += globalPbsMouseEnterEvent; 
       pbs[i].MouseLeave += globalPbsMouseLeaveEvent; 
       pbs[i].Tag = "PB" + i.ToString(); 
       pbs[i].Size = new Size(100, 100); 
       pbs[i].Margin = new Padding(0, 0, 0, 60); 
       pbs[i].Dock = DockStyle.Top; 
       pbs[i].SizeMode = PictureBoxSizeMode.StretchImage; 
       Panel p = i < 4 ? panel1 : panel2; 
       p.Controls.Add(pbs[i]); 
       p.Controls.Add(progressbars[i]); 
       pbs[i].BringToFront(); 
       progressbars[i].BringToFront(); 
      } 

這是班上AnimatedPictureBox代碼:

using System; 
using System.Windows.Forms; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using DannyGeneral; 

namespace WeatherMaps 
{ 
    class AnimatedPictureBox 
    { 
     //Use this custom PictureBox for convenience 
     public class AnimatedPictureBoxs : PictureBox 
     { 
      public static bool images; 
      List<string> imageFilenames; 
      Timer t = new Timer(); 
      public AnimatedPictureBoxs() 
      { 
       images = false; 
       AnimationInterval = 10; //It's up to you, the smaller, the faster. 
       t.Tick += Tick_Animate; 
      } 
      public int AnimationInterval 
      { 
       get { return t.Interval; } 
       set { t.Interval = value; } 
      } 
      public void Animate(List<string> imageFilenames) 
      { 
       this.imageFilenames = imageFilenames; 
       t.Start(); 
      } 
      public void StopAnimate() 
      { 
       t.Stop(); 
       i = 0; 
      } 
      int i; 
      private void Tick_Animate(object sender, EventArgs e) 
      { 
       if (images == true) 
       { 
        imageFilenames = null; 
       } 
       if (imageFilenames == null) 
       { 
        return; 
       } 
       else 
       { 
        try 
        { 
         if (i >= imageFilenames.Count) 
         { 
          i = 0; 
         } 
         else 
         { 

          Load(imageFilenames[i]); 
          i = (i + 1) % imageFilenames.Count; 

         } 
        } 
        catch (Exception err) 
        { 

         Logger.Write(err.ToString()); 

        } 
       } 
      } 
     } 
    } 
} 

我想要做的是當鼠標按鈕,用戶點擊後將暫停動畫,當他再次點擊鼠標的中間按鈕時,動畫將繼續。

但是在globalenter事件中,e dosent具有Button屬性。

+0

以及鼠標中鍵點擊事件應該被激活,當我進入其中一個pictureBoxes像globalPbsMouseEnterEvent – user3200169

+0

你需要'MouseEventArgs'。如何得到它們是另一個問題。 – user1306322

回答

1

在你的代碼,在這裏具體

public void globalPbsMouseEnterEvent(object sender, System.EventArgs e) 
{ 
    if (e.Button == MouseButtons.Middle) 
    { 
     MessageBox.Show("hi"); 
    } 
.................... 
} 

你不能做到這一點上MouseEnter真正需要使用MouseDownMouseWheel [例如]有MouseEventArgs

public void globalPb_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.MiddleButton = MouseButtonState.Pressed) 
    { 
     MessageBox.Show("hi"); 
    } 
.................... 
}