gpt4 book ai didi

vb.net - 条纹按钮的制作方法

转载 作者:行者123 更新时间:2023-12-01 15:23:37 24 4
gpt4 key购买 nike

我希望能够在运行时创建自定义条纹按钮。我可以创建按钮并设置背景颜色。该按钮是具有许多属性的自定义控件。这不起作用,按钮没有条纹:

    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.ClipRectangleBounds 将为您提供相对于父控件的绑定(bind)位置。

然后你需要在 Button Paint event 中做两件事来完成任务;

  1. 绘制按钮 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
  2. 通过重绘 BackColor 重绘您删除的 String

    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

你应该得到以下结果(注意按钮被剥离)

enter image description here

关于vb.net - 条纹按钮的制作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34613095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com