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

前端 未结 6 974
暖寄归人
暖寄归人 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 05:08

    This solution seems to be the best one out there and the most commonly accepted one - however, if you scroll to the bottom and touch a the actual flowcontrol behind the buttons (I tried to make this so that there would be empty space), you then have to double tap-and-hold the button for the scrolling to resume. Restarting the application restores the phone-like scrolling functionality. I am wondering if anyone else has seen this or figured it out - try it with your apps and see if it is the case as well. I modified the snippet above so that you can start a new project, copy and paste this into form1's code, and hit run.

       Public Class Form1
            Dim FlowPanel As New FlowLayoutPanel
            Private Function GenerateButton(ByVal pName As String) As Button
                Dim mResult As New Button
                With mResult
                    .Name = pName
                    .Text = pName
                    .Width = 128
                    .Height = 128
                    .Margin = New Padding(0)
                    .Padding = New Padding(0)
                    .BackColor = Color.CornflowerBlue
                    AddHandler .MouseDown, AddressOf Button_MouseDown
                    AddHandler .MouseMove, AddressOf Button_MouseMove
                End With
    
                Return mResult
            End Function
    
    
    
            Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    
                Me.Width = 806
                Me.Height = 480
                FlowPanel.Padding = New Padding(0)
                FlowPanel.Margin = New Padding(0)
                ' FlowPanel.ColumnCount = Me.Width / (128 + 6)
                FlowPanel.Dock = DockStyle.Fill
                FlowPanel.AutoScroll = True
                Me.Controls.Add(FlowPanel)
                Dim i As Integer
                For i = 1 To 98
                    FlowPanel.Controls.Add(GenerateButton("btn" & i.ToString))
                Next
            End Sub
    
            Dim myMouseDownPoint As Point
            Dim myCurrAutoSMouseDown As Point
            Private Sub Button_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
                myMouseDownPoint = PointToClient(Cursor.Position)
                myCurrAutoSMouseDown = FlowPanel.AutoScrollPosition
            End Sub
    
            Private Sub Button_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    Dim mLocation As Point = PointToClient(Cursor.Position)
                    If myMouseDownPoint <> mLocation Then
                        Dim mCurrAutoS As Point
                        Dim mDeslocation As Point = myMouseDownPoint - mLocation
                        mCurrAutoS.X = Math.Abs(myCurrAutoSMouseDown.X) + mDeslocation.X
                        mCurrAutoS.Y = Math.Abs(myCurrAutoSMouseDown.Y) + mDeslocation.Y
    
                        FlowPanel.AutoScrollPosition = mCurrAutoS
    
                    End If
                End If
            End Sub
        End Class
    

提交回复
热议问题