在c#中的图片框上引发鼠标滚轮事件

时间:2017-06-23 09:57:53

标签: c# winforms

在查看https://code.visualstudio.com/docs/languages/cssMousewheel event not firing以及MouseWheel event doesn't fire when using any control with scrolbars (in C# Windows Forms)之后,我编写了以下片段,其中......不起作用(即鼠标滚轮赢了)在VS2013,Win7,x64中点火。提供任何帮助。

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

namespace MyWinForm
{
    class Program
    {
        private Form theForm;
        private PictureBox thePictureBox;
        static void Main(string[] args)
        {

            Program theProgram = new Program();
            Application.Run(theProgram.theForm);

        }

        Program()
        {
            theForm = new Form();
            thePictureBox = new PictureBox();
            theForm.Controls.Add(thePictureBox);
            thePictureBox.Image = Image.FromFile(@"D:\cameraman.bmp");
            thePictureBox.Width = theImage.Width;
            thePictureBox.Height = theImage.Height;
            thePictureBox.MouseWheel += new MouseEventHandler(this.PictureBox_MouseWheel);
            thePictureBox.MouseHover += new EventHandler(this.PictureBox_MouseHover);

        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            Console.WriteLine("2 + 2 = 5");//Will never get here....

        }
        private void PictureBox_MouseHover(object sender, EventArgs e)
        {
            thePictureBox.Focus();


        }
    }
}

3 个答案:

答案 0 :(得分:1)

它适用于mouseMove而不是mousehover:

        pictureBox1.MouseMove += newEventHandler(this.PictureBox_MouseHover);

现在只需将名称从“悬停”更改为“移动”即可:p

答案 1 :(得分:0)

以防其他人正在寻找解决方案。 为了使pictureBox1触发MouseWheel事件,我添加了MouseEnter事件:

private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        pictureBox1.Focus();
    }

答案 2 :(得分:-1)

MouseWheel事件是中心鼠标事件。你必须滚动中心鼠标然后处理MouseWheel事件。