none
兩個Panel 共用一個scroll,滑鼠滾輪會影響 RRS feed

  • 問題

  • 我有兩個Panel

    都共用一個事件

          private void panel1_Scroll(object sender, ScrollEventArgs e)
            {
                if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
                {
                    this.panel1.VerticalScroll.Value = e.NewValue;
                    this.panel2.VerticalScroll.Value = e.NewValue;
                }
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                {
                    this.panel1.HorizontalScroll.Value = e.NewValue;
                    this.panel2.HorizontalScroll.Value = e.NewValue;
                }
            }

    這樣確實可以讓兩個Panel同步進行!!

    滑鼠拉Panel1的scrollbar,Panel2也會動!!

    但如果用滾輪去滾scrollbar,就不會同步了!!

    請問該怎麼改呢?

    2013年11月29日 上午 08:45

解答

  • 您好,

    請參考:讓Panel對Mouse滾輪事件(Wheel)有感覺

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form, IMessageFilter
        {
    
            // P/Invoke declarations
            [DllImport("user32.dll")]
            private static extern IntPtr WindowFromPoint(Point pt);
    
            [DllImport("user32.dll")]
            private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    
            public Form1()
            {
                InitializeComponent();
                Application.AddMessageFilter(this);
            }
    
            public bool PreFilterMessage(ref Message m)
            {
                if (m.Msg == 0x20a)
                {
                    // WM_MOUSEWHEEL, find the control at screen position m.LParam
                    Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                    IntPtr hWnd = WindowFromPoint(pos);
                    if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
                    {
                        SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
                        var ctrl = Control.FromHandle(hWnd) as Panel;
                        if (ctrl != null)
                        {
                            Console.WriteLine("Aft:{0}", ctrl.VerticalScroll.Value);
                            var se = new ScrollEventArgs(ScrollEventType.ThumbPosition, ctrl.VerticalScroll.Value, ScrollOrientation.VerticalScroll);
                            ProcScroll(se);
                        }
                        return true;
                    }
                }
                return false;
            }
    
            private void panel1_Scroll(object sender, ScrollEventArgs e)
            {
                ProcScroll(e);
            }
    
            private void panel2_Scroll(object sender, ScrollEventArgs e)
            {
                ProcScroll(e);
            }
    
            private void ProcScroll(ScrollEventArgs e)
            {
                int newValue = e.NewValue;
                if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
                {
    
                    this.panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum > newValue ? newValue : panel1.VerticalScroll.Maximum;
                    this.panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum > newValue ? newValue : panel2.VerticalScroll.Maximum;
                }
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                {
                    this.panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum > newValue ? newValue : panel1.HorizontalScroll.Maximum;
                    this.panel2.HorizontalScroll.Value = panel2.HorizontalScroll.Maximum > newValue ? newValue : panel2.HorizontalScroll.Maximum;
                }
            }
    
        }
    }
    


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/


    • 已編輯 亂馬客 2013年12月2日 上午 02:26 add code
    • 已標示為解答 KikrW 2013年12月2日 上午 02:56
    2013年12月2日 上午 01:59

所有回覆

  • 試看看這樣的做法是否OK?How to sync two panel scrolls
    2013年11月30日 上午 02:21
  • 謝謝您提供的做法,一樣可以達成同時控制兩個Panel

    但問題一樣,用滑鼠拉scrollbar可以同步,但用滑鼠滾輪就達不到同步!!

    謝謝!!!

    2013年12月2日 上午 01:15
  • 您好,

    請參考:讓Panel對Mouse滾輪事件(Wheel)有感覺

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form, IMessageFilter
        {
    
            // P/Invoke declarations
            [DllImport("user32.dll")]
            private static extern IntPtr WindowFromPoint(Point pt);
    
            [DllImport("user32.dll")]
            private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    
            public Form1()
            {
                InitializeComponent();
                Application.AddMessageFilter(this);
            }
    
            public bool PreFilterMessage(ref Message m)
            {
                if (m.Msg == 0x20a)
                {
                    // WM_MOUSEWHEEL, find the control at screen position m.LParam
                    Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                    IntPtr hWnd = WindowFromPoint(pos);
                    if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
                    {
                        SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
                        var ctrl = Control.FromHandle(hWnd) as Panel;
                        if (ctrl != null)
                        {
                            Console.WriteLine("Aft:{0}", ctrl.VerticalScroll.Value);
                            var se = new ScrollEventArgs(ScrollEventType.ThumbPosition, ctrl.VerticalScroll.Value, ScrollOrientation.VerticalScroll);
                            ProcScroll(se);
                        }
                        return true;
                    }
                }
                return false;
            }
    
            private void panel1_Scroll(object sender, ScrollEventArgs e)
            {
                ProcScroll(e);
            }
    
            private void panel2_Scroll(object sender, ScrollEventArgs e)
            {
                ProcScroll(e);
            }
    
            private void ProcScroll(ScrollEventArgs e)
            {
                int newValue = e.NewValue;
                if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
                {
    
                    this.panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum > newValue ? newValue : panel1.VerticalScroll.Maximum;
                    this.panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum > newValue ? newValue : panel2.VerticalScroll.Maximum;
                }
                if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                {
                    this.panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum > newValue ? newValue : panel1.HorizontalScroll.Maximum;
                    this.panel2.HorizontalScroll.Value = panel2.HorizontalScroll.Maximum > newValue ? newValue : panel2.HorizontalScroll.Maximum;
                }
            }
    
        }
    }
    


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/


    • 已編輯 亂馬客 2013年12月2日 上午 02:26 add code
    • 已標示為解答 KikrW 2013年12月2日 上午 02:56
    2013年12月2日 上午 01:59