Drawline help

Hello.

I have a canvas, canvas1, in a window and I have these properties…
MouseXDown As Integer
MouseYDown As Integer
MouseXUp As Integer
MouseYUp As Integer

In the paint event of Canvas1 I have this…
Sub Paint(g As Graphics, areas() As REALbasic.Rect)
g.ForeColor=RGB(255,0,0)
g.PenHeight = 5
g.PenWidth = 5
g.Drawline(MouseXDown, MouseYDown, MouseXUp, MouseYUp)
Me.Invalidate
End Sub

When the app launch. if I click the mouse and then release it after dragging it I was expecting a red line to be displayed from MouseDown to MouseUp but I do not see that happening.

When the app launches, i see a 5x5 points red-filled circle.

What am I doing wrong?

Thanks.

Lennox

You did return true in the mouse down event handler?

Thanks Wayne,
I didn’t.
Lennox

That’s because you haven’t dragged yet, so all the X/Y values are zero.

Not related to your problem, but don’t call me.Invalidate from within Paint.

MOUSE DOWN EVENT

MouseXDown=x
MouseYDown=y
return true

MOUSEUP EVENT

MouseXUp=x
MouseYUp=y
canvas1.invalidate

CANVAS PAINT EVENT

g.ForeColor=RGB(255,0,0)
g.PenHeight = 5
g.PenWidth = 5
g.Drawline(MouseXDown, MouseYDown, MouseXUp, MouseYUp)

Thanks Dave,
Works Great.

If I draw a second line the first one is deleted though. Why is that?

Thanks again.

Lennox

Because the canvas image is not a picture… it dras completely in the paint event, and you only have these 4 variables.

If you want the lines to persist, you need an actual picture the same size as the canvas.
Draw on that.

In the Canvas paint event, draw the picture to the canvas graphics instead of drawing lines on it.
Or create an array of object2d objects to record each new set of points, and draw them all in the canvas paint event

Because a Canvas is non-persistent… Only what happens in the PAINT event will stay…

If you want to make a “drawing” program, you should draw on a PICTURE object instead, and change the CANVAS Paint event to simply

g.drawpicture myPictureBuffer,0,0

I’m not sure, but there may be a simple scribble example program in the Xojo Example folder

Thanks Jeff and Dave,

What i am really trying to do is to make is a “screen marker” app, where the window is transparent and the user draws freehand lines to highlight a particular thing.

I know how to do the invisible window and invisible canvas.

Any suggestions.

Thanks,

Lennox

1/ Grab the screen into a picture
2/Open your window full screen (search for Kiosk… may be useful)
3/In the paint event, paint the picture.
4/When the mouse is used to draw, draw on the picture
5/When the canvas paints, draw the amended picture

If you want to be able to undo the line, store the picture AND an amended copy so that you can take parts from the original grab and reset that part of the screen

Thanks Jeff.
Lennox