Visual Basic:按钮上的picuter边框。如何?

时间:2013-08-08 19:58:43

标签: vb.net

嘿,我一直在寻找,直到我的大脑因“如何为按钮添加边框”的答案而受伤 一般的想法是我有一个带有人物肖像的按钮,这个按钮需要一个边框(绿色或红色)来指示该人是在线还是离线。 我的按钮是在运行时生成的,因此任何这些都必须通过代码实现。 另请注意,这些按钮是使用数据库中的行数生成的(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/

1 个答案:

答案 0 :(得分:2)

WinForms没有像WPF那样的边框控件,因此您必须使用GraphicsPath Class自行绘制边框。

这是一个绘制圆角矩形的函数,你需要提供按钮的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