how to get smartphone like scrolling for a winforms touchscreen app ( scrolling panel )

前端 未结 6 984
暖寄归人
暖寄归人 2020-12-30 04:15

After scouring the articles online I have come up with this design for a winforms based touchscreen app that needs smartphone like scrolling. The app itself will run on a ta

6条回答
  •  半阙折子戏
    2020-12-30 04:42

    7 Years later but for anyone that is looking for a neat and tidy winforms solution:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
        /// 
        /// Pass the panel into constructor and the control will be turned into a touch scrollable control.
        /// 
        public class TouchScroll
        {
            private Point mouseDownPoint;
            private Panel parentPanel;
    
            /// 
            /// pass in the panel you would like to be touch scrollable and it will be handled here.
            /// 
            /// The root panel you need to scroll with
            public TouchScroll(Panel panel)
            {
                parentPanel = panel;
                AssignEvents(panel);
            }
    
            private void AssignEvents(Control control)
            {
                control.MouseDown += MouseDown;
                control.MouseMove += MouseMove;
                foreach (Control child in control.Controls)
                    AssignEvents(child);
            }
    
            private void MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                    this.mouseDownPoint = Cursor.Position;
            }
    
            private void MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                    return;
    
                Point pointDifference = new Point(Cursor.Position.X - mouseDownPoint.X, Cursor.Position.Y - mouseDownPoint.Y);
    
                if ((mouseDownPoint.X == Cursor.Position.X) && (mouseDownPoint.Y == Cursor.Position.Y))
                    return;
    
                Point currAutoS = parentPanel.AutoScrollPosition;
                parentPanel.AutoScrollPosition = new Point(Math.Abs(currAutoS.X) - pointDifference.X, Math.Abs(currAutoS.Y) - pointDifference.Y);
                mouseDownPoint = Cursor.Position; //IMPORTANT
            }
        }
    }
    

提交回复
热议问题