2016-02-29 66 views
0

我已經成功地得到某種形式使用通過實施下面的代碼觸摸屏上時,我的FlowLayoutPanel的滾動的...vb.net FlowLayoutPanel的觸摸屏滾動

Dim mouseDownPoint As Point 
Private Sub FlowLayoutPanelUsers_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseDown 
    If (e.Button = MouseButtons.Left) Then 
     Me.mouseDownPoint = e.Location 
    End If 

End Sub 

Private Sub FlowLayoutPanelUsers_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseMove 
    If (e.Button <> MouseButtons.Left) Then 
     Return 
    End If 

    If ((mouseDownPoint.X = e.Location.X) _ 
       AndAlso (mouseDownPoint.Y = e.Location.Y)) Then 
     Return 
    End If 

    Dim currAutoS As Point = FlowLayoutPanelUsers.AutoScrollPosition 
    If (mouseDownPoint.Y > e.Location.Y) Then 
     'finger slide UP 
     If (currAutoS.Y <> 0) Then 
      currAutoS.Y = (Math.Abs(currAutoS.Y) - 1) 
     End If 

    ElseIf (mouseDownPoint.Y < e.Location.Y) Then 
     'finger slide down 
     currAutoS.Y = (Math.Abs(currAutoS.Y) + 1) 
    Else 
     currAutoS.Y = Math.Abs(currAutoS.Y) 
    End If 

    If (mouseDownPoint.X > e.Location.X) Then 
     'finger slide left 
     If (currAutoS.X <> 0) Then 
      currAutoS.X = (Math.Abs(currAutoS.X) - 1) 
     End If 

    ElseIf (mouseDownPoint.X < e.Location.X) Then 
     'finger slide right 
     currAutoS.X = (Math.Abs(currAutoS.X) + 1) 
    Else 
     currAutoS.X = Math.Abs(currAutoS.X) 
    End If 

    FlowLayoutPanelUsers.AutoScrollPosition = currAutoS 
    mouseDownPoint = e.Location 
    'IMPORTANT 
End Sub 

這是一些代碼,我已經找到在stackoverflow,所以感謝那初步!

如果可能,我希望此代碼執行的操作與其滾動的方式相反,因此如果向左滾動,FlowLayoutPanel會向右滾動,如果向上滾動,面板向下滾動,有點像Web瀏覽器。

有沒有人有任何見解呢?我已經嘗試了將負號反轉爲正號的簡單方法,反之亦然,但沒有效果。

在此先感謝。

+0

好了,我發現e.Location.X爲代表的控制,而不是我想滾動父控件。在將e.Location更改爲... FlowLayoutPanelUsers.PointToClient(MousePosition)後,這可以起到一定的作用。以防萬一任何人想要做類似 –

回答

0

修改後的代碼...

Dim mouseDownPoint As Point 
Private Sub FlowLayoutPanelUsers_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseDown 
    If (e.Button = MouseButtons.Left) Then 
     Me.mouseDownPoint = FlowLayoutPanelUsers.PointToClient(MousePosition) 
    End If 

End Sub 

Private Sub FlowLayoutPanelUsers_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseMove 
    If (e.Button <> MouseButtons.Left) Then 
     Return 
    End If 

    If ((mouseDownPoint.X = FlowLayoutPanelUsers.PointToClient(MousePosition).X) _ 
       AndAlso (mouseDownPoint.Y = FlowLayoutPanelUsers.PointToClient(MousePosition).Y)) Then 
     Return 
    End If 

    Dim currAutoS As Point = FlowLayoutPanelUsers.AutoScrollPosition 
    If (mouseDownPoint.Y > FlowLayoutPanelUsers.PointToClient(MousePosition).Y) Then 
     'finger slide UP 
     If (currAutoS.Y <> 0) Then 
      currAutoS.Y = (Math.Abs(currAutoS.Y) - 1) 
     End If 

    ElseIf (mouseDownPoint.Y < FlowLayoutPanelUsers.PointToClient(MousePosition).Y) Then 
     'finger slide down 
     currAutoS.Y = (Math.Abs(currAutoS.Y) + 1) 
    Else 
     currAutoS.Y = Math.Abs(currAutoS.Y) 
    End If 

    If (mouseDownPoint.X > FlowLayoutPanelUsers.PointToClient(MousePosition).X) Then 
     'finger slide left 
     If (currAutoS.X <> 0) Then 
      currAutoS.X = (Math.Abs(currAutoS.X) - 1) 
     End If 

    ElseIf (mouseDownPoint.X < FlowLayoutPanelUsers.PointToClient(MousePosition).X) Then 
     'finger slide right 
     currAutoS.X = (Math.Abs(currAutoS.X) + 1) 
    Else 
     currAutoS.X = Math.Abs(currAutoS.X) 
    End If 

    FlowLayoutPanelUsers.AutoScrollPosition = currAutoS 
    mouseDownPoint = FlowLayoutPanelUsers.PointToClient(MousePosition) 
    'IMPORTANT 
End Sub 
+0

我試圖用這種方法來添加觸摸滾動到相當「擁擠」與控件的flowlayoutpanel。不幸的是,只有當你不碰其中一個控件時,它纔會起作用,但是這些控件之間的小小開放部分之一。 FlowLayoutPanel似乎沒有keypreview功能,因此滾動的重要事件將丟失。有什麼建議麼? – Destek