2014-02-07 180 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"); 
    } 
.................... 
}