一个winform窗体中,有两个Panel
在窗体加载时注册了两个鼠标移动事件
this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.MouseWheel += new MouseEventHandler(panel2_MouseWheel);
代码如下:
        void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            // 处理鼠标滚动事件            // 此处判断鼠标是否在 Panel 区域中,如果不在则不响应滚动
            Rectangle pnlRightRectToForm = this.panel1.ClientRectangle; // 获得Panel的矩形区域
            pnlRightRectToForm.Offset(this.panel1.Location);            // 将Panel矩形区域转换为在Form空间中的占据区域
            if (!pnlRightRectToForm.Contains(e.Location))                 // 若当前鼠标位置点不在Panel区域中时
                return;            if (e.Delta < 0) // 向下滚动
            {                Point pos = new Point();
                pos.X = -this.panel1.AutoScrollPosition.X;        // 由于获取AutoScrollPosition的值为实际滚动值的负值
                pos.Y = -this.panel1.AutoScrollPosition.Y + 50;   // 故在此重新设置需要的滚动到的新值(位置值)
                this.panel1.AutoScrollPosition = pos;             // 切记获取AutoScrollPosition 与设置它的值所得结果并不相同
                this.panel1.AutoScrollPosition = pos;
            }
            else                    // 向上滚动
            {
                Point pos = new Point();
                pos.X = -this.panel1.AutoScrollPosition.X;
                pos.Y = -this.panel1.AutoScrollPosition.Y - 50;
                this.panel1.AutoScrollPosition = pos;
                this.panel1.AutoScrollPosition = pos;
            }
        }
当滚动鼠标滑轮时, 这两个panel一起上下滚  愁人   该怎么改呀

解决方案 »

  1.   

    为什么不分别在panel1和panel2中注册mouse事件呢?
      

  2.   

    this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
    this.MouseWheel += new MouseEventHandler(panel2_MouseWheel);
    这样,this.MouseWheel会执行两个动作
    至少你应该判断一下sender是panel1还是panel2,然后分别对待
      

  3.   

    那就不应该是注册两个MouseWheel事件,而是只注册成
    this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);然后在事件响应函数中写void panel1_MouseWheel(object sender, MouseEventArgs e)
            {
                // 处理鼠标滚动事件            // 此处判断鼠标是否在 Panel 区域中,如果不在则不响应滚动
                Rectangle pnlRightRectToForm1 = this.panel1.ClientRectangle; // 获得Panel的矩形区域
                pnlRightRectToForm1.Offset(this.panel1.Location);            // 将Panel矩形区域转换为在Form空间中的占据区域
                if (!pnlRightRectToForm1.Contains(e.Location))                 // 若当前鼠标位置点不在Panel区域中时
                    return;            if (e.Delta < 0) // 向下滚动
                {                Point pos = new Point();
                    pos.X = -this.panel1.AutoScrollPosition.X;        // 由于获取AutoScrollPosition的值为实际滚动值的负值
                    pos.Y = -this.panel1.AutoScrollPosition.Y + 50;   // 故在此重新设置需要的滚动到的新值(位置值)
                    this.panel1.AutoScrollPosition = pos;             // 切记获取AutoScrollPosition 与设置它的值所得结果并不相同
                    this.panel1.AutoScrollPosition = pos;
                }
                else                    // 向上滚动
                {
                    Point pos = new Point();
                    pos.X = -this.panel1.AutoScrollPosition.X;
                    pos.Y = -this.panel1.AutoScrollPosition.Y - 50;
                    this.panel1.AutoScrollPosition = pos;
                    this.panel1.AutoScrollPosition = pos;
                }            // 继续添加panel2的代码
                  Rectangle pnlRightRectToForm2 = this.panel2.ClientRectangle;
                // 以下类似panel1的做法
            }
      

  4.   

    改成:
    panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
    panel2.MouseWheel += new MouseEventHandler(panel2_MouseWheel);