Draw Circle Inside Circule Vb.net
Introduction
Welcome to the second (and most probable, the final) installment of my resizing Drawn objects series. In Part 1, "Creating Resizable Drawings in Visual Bones.NET, Part 1: Rectangles," I covered how to set things up nicely so that yous have a resizable Rectangular object. Today, I will take it a step further and prove you how to resize circular shapes and odd shapes.
Luckily, there are a lot of similarities from the previous installment, only manifestly there is still some work ahead. Let's become started!
If you haven't yet read the first part, please practice so. The start function as well contains all the lawmaking besides as a downloadable file containing the code. You will demand this projection for this article.
Let's start with the new design. Blueprint your project to resemble Effigy 1.
Figure ane: New Blueprint
I have enlarged the PictureBox area and added iii buttons. The buttons volition be used for Rectangle, Circumvolve, and Odd Shape, respectively.
Add a new Component to your project. Name it something practical, such equally clsMain, for case. Add together the post-obit code into clsMain:
Public Enum Shape Rect Circumvolve Odd End Enum
This Enum simply represents the type of object to exist drawn. Motility the NodePos Enum from clsObject to clsMain:
Public Enum NodePos TopLeft TopMiddle TopRight BottomLeft BottomMiddle BottomRight LeftMiddle RightMiddle None End Enum
This identifies the position of the Nodes on the drawn object. Your whole clsMain at present should look like the post-obit:
Public Class clsMain Public Enum Shape Rect Circle Odd Cease Enum Public Enum NodePos TopLeft TopMiddle TopRight BottomLeft BottomMiddle BottomRight LeftMiddle RightMiddle None End Enum End Form
The move was needed because these 2 Enums will exist used from inside the Object form every bit well equally the form.
Effigy 2: 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 System.Drawing.Drawing2D Imports ResizableObject_HTG.clsMain
frmResizeObject
Imports ResizableObject_HTG.clsMain
Add the following variables to clsObject:
Private shShape As Shape Private grShape As Graphics
shShape will identify the shape yous desire to draw, and grShape is a Graphics object to draw with. Edit your clsObject's constructor to include more parameters:
Public Sub New(ByVal rctTemp Every bit Rectangle, ByVal sh As Shape, _ ByVal moving-picture show Equally PictureBox) rectObject = rctTemp shShape = sh blnClick = False Me.picObject = picture AddHandler picObject.MouseDown, AddressOf picObject_MouseDown AddHandler picObject.MouseMove, AddressOf picObject_MouseMove AddHandler picObject.MouseUp, AddressOf picObject_MouseUp 'AddHandler picObject.Pigment, AddressOf picObject_Paint' Endeavour grShape = motion-picture show.CreateGraphics() Create(grShape, shShape) Catch ex As Exception MessageBox.Evidence(ex.Message) Finish Try Stop Sub
I have added the Shape then that it tin can be instantiated when this form gets chosen. Also, I have removed the Paint effect handler, considering you will exist using the grShape graphics object to draw. Edit the Create Sub to also include the Shape parameter:
Public Sub Create(ByVal g As Graphics, sh As Shape) Select Case sh Case Shape.Rect g.DrawRectangle(New Pen(Color.Green), rectObject) Instance Shape.Circumvolve thou.DrawEllipse(New Pen(Color.Orange), rectObject) Case Shape.Odd DrawSpiral(k) Cease Select For Each npPos As NodePos In [Enum].GetValues(GetType(NodePos)) one thousand.DrawRectangle(New Pen(Color.Blue), GetObject(npPos)) Next End Sub
You as well will detect the Select argument to make up one's mind which shape has been chosen. If it is a Rectangle, draw a rectangle using the built-in Graphics capabilities; the same holds true with a circle that will draw an Ellipse. With the Odd shape, a sub named DrawSpiral gets chosen that draws a spiral, which follows next:
Public Sub DrawSpiral(ByVal g As Graphics) Dim PI Every bit Double = 3.14159265358979 'Dim Orientation Equally Double = 3.356987413' 'ii.718281828 orientation' Dim Orientation Equally Double = 2.718281828 'orientation' Dim penSpiral Equally New Pen(Color.Maroon) Dim cx As Integer Dim x As Integer Dim cy As Integer Dim y Every bit Integer Dim rectSprial Equally New Rectangle(x, 10, 250, 250) cx = rectSprial.Width / ii cy = rectSprial.Height / 2 Dim a As Single Dim b As Single Dim i As Long Dim ang As Double a = 0.15 'shape' b = 0.15 'shape' For i = 0 To 10000 'size of screw' ang = (PI / 720) * i ten = 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' thousand.DrawLine(penSpiral, 10, y, x + ane, y + 1) Adjacent i Cease Sub
I too take commented out the Paint event for the PictureBox. You already will exist using a Graphics object to describe with:
'Individual Sub picObject_Paint(ByVal sender As Object,' 'ByVal e As PaintEventArgs)' 'Effort' 'Create(e.Graphics, shShape)' 'Grab ex As Exception' 'MessageBox.Show(ex.Message)' 'End Endeavor' 'Terminate Sub'
The rest of the code in clsObject remains the aforementioned. Add the post-obit variable to frmResizeObject:
Private shShape As Shape
Add together the post-obit code to all three buttons:
Individual Sub Button1_Click(sender Equally Object, east Every bit EventArgs) _ Handles Button1.Click objRect = Nix shShape = Shape.Rect objRect = New clsObject(New Rectangle(five, 5, 350, 350), _ shShape, picCanvas) End Sub Private Sub Button2_Click(sender As Object, e Every bit EventArgs) _ Handles Button2.Click objRect = Nothing shShape = Shape.Circle objRect = New clsObject(New Rectangle(5, 5, 350, 350), _ shShape, picCanvas) End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) _ Handles Button3.Click objRect = Nothing shShape = Shape.Odd objRect = New clsObject(New Rectangle(5, five, 350, 350), _ shShape, picCanvas) End Sub
That's it! Information technology is not perfect, but it works and tin can be made even more flexible. Running the program results in the post-obit outputs:
Figure 3: Rectangle
Figure 4: Circle
Effigy v: Spiral
The code for this article is available on GitHub.
Determination
Equally you can see: If you have ready your application properly the first fourth dimension, it is easy to expand and build onto it to provide better functionality. Enjoy playing around with your drawing application programming skills!
Source: https://www.codeguru.com/visual-basic/resizing-drawn-objects-with-vb-net-part-2-circles-and-odd-shapes/
Publicar un comentario for "Draw Circle Inside Circule Vb.net"