How to Draw a Circle Visual Basic Net

In Part 1 of this series, you created the Framework for your Cartoon Application, just it's by no means perfect nonetheless. A lot of improvements still need to be fabricated. In this article, y'all will:

  • Include the capability to first draw, and then fill up, the drawn objects
  • Include a marquee while drawing, just so you have a visible cue of the diameters of the current cartoon
  • Refine the triangle logic because in Function 1, the triangle wasn't as it should be, and didn't really draw according to the mouse movements

Enough talk. It'southward time to start!

Enable the Filling of Already Drawn Shapes

At the moment, yous tin can depict outline shapes, and depict Filled shapes, but you cannot Fill an already fatigued shape with a dissimilar color; that's all near to modify now. The first step is to add a Context Menu Object to frmTools. (If you accept not downloaded the lawmaking from Office 1, you should exercise then kickoff).

Adding A Context Menu

  • Open frmTools in the Form designer.
  • In the Toolbox (On the Windows Forms tab), scroll down until you lot observe the Context Menu Object. Double-click it to add together it to frmTools.
  • In the Properties Window, name the Context Menu cmCanvas.
  • Make sure the cmCanvas is still selected. If a Context Menu is selected, the menu volition appear at the superlative of the form. Click Context Menu at the height of the class.
  • Add the following items to the menu:
  • Command Name Properties
    mnuCOutline
    Text Outline Depict
    Checked Truthful
    RadioCheck True
    mnuCFillDraw
    Text Total Fill up Draw
    RadioCheck True
    mnuCDrawFill
    Text Draw And so Fill Full
    RadioCheck True
  • On frmTools, select the push button named butCFillColor, and set the ContextMenu property to cmCanvas.
  • You may alter the Image property to a better picture show indicating that this push contains a sub menu. In the included project, you will notice that I accept changed the picture to PaintBucket2.png; it looks almost the aforementioned equally the original except that it now contains a small-scale black box in the bottom right corner—to bespeak to the users where to click.

Speaking of clicking, add the necessary code to frmTools to set the proper ToolTips and enable the ContextMenu actions.

Adding code to frmTools to actuate the new ContextMenu

  • In frmTools_Load, supplant the following line:
  • tipCanvas.SetToolTip(butCFillColor, "Draw A Filled Shape")

    With:

    tipCanvas.SetToolTip(butCFillColor, "Fill Options")
  • Comment out the butCFillColor_Click effect procedure because, seeing the fact that you now have a context card, yous are not going to utilize information technology anymore:
  • 'Private Sub butCFillColor_Click(ByVal sender _ 'Every bit System.Object, ByVal due east Equally System.EventArgs) _ 'Handles butCFillColor.Click    'How Many Times Is This Tool Clicked?    'sFillClicked = sFillClicked + 1    'If This Tool Is Clicked Once ...    'If sFillClicked = 1 And then    ' 'Load A Different Picture (Change State)    ' butCFillColor.Image = Image.FromFile("PaintBucket.png")    ' 'Display A Different ToolTip, To Signal State /    ' 'Tool Change    ' tipCanvas.SetToolTip(butCFillColor, "Return to Outline    ' Color")    ' 'Fill Is Clicked, Fill The Objects Based On blnFillClicked    ' blnFillClicked = True    'Else    ' 'Fill up Is Not Clicked Anymore, Return To Original Button /    ' 'Tool State    ' sFillClicked = 0    ' blnFillClicked = Simulated    ' butCFillColor.Image = Image.FromFile("PaintBrush.png")    ' tipCanvas.SetToolTip(butCFillColor, "Draw A Filled Shape")    'End If 'End Sub          
  • Add the butCFillColor_MouseDown event to handle the Contextmenu:
    Private Sub butCFillColor_MouseDown(ByVal sender As Object, _    ByVal e As Organization.Windows.Forms.MouseEventArgs) _    Handles butCFillColor.MouseDown    If e.Button = MouseButtons.Left And eastward.X > 28 And east.Y > 28 And then       butCFillColor.ContextMenu.Testify(butCFillColor, _                                      New Bespeak(e.X, eastward.Y))    Stop If Cease Sub            

    Hither, you are determining where the MouseButton was pressed, so that yous then can show the context carte du jour.

  • Add outcome procedures for each of the ContextMenu items:
    Individual Sub mnuCOutline_Click(ByVal sender As Organization.Object, _    ByVal e As System.EventArgs) Handles mnuCOutline.Click    blnOutlineDrawClicked = True    blnFillDrawClicked    = False    blnDrawFillClicked    = False    mnuCOutline.Checked   = True    mnuCFillDraw.Checked  = False    mnuCDrawFill.Checked  = False End Sub            
    Private Sub mnuCFillDraw_Click(ByVal sender As System.Object, _    ByVal e As Organisation.EventArgs) Handles mnuCFillDraw.Click    blnOutlineDrawClicked = False    blnFillDrawClicked    = True    blnDrawFillClicked    = False    mnuCOutline.Checked   = Simulated    mnuCFillDraw.Checked  = True    mnuCDrawFill.Checked  = Imitation End Sub            
    Private Sub mnuCDrawFill_Click(ByVal sender As System.Object, _    ByVal due east As System.EventArgs) Handles mnuCDrawFill.Click    blnOutlineDrawClicked = Fake    blnFillDrawClicked    = False    blnDrawFillClicked    = True    mnuCOutline.Checked   = Faux    mnuCFillDraw.Checked  = False    mnuCDrawFill.Checked  = Truthful    blnCircleClicked      = False    blnTriangleClicked    = False    blnSquareClicked      = Fake    blnDrawClicked        = Faux    blnEraserClicked      = Fake Stop Sub            

