none
關於MouseWheel事件的困惑 RRS feed

  • 問題

  • 使用VS2005 C#

    目的:希望能利用滾輪來控制Panel中VerticalScroll的位置
    作法:
    我在試著在Form1上面放上一個Panel,然後再Panel上再放上一個PictureBox
    將Panel.AutoScroll = true, 並將PictureBox的大小設的比Panel大。
    此時Panel的Scroll會出現。
    問題1:在Panel的事件中找不到MouseWheel的事件。

    問題2:試著到Form的建構子中手動加入。但測試結果發現,即使我滾動滾輪,竟然都不會觸發這個事件。

    這實在很令我感到非常困惑?

    以下是測試的程式碼

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace MouseEventTest2
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();

          this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        }

        void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
          this.panel1.VerticalScroll.Value = 10;
          this.panel1.Refresh();
          this.panel1.Invalidate();
          this.panel1.Update();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          //MessageBox.Show(SystemInformation.NativeMouseWheelSupport.ToString());
        }
      }

      partial class Form1
      {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該公開 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
        ///
        /// </summary>
        private void InitializeComponent()
        {
          this.panel1 = new System.Windows.Forms.Panel();
          this.pictureBox1 = new System.Windows.Forms.PictureBox();
          this.panel1.SuspendLayout();
          ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
          this.SuspendLayout();
          //
          // panel1
          //
          this.panel1.AutoScroll = true;
          this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
          this.panel1.Controls.Add(this.pictureBox1);
          this.panel1.Location = new System.Drawing.Point(4, 13);
          this.panel1.Name = "panel1";
          this.panel1.Size = new System.Drawing.Size(285, 256);
          this.panel1.TabIndex = 0;
          //
          // pictureBox1
          //
          this.pictureBox1.Location = new System.Drawing.Point(17, 20);
          this.pictureBox1.Name = "pictureBox1";
          this.pictureBox1.Size = new System.Drawing.Size(585, 602);
          this.pictureBox1.TabIndex = 0;
          this.pictureBox1.TabStop = false;
          //
          // Form1
          //
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(295, 280);
          this.Controls.Add(this.panel1);
          this.Name = "Form1";
          this.Text = "Form1";
          this.Load += new System.EventHandler(this.Form1_Load);
          this.panel1.ResumeLayout(false);
          ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
          this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.PictureBox pictureBox1;
      }
    }
    2007年6月8日 上午 03:51

解答

所有回覆