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.
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
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 ...
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