1
我創建了一個簡單的應用程序,用鼠標滾輪縮放圖片框內的圖像。它在我的開發筆記本電腦(Win10)上運行完美。但是當我在臺式電腦(Win7)上運行它時,縮放(使用鼠標滾輪)功能不起作用。Control.MouseWheel事件
下面是我的我執行段:
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);
}
}
}
我覺得這是我的桌面PC上的Control.MouseWheel事件的問題。當我調試時,這個事件從來沒有出現,雖然我已經集中或點擊圖片框內。當我通過過濾WM_MOUSEWHEEL = 0x20a;
消息來嘗試其他實現時,它可以在我的筆記本電腦和我的桌面上運行。任何想法爲什麼這些不同的行爲發生?感謝您的時間。
Win10有一個系統選項,當我將鼠標懸停在它們上方時,滾動非活動窗口。默認打開。 Win7沒有這個。而且PictureBox和Panel都不會變得「活躍」。您可以訂閱PictureBox的MouseMove事件,並調用其Focus()方法作爲解決方法。 –
謝謝@HansPassant。我添加了MouseHover事件並且它正在工作。 – fins