简体   繁体   中英

How do i trigger the mouse middle button in enter event of the mouse?

I have this code:

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;

        }

But e.Button not exist Button not exist as property for e

This is the code in the constructor of form1 where i make the instance for the global enter event. The variable pbs is is array of 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();
            }

This is the code in the class 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());

                    }
                }
            }
        }
    }
}

What i want to do is when the user click on the middle mouse button it will pause the animation and when he click again on the middle button of the mouse the animation will continue.

But in the globalenter event e dosent have the Button property.

In your code, specifically here

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

You can't do it on MouseEnter actually need to use MouseDown , MouseWheel [for example] to have MouseEventArgs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM