Question button paint event on mousehover or mousedown

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have created my own button class that inherits the Button class and I have overrode the OnPaint method so I can draw my own graphics. However, I want to be able to achieve the same effect that the button.flatstyle.flat has: where it has a mousehover and mousedown color. How can i make my button draw a different color on mousehover or mousedown?

Here is my OnPaint method:
VB.NET:
    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        pevent.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        pevent.Graphics.Clear(Me.Parent.BackColor) 'get color from parent (fake transparency)

        Dim radius As Int32 = 12
        Dim buttonback As GraphicsPath = RoundRect(New Point(0), Me.Size, radius) 'draws rounded rectangle
        Dim highlight As GraphicsPath = TopLeft(New Point(0), Me.Size, radius) 'draws just topleft part
        Dim shadow As GraphicsPath = BottomRight(New Point(0), Me.Size, radius) 'draws just bottomright part
        Dim align As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}

        pevent.Graphics.FillPath(New SolidBrush(Me.BackColor), buttonback) 'set background color of button
        pevent.Graphics.FillPath(New TextureBrush(BackgroundImage, WrapMode.TileFlipY), buttonback) 'fill with gradient
        pevent.Graphics.DrawPath(New Pen(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), 1.5), highlight) 'add highlight top
        pevent.Graphics.DrawPath(New Pen(New SolidBrush(Color.FromArgb(200, 0, 0, 0)), 1.5), shadow) 'add lowlight bottom

        If Me.Text IsNot Nothing Then
            pevent.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.DisplayRectangle, align)
        End If

        If Not Enabled Then 'enabled property has been overridden btw
            pevent.Graphics.FillPath(New SolidBrush(Color.FromArgb(90, 0, 0, 0)), buttonback) 'darken button when disabled
        End If

    End Sub
 
Override OnMouseHover and the other mouse related methods to capture those states, Refresh the control and handle OnPaint accordingly. Pseudo:
VB.NET:
Private IsHover
OnMouseHover:  IsHover=True, Refresh
OnPaint: If IsHover Then paint hover
You can probably capture any possible state and translate to a PushButtonState Enumeration value, and use that in OnPaint to determine how to paint.
 
Thanks, that's what I was going todo however when I do it the way you suggested, it's slow. It takes a second for the button to actually change when you hover over it (even when I turn off any smoothing or anti-aliasing).
Unlike how the native flatstyle.flat mouseover works its almost immediate.
 
I think maybe you misunderstand what "hover" means. MouseHover means when the user moves the cursor over the control and then holds it still. If you want to do something as soon as the cursor enters the bounds of the control then you want MouseEnter, not MouseHover.
 
Back
Top