Drawing with mouse in VB

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 15061 Views - Last Post: 06 February 2012 - 11:39 AM Rate Topic: ----- 1 Votes

#1 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Drawing with mouse in VB

Post icon  Posted 18 December 2009 - 08:53 AM

Hi,

I am trying to draw in visual basic express edition 2008....

I have this code:

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
	Dim shouldpaint As Boolean
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		shouldpaint = False
	End Sub

	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		shouldpaint = True
	End Sub

	Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		shouldpaint = False
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		If shouldpaint = True Then
			Dim graphic As Graphics = CreateGraphics()

			graphic.FillRectangle _
			   (New SolidBrush(Color.Black), e.X, e.Y, 4, 4)
		End If
	End Sub
End Class


However, when I draw very fast, there is a big distance between the dots.....how can I fix this ?

TIA :D

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing with mouse in VB

#2 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 18 December 2009 - 10:07 AM

Anyone ?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9049
  • View blog
  • Posts: 33,972
  • Joined: 12-June 08

Re: Drawing with mouse in VB

Posted 18 December 2009 - 11:55 AM

You are drawing shapes, right? Not a user defined line?

Try this:
http://www.dreaminco...wtopic59049.htm
Was This Post Helpful? 0
  • +
  • -

#4 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Drawing with mouse in VB

Posted 18 December 2009 - 12:11 PM

View Postdarko01, on 18 Dec, 2009 - 02:53 PM, said:

However, when I draw very fast, there is a big distance between the dots.....how can I fix this ?


Instead of drawing 4x4 rectangles at the point where the mouse is, you should draw a line between the point where the mouse is now, and the previous location of the mouse.

You'll need another variable at the Form level to store the previous mouse location, say we call it PreviousLocation. Then in the mouse move event draw a line between the current location and PreviousLocation, and then update PreviousLocation with the current point for the next time mouse move fires.

This post has been edited by crepitus: 18 December 2009 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 18 December 2009 - 12:49 PM

View Postmodi123_1, on 18 Dec, 2009 - 10:55 AM, said:

You are drawing shapes, right? Not a user defined line?

Try this:
http://www.dreaminco...wtopic59049.htm


I am trying to draw a line...not a circle or similar

View Postcrepitus, on 18 Dec, 2009 - 11:11 AM, said:

View Postdarko01, on 18 Dec, 2009 - 02:53 PM, said:

However, when I draw very fast, there is a big distance between the dots.....how can I fix this ?


Instead of drawing 4x4 rectangles at the point where the mouse is, you should draw a line between the point where the mouse is now, and the previous location of the mouse.

You'll need another variable at the Form level to store the previous mouse location, say we call it PreviousLocation. Then in the mouse move event draw a line between the current location and PreviousLocation, and then update PreviousLocation with the current point for the next time mouse move fires.


This one promises....I am gonna develop the code and say if it works...

This post has been edited by darko01: 18 December 2009 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 19 December 2009 - 04:44 AM

I have problems....

Here is the code

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
	Dim shouldpaint As Boolean
	Dim Locations_X_1
	Dim Locations_Y_1
	Dim graphic As Graphics = CreateGraphics()
	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		Locations_X_1 = Cursor.Position.X
		Locations_Y_1 = Cursor.Position.Y
		shouldpaint = True
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		If shouldpaint = True Then
			graphic.DrawLine(Pens.Black, Locations_X_1, Locations_Y_1, e.X, e.Y)
		End If
	End Sub

	Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		shouldpaint = False
		Locations_X_1 = Cursor.Position.X
		Locations_Y_1 = Cursor.Position.Y
	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 19 December 2009 - 05:05 AM

OK, I have done it

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
	Dim shouldpaint As Boolean
	Dim Locations As PointF
	Dim graphic As Graphics = CreateGraphics()
	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		Locations = e.Location
		Label1.Text = Locations.ToString
		shouldpaint = True
	End Sub

	Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		If shouldpaint = True Then
			graphic.DrawLine(Pens.DarkBlue, Locations, Windows.Forms.Cursor.Position)
		End If
		shouldpaint = False
	End Sub
End Class



