如何在输入鼠标事件时触发鼠标中键?

时间:2014-02-07 05:48:20

标签: c# winforms

我有这段代码:

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不存在Button不存在作为e的属性

这是form1的构造函数中的代码,其中我为全局enter事件创建实例。变量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事件中,剂量具有Button属性。

1 个答案:

答案 0 :(得分: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");
    }
....................
}