The filling subs

The adjacent stride is to exercise the actual filling of the shapes once they are drawn. For this, you'll need to add the following two sub procedures to frmCanvas. The first sub is called CanvasFloodFill. The whole object of this office is to exercise the actual filling of the shapes. Take a closer wait:

Individual Sub CanvasFloodFill(ByVal FillBmp As Bitmap, _                             ByVal cffX As Integer, _                             ByVal cffY As Integer, _                             ByVal cffNewCol Every bit Colour) 'Get One-time Pixel Color Dim cffOldCol As Color = FillBmp.GetPixel(cffX, cffY) 'If Old Color Equals The New Color Get out If cffOldCol.ToArgb = cffNewCol.ToArgb Then Get out Sub  'Create A Stack For All The Points Dim FloodStack As New Stack(1000) 'Put Current Pixel On Stack FloodStack.Push(New Point(cffX, cffY)) 'Set up Current Pixel To New Color FillBmp.SetPixel(cffX, cffY, cffNewCol)  'While At that place Are Items In The Stack Do While FloodStack.Count > 0    'Remove & Return Point At Top Of Stack    Dim FillPt As Point = DirectCast(FloodStack.Popular(), Point)    If FillPt.X > 0 Then CanvasGetSetPixel(FillBmp, FloodStack, _       FillPt.X - i, FillPt.Y, cffOldCol, cffNewCol)    'Left    If FillPt.Y > 0 And then CanvasGetSetPixel(FillBmp, FloodStack, _       FillPt.10, FillPt.Y - 1, cffOldCol, cffNewCol)    'Top    'Right    If FillPt.X < FillBmp.Width - 1 Then CanvasGetSetPixel(FillBmp, _       FloodStack, FillPt.X + 1 _, FillPt.Y, cffOldCol, cffNewCol) _       FillPt.Y + one, cffOldCol, cffNewCol)    'Bottom Loop FloodStack.Articulate()    'Articulate Stack End Sub        

The next sub is called CanvasGetSetPixel. This procedure identifies the current pixel's colour, and so whether the current pixel is the aforementioned equally the old color—in other words, supersede the current colour with the new colour.

Private Sub CanvasGetSetPixel(ByVal gspBmp As Bitmap, _                               ByVal GetSetStack As Stack, _                               ByVal gspX Equally Integer, _                               ByVal gspY As Integer, _                               ByVal gspOldCol Equally Color, _                               ByVal gspNewCol As Color) 'Get Current Pixel Colour Dim gspClr As Color = gspBmp.GetPixel(gspX, gspY) 'If Electric current Color Equals Old Colour If gspClr.ToArgb = gspOldCol.ToArgb And so 'Push Next Indicate To Peak Of Stack    GetSetStack.Push(New Point(gspX, gspY)) 'Fix New Pixel Colour    gspBmp.SetPixel(gspX, gspY, gspNewCol) Terminate If End Sub        

These two subs piece of work in conjunction with 1 some other. The i sub creates a Stack onto which you can put all the pixels that need to exist replaced. The next sub determines which pixels to supercede. Not really as complicated as it looks.

To employ these subs, you demand to telephone call the CanvasFloodFill sub (which in turn calls the CanvasGetSetPixel sub) within the picCDraw_MouseDown event.

If blnDrawFillClicked Then    'Get Electric current Colour    Dim old_color As Color = bImage.GetPixel(east.X, e.Y)    'New Color = Selected Colour    Dim new_color As Color = cColor    If e.Push = MouseButtons.Left Then       'Fill Shape With New Color If Left Clicked       CanvasFloodFill(DirectCast(picCDraw.Image, Bitmap), _                                  e.10, e.Y, new_color)    Else       'Fill Shape With Old Colour If Right Clicked       CanvasFloodFill(DirectCast(picCDraw.Paradigm, Bitmap), _                                  e.X, due east.Y, old_color)    End If Stop If        

When run, the user is now able to fill the drawn shapes. He/she will select either cull to draw in Outline style or in Total Fill style. The user cannot depict with Draw then Fill up mode! Once the user has drawn a shape, he/she needs to select the Draw then Fill up option. In one case selected, he/she can simply click once inside the section he/she wants filled. If the user wants to draw once again, he/she must select Outline Draw or Full Fill Depict again. Big drawn objects will accept a bit longer to fill than smaller shapes.

couchjoiny1968.blogspot.com

Source: https://www.codeguru.com/dotnet/creating-your-own-drawing-application-with-visual-basic-net-part-2/

0 Response to "How to Draw a Circle Visual Basic Net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel