0

For learning purpose only, I'm trying to build a custom paint window panel based on an example available here in MSDN ([http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.visualstyles.visualstylerenderer%28v=vs.100%29.aspx][1])

as stated above how to adjust mouse cursor during resizing windows forms control at run time.?

here is what i did till now, but i commented the code lines which required to be adjusted really i will be appreciated for any kind of support. Thanks

Public Class WindowPanel
Inherits ContainerControl


Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
    MyBase.OnMouseDown(e)
    If windowRectangles("windowDropDownButton").Contains(e.Location) Then
        ' set pressed color 
        dropDownButtonColor = Color.Green
        isDropDown = True

    ElseIf windowRectangles("windowBottomRight").Contains(e.Location) Then
        isResizing = True

        Me.Cursor = Cursors.SizeNWSE
        resizeOffset.X = Me.Right - Me.Left - e.X
        resizeOffset.Y = Me.Bottom - Me.Top - e.Y

    ElseIf windowRectangles("windowCaption").Contains(e.Location) Then
        isMoving = True
        originalClick.X = e.X
        originalClick.Y = e.Y

    End If

    Invalidate()
End Sub

Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
    MyBase.OnMouseUp(e)

    If isMoving Then
        isMoving = False

        ' Change the cursor back to the default if the as user stops resizing. 
    ElseIf isResizing Then
        isResizing = False

    ElseIf windowRectangles("windowDropDownButton").Contains(e.Location) And isDropDown Then
        ' show dropDownMenu
        ' for testing purpose only, close thr application
        Application.Exit()
    End If

End Sub

Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
    MyBase.OnMouseMove(e)

    If (MouseButtons.Left And e.Button) = MouseButtons.Left Then

        If isResizing Then
            Me.Width = e.X + resizeOffset.X
            Me.Height = e.Y + resizeOffset.Y
            CalculateRectangles()

        ElseIf isMoving Then

            Dim XChange As Integer = Me.Location.X + (e.X - originalClick.X)
            Dim YChange As Integer = Me.Location.Y + (e.Y - originalClick.Y)
            Me.Location = New Point(XChange, YChange)

        ElseIf Not windowRectangles("windowDropDownButton").Contains(e.Location) And isDropDown Then
            isDropDown = False
            ' draw the normal image dropdwon
            ' now and for testing purpose only set the dropdown button filling color to normal color 
            dropDownButtonColor = Color.Gray
        End If

        ' The left mouse button is not down. 
    Else

        If windowRectangles("windowDropDownButton").Contains(e.Location) Then
            ' draw the hot image dropdwon
            ' but now and for testing purpose only set the dropdown button filling color to hot color 
            dropDownButtonColor = Color.Red
        Else
            ' draw the normal image dropdwon
            ' but now and for testing purpose only set the dropdown button filling color to normal color 
            dropDownButtonColor = Color.Gray
        End If


        ' ============================= To Do: solve the cursor problem ===========================================

        ' Use a resizing cursor if the cursor is on the windowBottomRight corner
        If windowRectangles("windowBottomRight").Contains(e.Location) Then
            Me.Cursor = Cursors.SizeNWSE
        Else
            Me.Cursor = Cursors.Default
        End If

        '' set the resizing cursor if the cursor is on the windowTopLeft corner
        'If windowRectangles("windowTopLeft").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeNWSE
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        '' set the resizing cursor if the cursor is on the windowTopRight corner
        'If windowRectangles("windowTopRight").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeNESW
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        '' set the resizing cursor if the cursor is on the windowBottomLeft corner
        'If windowRectangles("windowBottomLeft").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeNESW
        'Else
        '    Me.Cursor = Cursors.Default
        'End If


        '' set the resizing cursor if the cursor is on the windowTop
        'If windowRectangles("windowTop").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeNS
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        '' set the resizing cursor if the cursor is on the windowBottom
        'If windowRectangles("windowBottom").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeNS
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        '' set the resizing cursor if the cursor is on the windowLeft
        'If windowRectangles("windowLeft").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeWE
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        '' set the resizing cursor if the cursor is on the windowRight
        'If windowRectangles("windowRight").Contains(e.Location) Then
        '    Me.Cursor = Cursors.SizeWE
        'Else
        '    Me.Cursor = Cursors.Default
        'End If

        ' =========================================================================================================

    End If
    Invalidate()
End Sub


End Class
silver
  • 13
  • 3
  • Well, that's all rather painful. Windows already helps you implement this correctly, it asks you where the mouse is located with the WM_NCHITTEST message. Just give the proper response and it will change the mouse cursor for you. Check [this answer](http://stackoverflow.com/a/2666977/17034) for the code. – Hans Passant Jul 26 '14 at 12:02
  • Hans, i know how to do it by overriding WndProc but i wanna do it this way. thanks. – silver Jul 26 '14 at 14:32
  • Consider it a kind of challenge, the funny thing i did it for the rectangle and image too but i can not dot it this way. I think the idea here is to change the behavior of IF...ElseIf method – silver Jul 26 '14 at 14:50

0 Answers0