自定义调整鼠标位置大小不同,然后光标位置

时间:2017-03-20 09:07:02

标签: wpf vb.net

我想调整自定义窗口的大小,windowstyle=none 为此我想要使用一些Open-Sourelib。 所以我通过谷歌发现了这个article。 我稍微更改了代码后,因为我想使用一个按钮代替一个矩形来调整代码大小如下:

    Private bottomResize As Boolean = False
    Private initBtmY As Double

    Private Sub BottomResizeRect_MouseEnter _
    (ByVal sender As Object, ByVal e As _
    System.Windows.Input.MouseEventArgs) _
    Handles btResizeAndFold.MouseEnter

        bottomResize = False
        'Console.WriteLine("Mouse Enter called")
    End Sub

    Dim boing As Boolean = False
    Private Sub BottomResizeRect_MouseLeftButtonDown _
    (ByVal sender As Object, ByVal e As _
    System.Windows.Input.MouseButtonEventArgs) _
    Handles btResizeAndFold.PreviewMouseLeftButtonDown
        bottomResize = True
        boing = True
        'Console.WriteLine("Mouse left down called")

        'Get the initial Y coordinate 
        'cursor location on our window
        initBtmY = e.GetPosition(Me).Y
    End Sub

    Private Sub BottomResizeRect_MouseLeftButtonUp _
    (ByVal sender As Object,
    ByVal e As System.Windows.Input.MouseButtonEventArgs) _
    Handles btResizeAndFold.PreviewMouseLeftButtonUp
        'Console.WriteLine("Mouse left up called")
        bottomResize = False
        btResizeAndFold.ReleaseMouseCapture()
    End Sub

    Private Sub BottomResizeRect_MouseMove _
    (ByVal sender As Object, ByVal e As _
    System.Windows.Input.MouseEventArgs) _
    Handles btResizeAndFold.PreviewMouseMove
        'Get the new Y coordinate cursor location

        Dim newBtmY As Double = e.GetPosition(Me).Y
        'Get the change between the initial and
        'new cursor location
        Dim diff As Double = initBtmY - newBtmY
        'Minimum window height
        Dim minHeight As Integer = 200
        Dim differnceConstant = 5

        If bottomResize = True And (diff > differnceConstant Or diff < (differnceConstant * -1)) Then
            'Let our rectangle capture the mouse
            btResizeAndFold.CaptureMouse()

            Dim newHeight = e.GetPosition(Me).Y - diff

            If newHeight > minHeight Then
                Me.Height = newHeight
            End If

        End If
    End Sub

现在的问题是,如果我尝试通过按下鼠标左键然后拖动鼠标来调整窗口大小,则窗口高度的增加/减少与鼠标光标的移动不同步,所以问题是:如何使鼠标同步的运动成为窗口的增长

1 个答案:

答案 0 :(得分:2)

您可以使用Thumb来代替Button,它提供符合您要求的DragDelta事件:

https://wpf.2000things.com/2012/12/19/715-using-the-thumb-control-to-drag-objects-on-a-canvas/

这样你就不需要捕捉鼠标了(你不应该把CaptureMouse放在PreivewMouseMove中,它会做很多事情。)

只需将高度设置为当前高度+ΔY。