But, I am only able to draw in 300; 300 space, that`s the default size of the window, but I have enlarged it....how do I fix it ?

This post has been edited by darko01: 19 December 2009 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#8 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Drawing with mouse in VB

Posted 19 December 2009 - 06:47 AM

View Postdarko01, on 19 Dec, 2009 - 11:05 AM, said:

But, I am only able to draw in 300; 300 space, that`s the default size of the window, but I have enlarged it....how do I fix it ?


When you call CreateGraphics it ties the Graphics object to the surface of the Form at that particular instant in time. You have the call at the Form level. Code like that, will actually run at the start of the constructor. In the Form's case, this will occur before the call to InitializeComponent, which sets the Size of the form, so it gets the default form size - 300x300. The solution is to move the call to CreateGraphics so that it occurs after the form has initialized, you could put it into Form_Load.

Your code didn't quite work for me, I got a mess of lines. Here's a slightly tidier version, showing what I mean:

Public Class Form1

	Dim lastPoint As Point
	Dim graphic As Graphics
	Dim thickBlackPen As New Pen(Color.Black, 4.0F)

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		graphic = CreateGraphics()
		thickBlackPen.EndCap = Drawing2D.LineCap.Round
	End Sub

	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Set the initial location:
		lastPoint = e.Location
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		' If the left button is down...
		If e.Button = Windows.Forms.MouseButtons.Left Then
			' Draw between the last point, and the current mouse location.
			graphic.DrawLine(thickBlackPen, lastPoint, e.Location)
			' Make the last point equal the current point for next time.
			lastPoint = e.Location
		End If
	End Sub

End Class


Note though, there's still a problem. And it is a serious one - try drawing something and then minimizing and maximizing the form - all the drawing will disappear. Try drawing something and then opening Notepad, and moving the notepad window over the top of your form - all the drawing will disappear. The problem is that none of the drawing will persist when you use CreateGraphics.
http://www.bobpowell...ategraphics.htm

The solution is complicated - you have to draw on a bitmap, and draw the bitmap to the surface of the Form in the paint event. This event is triggered by windows when it realises that the surface of the control needs to be painted - as it does when you minimize/maximize or when another window passes over the top of it. Here's how you would do it, hopefully you can read it and see how it is done.

Public Class Form1

	Dim canvas As Bitmap		   ' The drawing is done onto this.
	Dim gfx As Graphics			  ' Draws on the bitmap called canvas.
	Dim lastPoint As Point
	Dim thickBlackPen As New Pen(Color.Black, 4.0F)


	Sub New()
		InitializeComponent()
		' You can set all of these properties in the IDE, I'm just doing it here so that you can copy and paste the code.
		Me.DoubleBuffered = True	 ' Reduces flicker. 
		Me.ClientSize = New Size(800, 600)
		Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D 
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		canvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
		gfx = Graphics.FromImage(canvas)
		thickBlackPen.EndCap = Drawing2D.LineCap.Round ' Looks better with an end cap.
	End Sub

	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Set the initial location:
		lastPoint = e.Location
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		' If the left button is down...
		If e.Button = Windows.Forms.MouseButtons.Left Then
			' Draw between the last point, and the current mouse location.
			gfx.DrawLine(thickBlackPen, lastPoint, e.Location)
			' Make the last point equal the current point for next time.
			lastPoint = e.Location
			Me.Refresh() ' trigger the paint event.
		End If
	End Sub

	Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
		' Draw the bitmap onto the surface of the form at (0,0).
		' DrawImageUnscaled is faster than DrawImage.
		e.Graphics.DrawImageUnscaled(canvas, Point.Empty)
	End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#9 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 19 December 2009 - 01:05 PM

View Postcrepitus, on 19 Dec, 2009 - 05:47 AM, said:

View Postdarko01, on 19 Dec, 2009 - 11:05 AM, said:

