Vb.net de xp buton
'VB.net de butonları XP yapmak icin
'(form1 in ustundeki button1 icin)
'oncelikle vb.net in drawing clasını import etmeliyiz,
'bunun icin formun en ustune;
Imports System.Drawing.Drawing2
'bundan sonra windows form designer generated code ifadesinin
'hemen altına grekli tanımlamaları yapalım;
Enum BtnState
Disabled
Normal
Hot
Pushed
End Enum
Const cornerR As Integer = 4
Const cornerD As Integer = (cornerR * 2)
'simdi butonun seklini degistiren paint Sub ını olusturalım;
Private Sub PaintXPButton(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Button1.Paint
'eger butonun ismi degisik ise yukarda button1 yerine kendi butonunuzun ismini yazmalısınız isterseniz virgul koyup istediginiz diger butonları yazarak aynı efekti verebilirsiniz.
Dim btn As Button = CType(sender, Button)
Dim backgroundBrush As New SolidBrush(Me.BackColor)
Dim bMouseDown As Boolean = (btn.MouseButtons And MouseButtons.Left) <> 0
Dim ptMouse As Point = Me.PointToClient(btn.MousePosition)
Dim bMouseInButton As Boolean = btn.Bounds.Contains(ptMouse)
'butonun davranısını belirliyelim
Dim state As BtnState
If (btn.Enabled = False) Then
state = BtnState.Disabled
ElseIf (bMouseDown = False) Then
If (bMouseInButton = True) Then
state = BtnState.Hot
Else 'bMouseInButton = False
state = BtnState.Normal
End If
Else 'bMouseDown = True
If (bMouseInButton = True) Then
state = BtnState.Pushed
Else 'bMouseInButton = False
state = BtnState.Hot
End If
End If
e.Graphics.FillRectangle(backgroundBrush, e.ClipRectangle)
Dim rect As New Rectangle(1, 1, e.ClipRectangle.Width - (cornerR + 1), e.ClipRectangle.Height - (cornerR + 1))
Dim layoutRect As New RectangleF(rect.X, rect.Y, rect.Width, rect.Height)
Dim gradNormal As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, 10, e.ClipRectangle.Height), _
SystemColors.ControlLightLight, SystemColors.ActiveCaption, LinearGradientMode.Vertical)
Dim gradPushed As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, 10, e.ClipRectangle.Height), _
SystemColors.ActiveCaption, SystemColors.ControlLightLight, LinearGradientMode.Vertical)
Dim strFormat As New StringFormat()
strFormat.Alignment = StringAlignment.Center
strFormat.LineAlignment = StringAlignment.Center
Dim pp As GraphicsPath = RoundRectPath(rect, cornerR)
If (state <> BtnState.Disabled) Then
Dim shadowRect As New Rectangle(0, 0, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2)
e.Graphics.DrawLine(SystemPens.ControlDarkDark, _
shadowRect.Left, shadowRect.Bottom - (cornerR + 1), _
shadowRect.Left, shadowRect.Top + cornerR)
e.Graphics.DrawArc(SystemPens.ControlDarkDark, _
New Rectangle(shadowRect.Left, shadowRect.Top, cornerD, cornerD), 180, 90)
e.Graphics.DrawLine(SystemPens.ControlDarkDark, _
shadowRect.Left + cornerR, shadowRect.Top, _
shadowRect.Right - (cornerR + 1), shadowRect.Top
e.Graphics.DrawArc(SystemPens.ControlDark, _
New Rectangle(shadowRect.Right - (cornerD + 1), shadowRect.Top, cornerD, cornerD), 270, 90)
e.Graphics.DrawLine(SystemPens.ControlLightLight, _
shadowRect.Right - 1, shadowRect.Top + cornerR, _
shadowRect.Right - 1, shadowRect.Bottom - (cornerR + 1))
e.Graphics.DrawArc(SystemPens.ControlLightLight, _
New Rectangle(shadowRect.Right - (cornerD + 1), _
shadowRect.Bottom - (cornerD + 1), cornerD, cornerD), 0, 90)
e.Graphics.DrawLine(SystemPens.ControlLightLight, _
shadowRect.Right - (cornerR + 1), shadowRect.Bottom - 1, _
shadowRect.Left + cornerR, shadowRect.Bottom - 1)
e.Graphics.DrawArc(SystemPens.ControlLight, _
New Rectangle(shadowRect.Left, shadowRect.Bottom - (cornerD + 1), cornerD, cornerD), 90, 90)
End If
Select Case state
Case BtnState.Disabled
e.Graphics.FillPath(SystemBrushes.ControlLight, pp)
e.Graphics.DrawPath(SystemPens.ControlDarkDark, pp)
e.Graphics.DrawString(btn.Text, btn.Font, SystemBrushes.ControlDarkDark, layoutRect, strFormat)
Case BtnState.Hot, BtnState.Normal
e.Graphics.FillPath(gradNormal, pp)
e.Graphics.DrawPath(SystemPens.ControlDarkDark, pp)
If (state = BtnState.Hot) Then
e.Graphics.DrawLine(Pens.Orange, rect.Left + 1, rect.Top + 1, rect.Left + 1, rect.Bottom - 1)
e.Graphics.DrawLine(Pens.Orange, rect.Right - 1, rect.Top + 1, rect.Right - 1, rect.Bottom - 1)
e.Graphics.DrawLine(Pens.PeachPuff, rect.Left + 2, rect.Top + 1, rect.Right - 2, rect.Top + 1)
e.Graphics.DrawLine(Pens.DarkOrange, rect.Left + 2, rect.Bottom - 1, rect.Right - 2, rect.Bottom - 1)
e.Graphics.DrawRectangle(Pens.Orange, rect.Left + 2, rect.Top + 2, rect.Width - 4, rect.Height - 5)
End If
e.Graphics.DrawString(btn.Text, btn.Font, SystemBrushes.ActiveCaptionText, layoutRect, strFormat)
Case BtnState.Pushed
e.Graphics.FillPath(gradPushed, pp)
Dim hiRect As Rectangle = rect
hiRect.Inflate(-1, -1)
e.Graphics.DrawRectangle(SystemPens.ControlLightLi ght, hiRect)
e.Graphics.DrawPath(SystemPens.ControlDarkDark, pp)
e.Graphics.DrawString(btn.Text, btn.Font, SystemBrushes.ActiveCaptionText, layoutRect, strFormat)
End Select
pp.Dispose()
strFormat.Dispose()
backgroundBrush.Dispose()
gradNormal.Dispose()
gradPushed.Dispose()
End Sub
'simdi de butona son seklini bir fonksiyon yardımıyla verelim
Public Shared Function RoundRectPath(ByVal rect As Rectangle, ByVal cornerRadius As Integer) As GraphicsPath
Dim cornerDiameter As Integer = cornerRadius * 2
Dim pp As New GraphicsPath()
pp.AddLine(rect.Left + cornerRadius, rect.Top, rect.Right - cornerRadius, rect.Top)
pp.AddArc(New Rectangle(rect.Right - cornerDiameter, rect.Top, cornerDiameter, cornerDiameter), 270, 90)
pp.AddLine(rect.Right, rect.Top + cornerRadius, rect.Right, rect.Bottom - cornerRadius)
pp.AddArc(New Rectangle(rect.Right - cornerDiameter, rect.Bottom - cornerDiameter, cornerDiameter, cornerDiameter), 0, 90)
pp.AddLine(rect.Right - cornerRadius, rect.Bottom, rect.Left + cornerRadius, rect.Bottom)
pp.AddArc(New Rectangle(rect.Left, rect.Bottom - cornerDiameter, cornerDiameter, cornerDiameter), 90, 90)
pp.AddLine(rect.Left, rect.Bottom - cornerRadius, rect.Left, rect.Top + cornerRadius)
pp.AddArc(New Rectangle(rect.Left, rect.Top, cornerDiameter, cornerDiameter), 180, 90)
pp.CloseFigure()
Return pp
End Function