Hobbies And Interests
Home  >> Science & Nature >> Science

How to Make a Circular Button in VB

A computer program is a series of commands that is executed by the central processing unit. There are many different programming languages, but the ones which are most often used today are C++, C# and Visual Basic (VB). Visual Basic is a programming language which also has a visual interface for creating the program interface, and this makes it particularly useful for the beginner. The standard Visual Basic interface only allows the creation of rectangular buttons, but circular buttons can be created using a small amount of code.

Things You'll Need

  • Visual Basic
Show More

Instructions

    • 1

      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
      recRegion = New RectangleF(0, 0, Me.Width, Me.Height)
      a.AddEllipse(recRegion)
      Me.Region = New Region(a)

    • 2

      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
      Dim p2 As Pen
      recBorder = New RectangleF(1, 1, Me.Width - 2, Me.Height - 2)
      'This line is very important to have smooth curves.

      g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
      ' Now we need to draw the 3D effect.

      g.DrawArc(p1, recBorder, 180, -180)
      g.DrawArc(p2, recBorder, 180, 180)

    • 3

      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)
      'If the Mouse is pressed draw the text and the image, if available,

      ' shifted 2 pixels to the right to simulate the 3D effect

      If Me.mousePressed Then
      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

    • 4

      Save the program, and run the program. A window should pop up with a series of circular and elliptical buttons.


https://www.htfbw.com © Hobbies And Interests