Aeglasin
Aeglasin

Reputation: 165

Custom resizing to mouse position different then cursor position

I want to resize a custom window, so windowstyle=none.
For that I do not want to use some Open-Sourelib. So I found this article via google. After I changed the code a bit, since i want to use a button instead of a rectangle for resizing the code looks like this:

    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

The problem now is, that if i try to resize my window by pressing the left mouse button and then drag the mouse, the increase/decrease of the height of my window is not synchonosly to the movement of my mouse cursor, so the question is: how to make the movement of the mouse sychronos to growth of the window

Upvotes: 0

Views: 121

Answers (1)

Mishka
Mishka

Reputation: 516

Instead of Button you could use Thumb, which provide with DragDelta Event that suits your requirement:

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

That way you dont need to capture the mouse(you shouldnt've put the CaptureMouse in the PreivewMouseMove anyway, it will do that a lot).

And just set the height as the current height + the delta Y.

Upvotes: 2

Related Questions