Things You'll Need
Instructions
Create a new Visual Basic project. The program can be started by painting the button, which can be done using the "AddEllipse" class. Type the following into the Visual Basic window:
Dim a As New System.Drawing.Drawing2D.GraphicsPath
Draw the edge of the buttons. To ensure smooth curves are drawn, set the SmoothingMode property to AntiAlias. Type the following code into the Visual Basic window:
Dim p1 As Pen
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.DrawArc(p1, recBorder, 180, -180)
Place a text label inside the button, and write the main control loop to determine if a button has been pressed. Type the following code into the Visual Basic window:
' Measure the size of the String to centralize it.
' If an image is present, the text is drawn next to the icon.
Dim textSize As SizeF = g.MeasureString(Me.Text, Me.Font)
' shifted 2 pixels to the right to simulate the 3D effect
If Me.mousePressed Then
Save the program, and run the program. A window should pop up with a series of circular and elliptical buttons.
recRegion = New RectangleF(0, 0, Me.Width, Me.Height)
a.AddEllipse(recRegion)
Me.Region = New Region(a)
Dim p2 As Pen
recBorder = New RectangleF(1, 1, Me.Width - 2, Me.Height - 2)
'This line is very important to have smooth curves.
' Now we need to draw the 3D effect.
g.DrawArc(p2, recBorder, 180, 180)
'If the Mouse is pressed draw the text and the image, if available,
If Me._Image Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, _
(((Me.Width + 3) - textSize.Width) / 2) + 2, _
(((Me.Height + 2) - textSize.Height) / 2) + 2)
Else
Dim pt As New Point(((Me.Width + 3) / 12) + 1, _
((Me.Height + 2 - 16) / 2) + 1)
Dim recString As New Rectangle(pt, New Size(16, 16))
g.DrawImage(_Image, recString)
g.DrawString(Me.Text, Me.Font, stringBrush, _
recString.X + recString.Width + 3,
(((Me.Height + 2) - textSize.Height) / 2) + 2)
End If
Else
If Me._Image Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, _
(((Me.Width + 3) - textSize.Width) / 2), _
(((Me.Height + 2) - textSize.Height) / 2))
Else
Dim pt As New Point((Me.Width + 3) / 12, (Me.Height + 2 - 16) / 2)
Dim recString As New Rectangle(pt, New Size(16, 16))
g.DrawImage(_Image, recString)
g.DrawString(Me.Text, Me.Font, stringBrush, _
recString.X + recString.Width + 3,
(((Me.Height + 2) - textSize.Height) / 2))
End If
End If