3

嘿,我一直在寻找“如何为按钮添加边框”的答案,直到我的大脑受伤一般的想法是我有一个带有人像的按钮,这个按钮需要一个边框(绿色或红色)表示此人是在线还是离线。我的按钮是在运行时生成的,所以这一切都必须通过代码来实现。另请注意,这些按钮是使用数据库中的行数(dtActive)生成的,这就是我的按钮的生成方式:

Public Sub GenerateUsers()
    Dim ContactID As Integer = Nothing
    Dim FirstName As String = ""
    Dim LastName As String = ""
    Dim FullName As String = ""
    Dim INTwidth As Integer = Nothing
    Dim TextRendering As Size
    dim CordX As Integer = 20
    Dim CordY As Integer = 30
    Dim RunThrough As Integer= 0
    Dim tileLine As integer = 1
    For Each Row As DataRow In dtActive.Rows
        ContactID = Row(0)
        FirstName = Row(1)
        LastName = Row(3)
        FullName = FirstName & " " & LastName
        TextRendering = TextRenderer.MeasureText(FullName, Font)
        INTwidth = TextRendering.Width
        Dim NewLabel As New Label()
        With NewLabel
            .Parent = Me
            .Text = FullName.ToString
            .Location = New Point(CordX + 5, CordY + 5)
            .Name = "Label" & ContactID
            .AutoSize = False
            .Size = New System.Drawing.Size(INTwidth, 20)
            Me.GroupBox1.Controls.Add(NewLabel)
        End With
        Dim LogOnBtn As New Button
        With LogOnBtn
            .Parent = Me
            .Location = New Point(CordX, CordY)
            .Name = "Tile" & ContactID
            .Size = New System.Drawing.Size(140, 140)
            .Text = Nothing
            Me.GroupBox1.Controls.Add(LogOnBtn)
            AddHandler LogOnBtn.Click, AddressOf LogInOut
            Try
                .BackgroundImage = System.Drawing.Bitmap.FromFile(Row(17).ToString) 'System.Drawing.Bitmap.FromFile(Row(17).ToString)
                .BackColor = Color.Red
                .FlatStyle = FlatStyle.Flat
                .BackgroundImageLayout = ImageLayout.Zoom
            Catch ex As Exception
            End Try
        End With
        CordX += 145

        RunThrough += 1 'this ensures that once 3 buttons(tiles) have been made, next 3 will be on a new line
        If RunThrough = 3 Then
            CordX = 20
            CordY += 145
            RunThrough = 0
        End If
    Next
End Sub

这是我的窗体:http: //imageshack.us/photo/my-images/29/2bjm.jpg/

我的想法是它看起来像这样:http: //imageshack.us/photo/my-images/547/otuk.jpg/

4

1 回答 1

2

WinForms 没有像 WPF 那样的边框控件,因此您将不得不使用GraphicsPath 类自己绘制边框。

这是一个绘制圆角矩形的函数,您需要提供按钮的 x、y 坐标、高度/宽度和圆角的半径值:

Public Sub DrawRoundRect(g As Graphics, p As Pen, x As Single, y As Single, width As Single, height As Single, radius As Single)
    Dim gp As New GraphicsPath()

    gp.AddLine(x + radius, y, x + width - (radius * 2), y) ' Line
    gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90) ' Corner
    gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)) ' Line
    gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90) ' Corner
    gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height) ' Line
    gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90) ' Corner
    gp.AddLine(x, y + height - (radius * 2), x, y + radius) ' Line
    gp.AddArc(x, y, radius * 2, radius * 2, 180, 90) ' Corner

    gp.CloseFigure()

    g.DrawPath(p, gp)

    gp.Dispose()
End Sub
于 2013-08-08T20:07:25.263 回答