Control.MouseWheel 事件
Control.MouseWheel Event
我创建了一个简单的应用程序,用于使用鼠标滚轮缩放图片框内的图像。它 运行 非常适合我的开发笔记本电脑 (Win10)。但是当我 运行 它在我的桌面 PC (Win7) 上时,缩放(使用鼠标滚轮)功能不起作用。
下面是我的实现片段:
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(4, 0);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(493, 583);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.AutoSize = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(1, 2);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(714, 593);
this.panel1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(719, 594);
this.Controls.Add(this.panel1);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private float ZOOM = 1.5f
private void pictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
pictureBox1.Focus();
if (e.Delta < 0) //ZoomIn
{
Console.WriteLine("Mouse Wheel Zoom In");
if ((pictureBox1.Width < panel1.Width) && (pictureBox1.Height < panel1.Height))
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * ZOOM);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * ZOOM);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.Refresh();
}
}
else
{
//ZoomOut
Console.WriteLine("Mouse Wheel Zoom Out");
if ((pictureBox1.Width > panel1.Width) &&
(pictureBox1.Height > panel1.Height))
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / ZOOM);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / ZOOM);
}
}
}
我认为这是我台式电脑上的 Control.MouseWheel 事件的问题。当我调试时,尽管我已经在图片框内聚焦或单击,但此事件从未出现。当我通过过滤 WM_MOUSEWHEEL = 0x20a;
消息尝试使用实现的其他实现时,它在我的笔记本电脑和台式机上都有效。知道为什么会发生这些不同的行为吗?感谢您的时间。
事实证明,Win 10 有一个名为“当我将鼠标悬停在它们上方时滚动不活动 windows”的系统选项。这就是为什么我以前的代码仅在 Win 10 机器上工作的原因。我将以下行添加到修复它。感谢@HansPassant 的提示。
private void picBox_MouseHover(object sender, EventArgs e)
{
picBox.Focus();
}
我创建了一个简单的应用程序,用于使用鼠标滚轮缩放图片框内的图像。它 运行 非常适合我的开发笔记本电脑 (Win10)。但是当我 运行 它在我的桌面 PC (Win7) 上时,缩放(使用鼠标滚轮)功能不起作用。
下面是我的实现片段:
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(4, 0);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(493, 583);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.AutoSize = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(1, 2);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(714, 593);
this.panel1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(719, 594);
this.Controls.Add(this.panel1);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private float ZOOM = 1.5f
private void pictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
pictureBox1.Focus();
if (e.Delta < 0) //ZoomIn
{
Console.WriteLine("Mouse Wheel Zoom In");
if ((pictureBox1.Width < panel1.Width) && (pictureBox1.Height < panel1.Height))
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * ZOOM);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * ZOOM);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.Refresh();
}
}
else
{
//ZoomOut
Console.WriteLine("Mouse Wheel Zoom Out");
if ((pictureBox1.Width > panel1.Width) &&
(pictureBox1.Height > panel1.Height))
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / ZOOM);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / ZOOM);
}
}
}
我认为这是我台式电脑上的 Control.MouseWheel 事件的问题。当我调试时,尽管我已经在图片框内聚焦或单击,但此事件从未出现。当我通过过滤 WM_MOUSEWHEEL = 0x20a;
消息尝试使用实现的其他实现时,它在我的笔记本电脑和台式机上都有效。知道为什么会发生这些不同的行为吗?感谢您的时间。
事实证明,Win 10 有一个名为“当我将鼠标悬停在它们上方时滚动不活动 windows”的系统选项。这就是为什么我以前的代码仅在 Win 10 机器上工作的原因。我将以下行添加到修复它。感谢@HansPassant 的提示。
private void picBox_MouseHover(object sender, EventArgs e)
{
picBox.Focus();
}