如何在运行时调整Windows窗体控件的大小时调整鼠标光标。

时间:2014-07-26 10:18:33

标签: vb.net

仅出于学习目的,我正在尝试根据MSDN中提供的示例构建自定义绘制窗口面板([http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.visualstyles.visualstylerenderer%28v=vs.100%29.aspx][1]

如上所述如何在运行时调整窗体控件大小时调整鼠标光标。?

这是我到目前为止所做的,但我评论了需要调整的代码行 我真的很感激任何支持。感谢

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

0 个答案:

没有答案