4 Replies - 4485 Views - Last Post: 16 July 2008 - 08:38 AM Rate Topic: -----

#1 ashura517  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 11-March 08

Rubberband rectangle drawing doesnt work!

Post icon  Posted 16 July 2008 - 01:27 AM

Hi

I am trying to draw rubber band rectangles in VB.NET. The code somehow works but it doesnt draw out the rectangle after the dashed lines are drawn when i pressed left mouse button and drag. It disappear when i released the mouse button and no rectangles are drawn. Here's the code:

Dim bHaveMouse As Boolean
	Dim ptOriginal As Point
	Dim ptLast As Point

	' Convert and Normalize the points and draw the reversible frame.
	Private Sub MyDrawReversibleRectangle(ByVal p1 As Point, ByVal p2 As Point)
		Dim rc As Rectangle
		' Convert the points to screen coordinates.
		p1 = PointToScreen(p1)
		p2 = PointToScreen(p2)
		' Normalize the rectangle.
		If (p1.X < p2.X) Then
			rc.X = p1.X
			rc.Width = p2.X - p1.X
		Else
			rc.X = p2.X
			rc.Width = p1.X - p2.X
		End If
		If (p1.Y < p2.Y) Then
			rc.Y = p1.Y
			rc.Height = p2.Y - p1.Y
		Else
			rc.Y = p2.Y
			rc.Height = p1.Y - p2.Y
		End If
		' Draw the reversible frame.
		ControlPaint.DrawReversibleFrame(rc, Color.Red, FrameStyle.Dashed)
	End Sub

	' Called when the left mouse button is pressed down.
	Public Sub MyMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
		' Make a note that we "have the mouse".
		bHaveMouse = True
		' Store the "starting point" for this rubber-band rectangle.
		ptOriginal.X = e.X
		ptOriginal.Y = e.Y
		' Special value lets us know that no previous
		' rectangle needs to be erased.
		ptLast.X = -1
		ptLast.Y = -1
	End Sub

	' called when the left mouse button is released
	Public Sub MyMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		' Set internal flag to know we no longer "have the mouse".
		bHaveMouse = False
		' If we have drawn previously, draw again in that spot
		' to remove the lines.
		If (ptLast.X <> -1) Then
			Dim ptCurrent As Point
			ptCurrent.X = e.X
			ptCurrent.Y = e.Y
			MyDrawReversibleRectangle(ptOriginal, ptLast)
		End If
		' Set flags to know that there is no "previous" line to reverse.
		ptLast.X = -1
		ptLast.Y = -1
		ptOriginal.X = -1
		ptOriginal.Y = -1
	End Sub

	' Called when the mouse is moved.
	Public Sub MyMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
		Dim ptCurrent As Point
		ptCurrent.X = e.X
		ptCurrent.Y = e.Y
		' If we "have the mouse", then we draw our lines.
		If (bHaveMouse) Then
			' If we have drawn previously, draw again in
			' that spot to remove the lines.
			If (ptLast.X <> -1) Then
				MyDrawReversibleRectangle(ptOriginal, ptLast)
			End If
			' Update last point.
			ptLast = ptCurrent
			' Draw new lines.
			MyDrawReversibleRectangle(ptOriginal, ptCurrent)
		End If
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		bHaveMouse = False
	End Sub


Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Rubberband rectangle drawing doesnt work!

#2 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Rubberband rectangle drawing doesnt work!

Posted 16 July 2008 - 04:02 AM

Comment out the lines in red.
	' called when the left mouse button is released
	Public Sub MyMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
		' Set internal flag to know we no longer "have the mouse".
		bHaveMouse = False
		' If we have drawn previously, draw again in that spot
		' to remove the lines.
 [color=#CC0000]	   'If (ptLast.X <> -1) Then
		'	Dim ptCurrent As Point
		'	ptCurrent.X = e.X
		'	ptCurrent.Y = e.Y
		'	MyDrawReversibleRectangle(ptOriginal, ptLast)
		'End If
  [/color]	   ' Set flags to know that there is no "previous" line to reverse.
		ptLast.X = -1
		ptLast.Y = -1
		ptOriginal.X = -1
		ptOriginal.Y = -1
	End Sub


Well red didn't show but up where the color is defined.

This post has been edited by WayneSpangler: 16 July 2008 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 ashura517  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 11-March 08

Re: Rubberband rectangle drawing doesnt work!

Posted 16 July 2008 - 05:21 AM

Thanks WayneSprangler. I edited the code..this time the dotted lines did not disappear. However, how do i make the rectangle dotted lines transform to solid lines once i release the left mouse botton? U know what i mean? <_<
Was This Post Helpful? 0
  • +
  • -

#4 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Rubberband rectangle drawing doesnt work!

Posted 16 July 2008 - 07:01 AM

Add a boolean called dashed to your MyDrawReversibleRectangle subroutine.
	Private Sub MyDrawReversibleRectangle(ByVal p1 As Point, ByVal p2 As Point, ByVal dashed As Boolean)


Now when you draw the rectangle use code like this.
		' Draw the reversible frame.
		If dashed Then
			ControlPaint.DrawReversibleFrame(rc, Color.Red, FrameStyle.Dashed)
		Else
			ControlPaint.DrawReversibleFrame(rc, Color.Black, FrameStyle.Thick)
		End If


and change your calls:
In MouseUp
MyDrawReversibleRectangle(ptOriginal, ptLast, False)


In MouseMove
				MyDrawReversibleRectangle(ptOriginal, ptLast, True)
			MyDrawReversibleRectangle(ptOriginal, ptCurrent,true)


Was This Post Helpful? 0
  • +
  • -

#5 ashura517  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 11-March 08

Re: Rubberband rectangle drawing doesnt work!

Posted 16 July 2008 - 08:38 AM

Many thanks! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1