But, I am only able to draw in 300; 300 space, that`s the default size of the window, but I have enlarged it....how do I fix it ?


When you call CreateGraphics it ties the Graphics object to the surface of the Form at that particular instant in time. You have the call at the Form level. Code like that, will actually run at the start of the constructor. In the Form's case, this will occur before the call to InitializeComponent, which sets the Size of the form, so it gets the default form size - 300x300. The solution is to move the call to CreateGraphics so that it occurs after the form has initialized, you could put it into Form_Load.

Your code didn't quite work for me, I got a mess of lines. Here's a slightly tidier version, showing what I mean:

Public Class Form1

	Dim lastPoint As Point
	Dim graphic As Graphics
	Dim thickBlackPen As New Pen(Color.Black, 4.0F)

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		graphic = CreateGraphics()
		thickBlackPen.EndCap = Drawing2D.LineCap.Round
	End Sub

	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Set the initial location:
		lastPoint = e.Location
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		' If the left button is down...
		If e.Button = Windows.Forms.MouseButtons.Left Then
			' Draw between the last point, and the current mouse location.
			graphic.DrawLine(thickBlackPen, lastPoint, e.Location)
			' Make the last point equal the current point for next time.
			lastPoint = e.Location
		End If
	End Sub

End Class


Note though, there's still a problem. And it is a serious one - try drawing something and then minimizing and maximizing the form - all the drawing will disappear. Try drawing something and then opening Notepad, and moving the notepad window over the top of your form - all the drawing will disappear. The problem is that none of the drawing will persist when you use CreateGraphics.
http://www.bobpowell...ategraphics.htm

The solution is complicated - you have to draw on a bitmap, and draw the bitmap to the surface of the Form in the paint event. This event is triggered by windows when it realises that the surface of the control needs to be painted - as it does when you minimize/maximize or when another window passes over the top of it. Here's how you would do it, hopefully you can read it and see how it is done.

Public Class Form1

	Dim canvas As Bitmap		   ' The drawing is done onto this.
	Dim gfx As Graphics			  ' Draws on the bitmap called canvas.
	Dim lastPoint As Point
	Dim thickBlackPen As New Pen(Color.Black, 4.0F)


	Sub New()
		InitializeComponent()
		' You can set all of these properties in the IDE, I'm just doing it here so that you can copy and paste the code.
		Me.DoubleBuffered = True	 ' Reduces flicker. 
		Me.ClientSize = New Size(800, 600)
		Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D 
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		canvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
		gfx = Graphics.FromImage(canvas)
		thickBlackPen.EndCap = Drawing2D.LineCap.Round ' Looks better with an end cap.
	End Sub

	Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Set the initial location:
		lastPoint = e.Location
	End Sub

	Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		' If the left button is down...
		If e.Button = Windows.Forms.MouseButtons.Left Then
			' Draw between the last point, and the current mouse location.
			gfx.DrawLine(thickBlackPen, lastPoint, e.Location)
			' Make the last point equal the current point for next time.
			lastPoint = e.Location
			Me.Refresh() ' trigger the paint event.
		End If
	End Sub

	Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
		' Draw the bitmap onto the surface of the form at (0,0).
		' DrawImageUnscaled is faster than DrawImage.
		e.Graphics.DrawImageUnscaled(canvas, Point.Empty)
	End Sub

End Class



the code you gave me worked.....now...how can I do something that does the Pencil tool in MS Paint ?

If possible, just instruct me first, then I`ll try to develop a code...

This post has been edited by darko01: 19 December 2009 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Drawing with mouse in VB

Posted 20 December 2009 - 03:53 AM

View Postdarko01, on 19 Dec, 2009 - 07:05 PM, said:

how can I do something that does the Pencil tool in MS Paint ?


I'm not sure how the pencil differs from the brush. You would need to draw something when the mouse first goes down. Then set lastPoint. In MouseMove draw between lastPoint and e.Location, and then update lastPoint as before. If you want to change the width of the pencil, you can do that with the Pen constructor - I used a width of 4. I added the end caps because the line didn't look very good when it didn't use them...
Was This Post Helpful? 1
  • +
  • -

#11 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 20 December 2009 - 01:00 PM

Oh...thank you very much...

