Results 1 to 4 of 4

Thread: Draw a rectangle using VB.NET

  1. #1
    Join Date
    Nov 2009
    Posts
    36

    Draw a rectangle using VB.NET

    I'm trying to create an application in vb.net like the Windows Paint, but I do not know how we can create a rectangle. Or rather use to create the DrawRectangle and leave the initial point and end the event MouseDown MouseUp. But I want to make sure that while you drag the mouse over the picturebox I see a preview of what will create, but I have no idea how to do.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Draw a rectangle using VB.NET

    Use the MouseMove event to update the preview. Control.MouseMove Event occurs when the mouse pointer is moved over the control.

    Declaration:

    Code:
    Public Event MouseMove As MouseEventHandler
    Usage:

    Code:
    Dim instance As Control
    Dim handler As MouseEventHandler
    
    AddHandler instance.MouseMove, handler

  3. #3
    Join Date
    Nov 2009
    Posts
    36

    Re: Draw a rectangle using VB.NET

    I had already thought, but I do not know how. Basically I have 2 routines, one that I copy in memory the initial image that is in PaintBox1, and the other charging me a video:

    Code:
    Private Sub SaveImageTEMP()
    TempBMP = New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
    PictureBox1.DrawToBitmap(TempBMP, New Rectangle(0, 0, TempBMP.Width, TempBMP.Height))
    Exit Sub
    
    Private Sub PasteImageTEMP()
    Dim bm As Bitmap = TempBMP 
    Dim bm_dest As New Bitmap(bm.Width, bm.Height)
    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
    gr_dest.DrawImage(bm, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
    g = Graphics.FromImage(bm)
    PictureBox1.Image = bm
    Exit Sub
    Then in the events related to Paintbox:
    MouseDown:
    SaveImageTEMP-here I call the procedure and stores the coordinates of the point of departure

    Mousemove:
    -do from the PasteImageTEMP to fetch initial image, then stores the point where I am and launch a DrawRectangle

    MouseUp
    -name still PasteImageTEMP and then draw the rectangle final.

    But still can not see anything while dragging the mouse, but when I release it draws the rectangle you want, plus a series of rectangles "intermediate" I would have used before ...

  4. #4
    Join Date
    May 2008
    Posts
    685

    Re: Draw a rectangle using VB.NET

    Not so much draw the rectangle itself, the problem as the fact that giving the Refresh Control on where you draw, involves the cancellation of any existing rectangles.

    In short, a good solution to this problem is to separate the current design drawings that are already on the control. When disconnecting the mouse (MouseDown) create an object "right", and add it to a collection. When drawing (MouseMove) as well as refresh the Control to get rid of the "intermediate traces" of the drawing, I'm going to redraw the rectangles above.

    In my example drawing rectangles on a PictureBox ("PCB"):

    Code:
    Public Class Form1
    
        Private P As New Pen(Color.Green, 2)
        Private R As Rectangle
        Private rect As Rectangle
        Private rectangles As New List(Of Rectangle)
    
        Private inDrawing As Boolean = False
        Private xStart As Integer = 0
        Private yStart As Integer = 0
        Private xEnd As Integer = 0
        Private yEnd As Integer = 0
    
        Private Class Rectangle
            Public Rct As Rectangle
            Public Pn As Pen
            Public Sub New(ByVal P As Pen, ByVal R As Rectangle)
                Pn = P
                Rct = R
            End Sub
        End Class
    
        Private Sub RiDraw()
    
            PCB.Refresh()
            PCB.CreateGraphics.DrawRectangle(P, rect)
            For Each R As Rectangle In rectangle
                PCB.CreateGraphics.DrawRectangle(R.Pn, R.Rct)
            Next
    
        End Sub
    
        Private Sub PCB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PCB.MouseDown
    
            PCB.Cursor = Cursors.Cross
            inDrawing = True
            xStart = e.X
            yStart = e.Y
    
        End Sub
    
        Private Sub PCB_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PCB.MouseMove
    
            If inDrawing = False Then Exit Sub
    
            xEnd = e.X
            yEnd = e.Y
            rect = New Rectangle
            If xStart < xEnd Then
                rect.X = xStart
            Else
                rect.X = xEnd
            End If
            If yStart < yEnd Then
                rect.Y = yStart
            Else
                rect.Y = yEnd
            End If
            rect.Width = Math.Abs(xEnd - xStart)
            rect.Height = Math.Abs(yEnd - yStart)
    
            RiDraw()
    
        End Sub
    
        Private Sub PCB_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PCB.MouseUp
    
            PCB.Cursor = Cursors.Arrow
            inDrawing = False
            R = New Rectangle(P, rect)
            rectangles.Add(R)
    
        End Sub
    
    End Class

Similar Threads

  1. Make artboard from (indesign) rectangle
    By Madhuparna in forum Windows Software
    Replies: 6
    Last Post: 14-07-2010, 12:03 AM
  2. What is the use of Rectangle class in java?
    By Gokul20 in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 03:46 PM
  3. Need to fix problems of Drawing Rectangle in C++.
    By super soaker in forum Software Development
    Replies: 5
    Last Post: 23-01-2010, 07:07 PM
  4. label inside a rectangle
    By Linux-Us in forum Software Development
    Replies: 2
    Last Post: 21-11-2009, 02:59 PM
  5. Universal Focus Rectangle Remover
    By XDRoX in forum Tips & Tweaks
    Replies: 2
    Last Post: 16-04-2009, 09:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,023,599.75640 seconds with 17 queries