Draw Circle Inside Circule Ù‡ù‰ Vb.net

Introduction

Welcome to the second (and most likely, the last) installment of my resizing Drawn objects series. In Part 1, "Creating Resizable Drawings in Visual Basic.NET, Part ane: Rectangles," I covered how to set things upwardly nicely then that y'all have a resizable Rectangular object. Today, I will take it a step farther and evidence you lot how to resize round shapes and odd shapes.

Luckily, there are a lot of similarities from the previous installment, simply obviously there is nevertheless some piece of work alee. Permit's get started!

If you haven't all the same read the first role, please exercise and so. The first part also contains all the lawmaking likewise as a downloadable file containing the lawmaking. Yous will demand this project for this commodity.

Let'due south commencement with the new design. Design your project to resemble Figure one.

New Design
Figure 1: New Design

I have enlarged the PictureBox surface area and added three buttons. The buttons will be used for Rectangle, Circumvolve, and Odd Shape, respectively.

Add a new Component to your projection. Proper noun information technology something practical, such as clsMain, for example. Add the following code into clsMain:

          Public Enum Shape       Rect       Circle       Odd     End Enum        

This Enum just represents the blazon of object to be drawn. Motion the NodePos Enum from clsObject to clsMain:

          Public Enum NodePos        TopLeft       TopMiddle       TopRight        BottomLeft       BottomMiddle       BottomRight        LeftMiddle       RightMiddle        None     Terminate Enum        

This identifies the position of the Nodes on the drawn object. Your whole clsMain now should wait like the following:

Public Class clsMain    Public Enum Shape       Rect       Circle       Odd     End Enum     Public Enum NodePos        TopLeft       TopMiddle       TopRight        BottomLeft       BottomMiddle       BottomRight        LeftMiddle        RightMiddle        None     Finish Enum   End Grade        

The move was needed considering these 2 Enums will be used from within the Object class too as the form.

Add Component
Figure two: Add Component

You now need to import this class into both:

clsObject

Imports ResizableObject_HTG.clsMain

All the imports for clsObject should include the following:

Imports Arrangement.Drawing.Drawing2D Imports ResizableObject_HTG.clsMain        

frmResizeObject

Imports ResizableObject_HTG.clsMain

Add the following variables to clsObject:

          Individual shShape As Shape    Private grShape Every bit Graphics        

shShape volition identify the shape y'all want to draw, and grShape is a Graphics object to describe with. Edit your clsObject'due south constructor to include more parameters:

Public Sub New(ByVal rctTemp As Rectangle,          ByVal sh As Shape, _       ByVal pic As PictureBox)     rectObject = rctTemp          shShape = sh          blnClick = False     Me.picObject = pic     AddHandler picObject.MouseDown, AddressOf picObject_MouseDown    AddHandler picObject.MouseMove, AddressOf picObject_MouseMove    AddHandler picObject.MouseUp, AddressOf picObject_MouseUp          'AddHandler picObject.Paint, AddressOf picObject_Paint'          Try          grShape = pic.CreateGraphics()          Create(grShape, shShape)          Grab ex Equally Exception        MessageBox.Show(ex.Message)     Cease Effort  Stop Sub        

I take added the Shape and so that it can exist instantiated when this form gets called. Too, I have removed the Pigment event handler, considering yous will be using the grShape graphics object to draw. Edit the Create Sub to also include the Shape parameter:

          Public Sub Create(ByVal g Every bit Graphics, sh Every bit Shape)          Select Case sh          Case Shape.Rect          g.DrawRectangle(New Pen(Color.Green), rectObject)          Case Shape.Circle          1000.DrawEllipse(New Pen(Color.Orangish), rectObject)          Case Shape.Odd          DrawSpiral(g)          Finish Select          For Each npPos Equally NodePos In             [Enum].GetValues(GetType(NodePos))           g.DrawRectangle(New Pen(Color.Blueish), GetObject(npPos))        Next     Terminate Sub        

You also volition notice the Select statement to determine which shape has been chosen. If it is a Rectangle, depict a rectangle using the congenital-in Graphics capabilities; the same holds true with a circle that will draw an Ellipse. With the Odd shape, a sub named DrawSpiral gets called that draws a spiral, which follows next:

          Public Sub DrawSpiral(ByVal g Every bit Graphics)        Dim PI As Double = 3.14159265358979       'Dim Orientation Every bit Double = iii.356987413'       'two.718281828 orientation'       Dim Orientation As Double = two.718281828    'orientation'        Dim penSpiral As New Pen(Color.Maroon)        Dim cx As Integer       Dim 10 As Integer       Dim cy As Integer       Dim y As Integer        Dim rectSprial As New Rectangle(10, ten, 250, 250)        cx = rectSprial.Width / 2       cy = rectSprial.Height / 2        Dim a As Single       Dim b Every bit Single       Dim i As Long       Dim ang As Double        a = 0.xv    'shape'       b = 0.15    'shape'        For i = 0 To 10000    'size of screw'           ang = (PI / 720) * i           x = cx + (a * (Math.Cos(ang)) *          (Orientation ^ (b * ang)))          y = cy - (a * (Math.Sin(ang)) *          (Orientation ^ (b * ang)))           'The higher the + number, the thicker the lines'          g.DrawLine(penSpiral, x, y, x + i, y + 1)        Next i     End Sub        

I as well have commented out the Paint event for the PictureBox. You already will be using a Graphics object to draw with:

          'Private Sub picObject_Paint(ByVal sender Equally Object,'    'ByVal east As PaintEventArgs)'        'Attempt'           'Create(eastward.Graphics, shShape)'        'Catch ex As Exception'           'MessageBox.Show(ex.Message)'        'End Try'     'Finish Sub'        

The rest of the code in clsObject remains the same. Add the following variable to frmResizeObject:

          Private shShape As Shape

Add the following code to all three buttons:

          Private Sub Button1_Click(sender As Object, e Equally EventArgs) _          Handles Button1.Click        objRect = Nothing       shShape = Shape.Rect       objRect = New clsObject(New Rectangle(five, five, 350, 350), _                 shShape, picCanvas)     End Sub     Private Sub Button2_Click(sender As Object, e Every bit EventArgs) _          Handles Button2.Click       objRect = Zilch       shShape = Shape.Circumvolve       objRect = New clsObject(New Rectangle(5, 5, 350, 350), _                 shShape, picCanvas)     Finish Sub     Private Sub Button3_Click(sender As Object, e Equally EventArgs) _          Handles Button3.Click       objRect = Nothing       shShape = Shape.Odd       objRect = New clsObject(New Rectangle(5, five, 350, 350), _                 shShape, picCanvas)    End Sub        

That's information technology! It is not perfect, but information technology works and can exist made even more flexible. Running the plan results in the following outputs:

Rectangle
Figure iii: Rectangle

Circle
Effigy iv: Circle

Spiral
Figure v: Screw

The lawmaking for this commodity is available on GitHub.

Conclusion

As you can see: If you have gear up your application properly the start time, it is easy to expand and build onto it to provide ameliorate functionality. Enjoy playing around with your drawing awarding programming skills!

masseywortatuslege.blogspot.com

Source: https://www.codeguru.com/visual-basic/resizing-drawn-objects-with-vb-net-part-2-circles-and-odd-shapes/

0 Response to "Draw Circle Inside Circule Ù‡ù‰ Vb.net"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel