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

前端 未结 6 975
暖寄归人
暖寄归人 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:52

    To the original post, considering Cheap Funeral answer.

    This version is more simple and more fluid to the touch (considers the finger move speed but not considering the effect of still scrolling after removing the finger, like moving but speeding down(maybe i'll do that whem i have time))

    Works for a panel or FlowLayoutPanel

    Create a form and insert a FlowLayoutPanel on it.

    Form code:

    Public Class Form1
    
    Private Function GenerateButton(pName As String) As Button
        Dim mResult As New Button
        With mResult
            .Name = pName
            .Text = pName
            .Width = FlowPanel.Width
            .Height = 100
            .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(sender As Object, e As EventArgs) Handles MyBase.Load
        FlowPanel.Padding = New Padding(0)
        FlowPanel.Margin = New Padding(0)
        Dim i As Integer
        For i = 1 To 100
            FlowPanel.Controls.Add(GenerateButton("btn" & i.ToString))
        Next
    End Sub
    
    Dim myMouseDownPoint As Point
    Dim myCurrAutoSMouseDown As Point
    Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs) Handles FlowPanel.MouseDown
        myMouseDownPoint = PointToClient(Cursor.Position)
        myCurrAutoSMouseDown = FlowPanel.AutoScrollPosition
    End Sub
    
    Private Sub Button_MouseMove(sender As Object, e As MouseEventArgs) Handles FlowPanel.MouseMove
        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
    

    TIP: To hide the scrolls of the flowlayoutpanel(or panel), create a usercontrol that inherits from flowlayoutpanel(or panel) and:

    Imports System.Runtime.InteropServices
    
    Public Class FlowLayoutPanelExt
    Inherits FlowLayoutPanel
    
    
     _
    Private Shared Function ShowScrollBar(hWnd As IntPtr, wBar As Integer, bShow As Boolean) As  Boolean
    End Function
    
    Private Enum ScrollBarDirection
        SB_HORZ = 0
        SB_VERT = 1
        SB_CTL = 2
        SB_BOTH = 3
    End Enum
    
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If Me.Visible Then
            ShowScrollBar(Me.Handle, CInt(ScrollBarDirection.SB_BOTH), False)
            MyBase.WndProc(m)
        End If
    End Sub
    
    Public Sub New()
    
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        Me.AutoScroll = True
    End Sub
    
    End Class
    

提交回复
热议问题