As you know, I am trying to develop something like M$ Paint....I`ll add some other features to the program like size & color changing....of course, if I have troubles, I`ll ask you....but first I`ll try to do it on my own....
Was This Post Helpful? 0
  • +
  • -

#12 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 21 December 2009 - 01:56 PM

OK, I am in trouble...

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
	Dim tooltype
	Dim pencolor
	Dim myPen As Pen = New Pen(Color.Black)
	Dim canvas As Bitmap		   ' The drawing is done onto this.
	Dim gfx As Graphics			  ' Draws on the bitmap called canvas.
	Dim lastPoint As Point
	Dim thickBlackPen As New Pen(Color.Black, 4.0F)
	' True while we are drawing the new line.
	Private m_Drawing As Boolean

	' Buffer for erasing rubberband lines.
	Private m_BufferBitmap As Bitmap
	Private m_BufferGraphics As Graphics

	' The mouse position.
	Private m_X1 As Integer
	Private m_Y1 As Integer
	Private m_X2 As Integer
	Private m_Y2 As Integer

	' Start drawing a rubberband line.
	Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Do nothing if this isn'tthe left mouse button.
		If e.Button <> MouseButtons.Left Then Exit Sub
		m_Drawing = True
		' Save a snapshot of the form.
		SaveSnapshot()

		' Save the current mouse position.
		m_X1 = e.X
		m_X2 = e.X
		m_Y1 = e.Y
		m_Y2 = e.Y
		lastPoint = e.Location
	End Sub

	Private Sub SaveSnapshot()
		If tooltype = "Line" Then
			Dim new_bitmap As Bitmap

			' Make a new bitmap that fits the form.
			new_bitmap = New Bitmap(Me.Size.Width, Me.Size.Height, Me.CreateGraphics())
			m_BufferGraphics = Graphics.FromImage(new_bitmap)

			' Clear the new bitmap.
			m_BufferGraphics.Clear(Me.BackColor)

			' Copy the existing bitmap's contents into
			' the new bitmap.
			If Not (m_BufferBitmap Is Nothing) Then
				m_BufferGraphics.DrawImage(m_BufferBitmap, 0, 0)
			End If

			' Save the new bitmap and graphics objects.
			m_BufferBitmap = new_bitmap
		End If
	End Sub

	' Continue drawing the rubberband line.
	Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		' Do nothing if we're not drawing.
		If Not m_Drawing Then Exit Sub

		' Save the new point.
		m_X2 = e.X
		m_Y2 = e.Y

		' Erase the previous line.
		DrawForm(Me.CreateGraphics())

		' Draw the new line directly on the form.

		Select Case tooltype
			Case "Line"
				Me.CreateGraphics().DrawLine( _
myPen, m_X1, m_Y1, m_X2, m_Y2)
			Case "Pencil"
				If e.Button = Windows.Forms.MouseButtons.Left Then
					' Draw between the last point, and the current mouse location.
					gfx.DrawLine(myPen, lastPoint, e.Location)
					' Make the last point equal the current point for next time.
					lastPoint = e.Location
					Me.Refresh() ' trigger the paint event.
				End If
		End Select
	End Sub

	' Redraw the saved buffer.
	Private Sub DrawForm(ByVal gr As Graphics)
		If Not (m_BufferBitmap Is Nothing) Then gr.DrawImage(m_BufferBitmap, 0, 0)
	End Sub

	' Finish drawing the new line.
	Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		' Do nothing if we're not drawing.
		If Not m_Drawing Then Exit Sub
		m_Drawing = False

		' Save the new point.
		m_X2 = e.X
		m_Y2 = e.Y

		' Draw the new line permanently on the buffer.

		Select Case tooltype
			Case "Line"
				m_BufferGraphics.DrawLine( _
myPen, m_X1, m_Y1, m_X2, m_Y2)
			Case "Pencil"
				If e.Button = Windows.Forms.MouseButtons.Left Then
					' Draw between the last point, and the current mouse location.
					gfx.DrawLine(myPen, lastPoint, e.Location)
					' Make the last point equal the current point for next time.
					lastPoint = e.Location
					Me.Refresh() ' trigger the paint event.
				End If
		End Select
		' Redraw to show the new line.
		DrawForm(Me.CreateGraphics())
	End Sub

	' Redraw the form.
	Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
		If tooltype = "Line" Then
			DrawForm(e.Graphics)
		ElseIf tooltype = "Pencil" Then
			e.Graphics.DrawImageUnscaled(canvas, Point.Empty)
		End If
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		myPen = Pens.Black
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		myPen = Pens.Blue
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		myPen = Pens.Red
	End Sub

	Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.DoubleBuffered = True
		myPen = Pens.Black
		tooltype = "Pencil"
		If tooltype = "Pencil" Then
			canvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
			gfx = Graphics.FromImage(canvas)
			thickBlackPen.EndCap = Drawing2D.LineCap.Round ' Looks better with an end cap.
		End If
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		tooltype = "Line"
	End Sub

	Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
		tooltype = "Pencil"
	End Sub
End Class



The line is OK, but the pencil is not OK....check it out and please tell me how to fix the "bugs"...

P.S...You`ll see what`s the problem with the Pencil, no need to explain, right ?


You will need to add 5 buttons for:

blue color
red color
black color
draw line
draw *like pencil*

This post has been edited by darko01: 21 December 2009 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#13 darko01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-September 09

Re: Drawing with mouse in VB

Posted 22 December 2009 - 12:58 PM

Seems the problem is in the following line:

m_BufferGraphics.Clear(Me.BackColor)



...when I remove it, the pencil is OK, but the line is not ( a mess of lines ) ....when I add it, the line is OK, but the pencil drawing disappears...can you help me ?

B)
Was This Post Helpful? 0
  • +
  • -

#14 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Drawing with mouse in VB

Posted 22 December 2009 - 03:15 PM

You're using CreateGraphics again. I guess it is forgivable in this situation as you actually want temporary graphcis...
Never use CreateGraphics (except in particular circumstances such as when you are measuring Strings).

You can use ControlPaint.DrawReversibleLine to draw lines that you can erase, but I've always found that it doesn't work very well.

One way to draw temporary lines would be to use instance variables for the two points of the line. You would set the first point when the mouse goes down, and the second point when the mouse moves, then call Refresh to trigger the paint event. In the paint event you would draw canvas, and then draw the line (when it is appropriate) on top of it:


Private tempLineStart As Point
Private tempLineEnd As Point

Sub Paint(...)

	e.Graphics.DrawImageUnscaled(canvas....)
	If (drawTempLines = True) Then e.Graphics.DrawLine(pen1, tempLineStart, tempLineEnd)

End Sub


When the end point changes because the mouse moved, paint will be triggered, the old line will be erased by the DrawImageUnscaled call (it will draw over the top). Then, even later, when you mouse-up, you would probably want to draw the final line onto the canvas and call Refresh, which would again erase the last temporary line.
Was This Post Helpful? 0
  • +
  • -

#15 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Drawing with mouse in VB

Posted 22 December 2009 - 03:27 PM

Yes, definitely don't use ControlPaint.DrawReversibleLine, it can make a mess of the whole screen...

Public Class Form1

	Private Shared rand As New Random
	Private WithEvents t As New Timer With {.Interval = 50, .Enabled = True}
	Private lines As New List(Of Line)

	Private Class Line

		Public PointA As Point
		Public PointB As Point
		Public Color As Color

		Public Shared Function GetRandomLine() As Line
			Return New Line With {.Color = RandomColor(), .PointA = RandomScreenPoint(), .PointB = RandomScreenPoint()}
		End Function

		Private Shared Function RandomScreenPoint() As Point
			Return New Point(rand.Next(Screen.PrimaryScreen.WorkingArea.Width), rand.Next(Screen.PrimaryScreen.WorkingArea.Height))
		End Function

		Private Shared Function RandomColor() As Color
			Return Color.FromArgb(rand.Next Or &HFF000000)
		End Function

	End Class

	Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
		' Get a new line '
		Dim line As Line = line.GetRandomLine
		' Draw it on the screen '
		ControlPaint.DrawReversibleLine(line.PointA, line.PointB, line.Color)
		' Add it to the list '
		lines.Add(line)
		' If we have 10 lines then get the 0th one '
		If lines.Count > 9 Then
			line = lines(0)
			lines.RemoveAt(0)
			' Draw it again to erase '
			ControlPaint.DrawReversibleLine(line.PointA, line.PointB, line.Color)
		End If
	End Sub

End Class


Try moving the form, you can move the lines, and so they don't get erased.

This post has been edited by crepitus: 22 December 2009 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2