条纹按钮的制作方法
How to make a striped button
我希望能够在运行时创建自定义条纹按钮。
我可以创建按钮并设置背景颜色。
该按钮是具有许多属性的自定义控件。
这不起作用,按钮没有条纹:
Private Sub EventButton_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If isStripy Then
e.Graphics.FillRectangle(New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.WhiteSmoke, Me.BackColor), Me.Bounds)
End If
End Sub
感谢任何建议或指导,
谢谢
为此,您不应使用 Me.Bounds
,而应使用 e.ClipRectangle
。 Bounds
会给你相对于父控件的绑定位置。
那么你需要在Button
Paint
event
中做两件事来完成任务;
绘制 Button
BackColor
Dim brush As Drawing2D.HatchBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Brown, btn.BackColor)
e.Graphics.FillRectangle(brush, e.ClipRectangle()) 'Draw the background
重绘 String
重绘 BackColor
Dim stringSize As SizeF = e.Graphics.MeasureString(btn.Text, btn.Font) 'Needed to redraw the text
Dim textX As Single = (e.ClipRectangle().Width - stringSize.Width) / 2 'Assuming in the centre
Dim textY As Single = (e.ClipRectangle().Height - stringSize.Height) / 2 'Assuming in the centre
e.Graphics.DrawString(btn.Text, btn.Font, New SolidBrush(btn.ForeColor), textX, textY) 'Redraw the text
例如,您的代码应如下所示:
Imports System.Windows.Forms
Public Class Form1
Private Sub MyButton1_Paint(sender As Object, e As PaintEventArgs) Handles MyButton1.Paint
Dim btn As MyButton = TryCast(sender, MyButton)
If btn.IsStripy Then
Dim brush As Drawing2D.HatchBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Brown, btn.BackColor)
e.Graphics.FillRectangle(brush, e.ClipRectangle()) 'Draw the background
Dim stringSize As SizeF = e.Graphics.MeasureString(btn.Text, btn.Font) 'Needed to redraw the text
Dim textX As Single = (e.ClipRectangle().Width - stringSize.Width) / 2 'Assuming in the centre
Dim textY As Single = (e.ClipRectangle().Height - stringSize.Height) / 2 'Assuming in the centre
e.Graphics.DrawString(btn.Text, btn.Font, New SolidBrush(btn.ForeColor), textX, textY) 'Redraw the text
End If
End Sub
End Class
Public Class MyButton
Inherits Button
Public IsStripy As Boolean = True
End Class
你应该得到以下结果(注意按钮被剥离)
我希望能够在运行时创建自定义条纹按钮。 我可以创建按钮并设置背景颜色。 该按钮是具有许多属性的自定义控件。 这不起作用,按钮没有条纹:
Private Sub EventButton_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If isStripy Then
e.Graphics.FillRectangle(New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.WhiteSmoke, Me.BackColor), Me.Bounds)
End If
End Sub
感谢任何建议或指导,
谢谢
为此,您不应使用 Me.Bounds
,而应使用 e.ClipRectangle
。 Bounds
会给你相对于父控件的绑定位置。
那么你需要在Button
Paint
event
中做两件事来完成任务;
绘制
Button
BackColorDim brush As Drawing2D.HatchBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Brown, btn.BackColor) e.Graphics.FillRectangle(brush, e.ClipRectangle()) 'Draw the background
重绘
String
重绘BackColor
Dim stringSize As SizeF = e.Graphics.MeasureString(btn.Text, btn.Font) 'Needed to redraw the text Dim textX As Single = (e.ClipRectangle().Width - stringSize.Width) / 2 'Assuming in the centre Dim textY As Single = (e.ClipRectangle().Height - stringSize.Height) / 2 'Assuming in the centre e.Graphics.DrawString(btn.Text, btn.Font, New SolidBrush(btn.ForeColor), textX, textY) 'Redraw the text
例如,您的代码应如下所示:
Imports System.Windows.Forms
Public Class Form1
Private Sub MyButton1_Paint(sender As Object, e As PaintEventArgs) Handles MyButton1.Paint
Dim btn As MyButton = TryCast(sender, MyButton)
If btn.IsStripy Then
Dim brush As Drawing2D.HatchBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Brown, btn.BackColor)
e.Graphics.FillRectangle(brush, e.ClipRectangle()) 'Draw the background
Dim stringSize As SizeF = e.Graphics.MeasureString(btn.Text, btn.Font) 'Needed to redraw the text
Dim textX As Single = (e.ClipRectangle().Width - stringSize.Width) / 2 'Assuming in the centre
Dim textY As Single = (e.ClipRectangle().Height - stringSize.Height) / 2 'Assuming in the centre
e.Graphics.DrawString(btn.Text, btn.Font, New SolidBrush(btn.ForeColor), textX, textY) 'Redraw the text
End If
End Sub
End Class
Public Class MyButton
Inherits Button
Public IsStripy As Boolean = True
End Class
你应该得到以下结果(注意按钮被剥离)