读书人

C#开发软件这个按钮怎么变成这样?写什

发布时间: 2013-07-04 11:45:33 作者: rapoo

C#开发软件这个按钮如何变成这样?写什么样式..
C#开发软件这个按钮如何变成这样?写什么样式..
是软件 不是网页
左边的变成右边的
C#开发软件这个按钮怎么变成这样?写什么样式.
[解决办法]


Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class buttonEx
Inherits Button
Private _baseColor As Color = Color.FromArgb(160, 199, 223)
Private _controlState As ControlState
Private _imageWidth As Integer = 18
Private _roundStyle As RoundStyle = RoundStyle.All
Private _radius As Integer = 8

Public Sub New()
MyBase.New()
SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.SupportsTransparentBackColor, True)
End Sub

<DefaultValue(GetType(Color), "160, 199, 223")> _
Public Property BaseColor() As Color
Get
Return _baseColor
End Get
Set(ByVal value As Color)
_baseColor = value
MyBase.Invalidate()
End Set
End Property

<DefaultValue(18)> _
Public Property ImageWidth() As Integer
Get
Return _imageWidth


End Get
Set(ByVal value As Integer)
If Value <> _imageWidth Then

_imageWidth = If(Value < 12, 12, Value)
MyBase.Invalidate()
End If
End Set
End Property

<DefaultValue(GetType(RoundStyle), "1")> _
Public Property RoundStyle() As RoundStyle
Get
Return _roundStyle
End Get
Set(ByVal value As RoundStyle)
If _roundStyle <> Value Then
_roundStyle = Value
MyBase.Invalidate()
End If
End Set
End Property

<DefaultValue(8)> _
Public Property Radius() As Integer
Get
Return _radius
End Get
Set(ByVal value As Integer)
If _radius <> Value Then
_radius = If(Value < 4, 4, Value)
MyBase.Invalidate()
End If
End Set
End Property

Friend Property ControlState() As ControlState


Get
Return _controlState
End Get
Set(ByVal value As ControlState)
If _controlState <> Value Then
_controlState = Value
MyBase.Invalidate()
End If
End Set
End Property

Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
MyBase.OnMouseEnter(e)
ControlState = ControlState.Hover
End Sub

Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
MyBase.OnMouseLeave(e)
ControlState = ControlState.Normal
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
MyBase.OnMouseDown(e)
If e.Button = MouseButtons.Left AndAlso e.Clicks = 1 Then
ControlState = ControlState.Pressed
End If
End Sub

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
MyBase.OnMouseUp(e)
If e.Button = MouseButtons.Left AndAlso e.Clicks = 1 Then
If ClientRectangle.Contains(e.Location) Then
ControlState = ControlState.Hover
Else
ControlState = ControlState.Normal
End If


End If
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
MyBase.OnPaintBackground(e)

Dim g As Graphics = e.Graphics
Dim imageRect As Rectangle
Dim textRect As Rectangle

CalculateRect(imageRect, textRect)
g.SmoothingMode = SmoothingMode.AntiAlias

Dim baseColor As Color
Dim borderColor As Color
Dim innerBorderColor As Color = Color.FromArgb(200, 255, 255, 255)

If Enabled Then
Select Case ControlState
Case ControlState.Hover
baseColor = GetColor(_baseColor, 0, -13, -8, -3)
borderColor = _baseColor
Exit Select
Case ControlState.Pressed
baseColor = GetColor(_baseColor, 0, -35, -24, -9)
borderColor = _baseColor
Exit Select
Case Else
baseColor = _baseColor
borderColor = _baseColor


Exit Select
End Select
Else
'禁用背景和边框颜色
baseColor = Color.FromArgb(255, 255, 255)
borderColor = Color.FromArgb(56, 134, 207)
End If

RenderBackgroundInternal(g, ClientRectangle, baseColor, borderColor, innerBorderColor, RoundStyle, _
Radius, 0.35F, True, True, LinearGradientMode.Vertical)

If Image IsNot Nothing Then
g.InterpolationMode = InterpolationMode.HighQualityBilinear
g.DrawImage(Image, imageRect, 0, 0, Image.Width, Image.Height, _
GraphicsUnit.Pixel)
End If

TextRenderer.DrawText(g, Text, Font, textRect, ForeColor, GetTextFormatFlags(TextAlign, RightToLeft = RightToLeft.Yes))
End Sub

Private Sub CalculateRect(ByRef imageRect As Rectangle, ByRef textRect As Rectangle)
imageRect = Rectangle.Empty
textRect = Rectangle.Empty
If Image Is Nothing Then
textRect = New Rectangle(2, 0, Width - 4, Height)
Return
End If
Select Case TextImageRelation
Case TextImageRelation.Overlay
imageRect = New Rectangle(2, (Height - ImageWidth) \ 2, ImageWidth, ImageWidth)


textRect = New Rectangle(2, 0, Width - 4, Height)
Exit Select
Case TextImageRelation.ImageAboveText
imageRect = New Rectangle((Width - ImageWidth) \ 2, 2, ImageWidth, ImageWidth)
textRect = New Rectangle(2, imageRect.Bottom, Width, Height - imageRect.Bottom - 2)
Exit Select
Case TextImageRelation.ImageBeforeText
imageRect = New Rectangle(2, (Height - ImageWidth) \ 2, ImageWidth, ImageWidth)
textRect = New Rectangle(imageRect.Right + 2, 0, Width - imageRect.Right - 4, Height)
Exit Select
Case TextImageRelation.TextAboveImage
imageRect = New Rectangle((Width - ImageWidth) \ 2, Height - ImageWidth - 2, ImageWidth, ImageWidth)
textRect = New Rectangle(0, 2, Width, Height - imageRect.Y - 2)
Exit Select
Case TextImageRelation.TextBeforeImage
imageRect = New Rectangle(Width - ImageWidth - 2, (Height - ImageWidth) \ 2, ImageWidth, ImageWidth)
textRect = New Rectangle(2, 0, imageRect.X - 2, Height)
Exit Select


End Select

If RightToLeft = RightToLeft.Yes Then
imageRect.X = Width - imageRect.Right
textRect.X = Width - textRect.Right
End If
End Sub

读书人网 >C#

热点推荐