Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 04-11-2009
Member
 
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
Reply With Quote
  #3  
Old 04-11-2009
Member
 
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 ...
Reply With Quote
  #4  
Old 04-11-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 11:52 AM.