Hack Forums
[.NET] Custom button with fade effect when mouse over hides button image - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Custom button with fade effect when mouse over hides button image (/showthread.php?tid=5932911)



Custom button with fade effect when mouse over hides button image - LeonieHackPetaBytes2 - Jan 23, 2019

hey dear reddit, i was searching for a cool looking custom button with fade effect when mouse over, i found one pretty nice custom button theme, but unfortunately the fade effect is getting layed over the button overlay image:/ i want to use the buttons as maximize minimize and close buttons, so i added the images to the buttons but the forecolor fade is covering it.. i hope you understand what i want to fix its late at me and i cant sleep well until i found a sollution for that issue..


https: // i.imgur.com/6zB1f0x.gif

here is the button theme, just paste it into a new .vb file, run the project and the new button should show up in your toolbox:)



Code
Imports System.Drawing.Imaging

Public Class MyFadeEffect : Inherits Control

   Private ImgAtt As New ImageAttributes
   Private StartFade As Boolean, Alpha As Single = 0.0, SpeedFade As Single = 0.0
   Private ImgHover As Bitmap, GrHover As Graphics, _SetImage As Image

   Private Property SetImage() As Image
       Get
           Return _SetImage
       End Get
       Set(value As Image)
           _SetImage = value
           Invalidate()
       End Set
   End Property


   Sub New()
       SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
       DoubleBuffered = True
       Size = New Size(150, 50)
   End Sub


   Private Sub DrawFadeEffect(G As Graphics)
       SpeedFade = 0.11

       Dim _Matrix As New ColorMatrix(New Single()() {
                                      New Single() {1, 0, 0, 0, 0},
                                      New Single() {0, 1, 0, 0, 0},
                                      New Single() {0, 0, 1, 0, 0},
                                      New Single() {0, 0, 0, Alpha, 0},
                                      New Single() {0, 0, 0, 0, 1}})
       ImgAtt.SetColorMatrix(_Matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

       ImgHover = New Bitmap(Width, Height)
       GrHover = Graphics.FromImage(ImgHover)

       Dim _Brush As New SolidBrush(Color.FromArgb(250, Color.LightBlue))

       GrHover.FillRectangle(_Brush, 0, 0, Width, Height)

       G.DrawImage(ImgHover, New Rectangle(0, 0, ImgHover.Width, ImgHover.Height),
                   0, 0, ImgHover.Width, ImgHover.Height, GraphicsUnit.Pixel, ImgAtt)

       ImgHover.Dispose()
       GrHover.Dispose()
   End Sub


   Protected Overrides Sub OnPaint(e As PaintEventArgs)
       MyBase.OnPaint(e)
       e.Graphics.DrawRectangle(Pens.Red, 0, 0, Width - 1, Height - 1)
       TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor)

   End Sub

   Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
       MyBase.OnPaintBackground(e)
       DrawFadeEffect(e.Graphics)
   End Sub


   Protected Overrides Sub OnMouseEnter(e As EventArgs)
       MyBase.OnMouseEnter(e)
       StartFade = True
       TmrFade.Enabled = True
       Invalidate()
   End Sub


   Protected Overrides Sub OnMouseLeave(e As EventArgs)
       MyBase.OnMouseLeave(e)
       StartFade = False
       TmrFade.Enabled = True
       Invalidate()
   End Sub

   Private Sub TmrFade_Tick(sender As Object, e As EventArgs) Handles TmrFade.Tick
       If StartFade Then
           Alpha += SpeedFade
       Else
           Alpha -= SpeedFade
       End If

       SetImage = ImgHover

       If Alpha >= 1 Then
           Alpha = 1
           TmrFade.Enabled = False
       ElseIf Alpha <= 0 Then
           Alpha = 0
           TmrFade.Enabled = False
       End If

   End Sub

   Friend WithEvents TmrFade As New Timer With {.Enabled = False, .Interval = 1}
End Class



RE: Custom button with fade effect when mouse over hides button image - 0xDEAD10CC - Jan 23, 2019

This isn't reddit, but you need to draw the image after you draw the background. Lots of allocations here for things that could be created once too.