For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > December 2005 > Draw a line on Picture box









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Draw a line on Picture box
refaul

2005-12-12, 6:23 pm

I have the following code for drawing a line on form......can any one help me for drawing a line on picture box using the same following code.for information I faced the problem because Picture box has no Load options....
visual basic code:---------------------------------------------------general decleration
Dim cx1, cx2, cy1, cy2, m1, m2 As Integer
Dim line1 As Boolean
Private Sub Form_Load()
DrawWidth = 1 ' Use wider brush.

ForeColor = RGB(255, 0, 0) ' Set drawing color.
Refresh

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Screen.MousePointer = vbCrosshair 'Changing the mouse pointer to Crosshair
line1 = True ' Start Drawing the line
cx1 = X ' Set to X1 Co-ordinate point where 'Userpoints
cy1 = Y ' Set to Y1 Co-ordinate point where User 'points
PSet (cx1, cy1)
' Line -(cx1, cy1) ' Draw the starting point of the line
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If line1 = True Then
Refresh
cx2 = X
cy2 = Y
m1 = (cx1 + cx1) / 2
m2 = (cy1 + cy2) / 2
Line (cx1, cy1)-(m1, m2)
'Line (m1, m2)-(m1, Y)
Line (m1, m2)-(X, m2)
Line (X, m2)-(X, Y)
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
line1 = False
End Sub
Randy Birch

2005-12-12, 9:55 pm

The picture controls supports pset and line.. just have to reference the
control in the drawing code:

Option Explicit

Private cx1 As Integer, cx2 As Integer
Private cy1 As Integer, cy2 As Integer
Private m1 As Integer, m2 As Integer
Private line1 As Boolean

Private Sub Form_Load()

With Picture1
.DrawWidth = 1
.ForeColor = RGB(255, 0, 0)
.Refresh
End With

End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

Screen.MousePointer = vbCrosshair
line1 = True
cx1 = X
cy1 = Y
Picture1.PSet (cx1, cy1)

End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)

If line1 = True Then

Picture1.Refresh

cx2 = X
cy2 = Y

m1 = (cx1 + cx1) / 2
m2 = (cy1 + cy2) / 2

Picture1.Line (cx1, cy1)-(m1, m2)
Picture1.Line (m1, m2)-(X, m2)
Picture1.Line (X, m2)-(X, Y)

End If

End If

End Sub


Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)

line1 = False
Screen.MousePointer = vbDefault

End Sub


--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------



"refaul" <refaul.1zye9o@mail.codecomments.com> wrote in message
news:refaul.1zye9o@mail.codecomments.com...
:
: I have the following code for drawing a line on form......can any one
: help me for drawing a line on picture box using the same following
: code.for information I faced the problem because Picture box has no
: Load options....
: visual basic
: code:---------------------------------------------------general
: decleration
: Dim cx1, cx2, cy1, cy2, m1, m2 As Integer
: Dim line1 As Boolean
: Private Sub Form_Load()
: DrawWidth = 1 ' Use wider brush.
:
: ForeColor = RGB(255, 0, 0) ' Set drawing color.
: Refresh
:
: End Sub
:
: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
: Single, Y As Single)
: Screen.MousePointer = vbCrosshair 'Changing the mouse pointer to
: Crosshair
: line1 = True ' Start Drawing the line
: cx1 = X ' Set to X1 Co-ordinate point
: where 'Userpoints
: cy1 = Y ' Set to Y1 Co-ordinate point
: where User 'points
: PSet (cx1, cy1)
: ' Line -(cx1, cy1) ' Draw the starting point of the
: line
: End Sub
:
: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
: Single, Y As Single)
: If line1 = True Then
: Refresh
: cx2 = X
: cy2 = Y
: m1 = (cx1 + cx1) / 2
: m2 = (cy1 + cy2) / 2
: Line (cx1, cy1)-(m1, m2)
: 'Line (m1, m2)-(m1, Y)
: Line (m1, m2)-(X, m2)
: Line (X, m2)-(X, Y)
: End If
: End Sub
: Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As
: Single, Y As Single)
: line1 = False
: End Sub
:
:
:
: --
: refaul
: ------------------------------------------------------------------------
: Posted via http://www.codecomments.com
: ------------------------------------------------------------------------
:

MikeD

2005-12-12, 9:55 pm

Comments inline

"refaul" <refaul.1zye9o@mail.codecomments.com> wrote in message
news:refaul.1zye9o@mail.codecomments.com...
>
> I have the following code for drawing a line on form......can any one
> help me for drawing a line on picture box using the same following
> code.for information I faced the problem because Picture box has no
> Load options....
> visual basic
> code:---------------------------------------------------general
> decleration
> Dim cx1, cx2, cy1, cy2, m1, m2 As Integer


Do you realize all those variables except the last one (m2) are of type
Variant? Most likely, you intended them all to be of Integer type. If you're
going to declare multiple variables on one Dim line, you need to specify the
type for each variable. For example:

Dim cx1 As Integer, cx2 As Integer, cy1 As Integer....


> Dim line1 As Boolean
> Private Sub Form_Load()
> DrawWidth = 1 ' Use wider brush.
>
> ForeColor = RGB(255, 0, 0) ' Set drawing color.
> Refresh
>
> End Sub
>
> Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
> Single, Y As Single)
> Screen.MousePointer = vbCrosshair 'Changing the mouse pointer to
> Crosshair
> line1 = True ' Start Drawing the line
> cx1 = X ' Set to X1 Co-ordinate point
> where 'Userpoints
> cy1 = Y ' Set to Y1 Co-ordinate point
> where User 'points
> PSet (cx1, cy1)
> ' Line -(cx1, cy1) ' Draw the starting point of the
> line
> End Sub
>
> Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
> Single, Y As Single)
> If line1 = True Then
> Refresh
> cx2 = X
> cy2 = Y
> m1 = (cx1 + cx1) / 2
> m2 = (cy1 + cy2) / 2
> Line (cx1, cy1)-(m1, m2)
> 'Line (m1, m2)-(m1, Y)
> Line (m1, m2)-(X, m2)
> Line (X, m2)-(X, Y)
> End If
> End Sub
> Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As
> Single, Y As Single)
> line1 = False
> End Sub
>


You need to reset the mouse pointer in MouseUp. It's staying as a crosshair.

The code should work fine in a PictureBox, but you need to move the code to
the corresponding PictureBox events and you need to reference the
PictureBox. All of your Line statements, since they're not qualified with an
object name, are for the form.

--
Mike
Microsoft MVP Visual Basic


refaul

2005-12-13, 10:17 pm

Thank you...I did it according to your CODE and finally I got its working.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com