I am writing a rudimentary drawing app for my own amusement. The app uses a canvas control on a window .
The user clicks on a button representing a set of primitives. This sets a variable to either ‘line’ , ‘rectangle’,’ circle’ and so on, so the app knows what to draw.
here is the code for the pushbutton action event.
dmode="line"// Dmode is the drawing mode
Once the mouse pointer begins moving on the canvas area it will not start drawing the shape until the mousedown event is triggered. mousedown code as follows;
PenDown=true// allow the canvas to draw
spx=x// starting point for x
spy=y// starting point for y
return true// recognise the mouse up event
code for the mouse move event
if PenDown=true then// PenDown must be testing false because this block does not process
epx=x// end point for x
epy=y// end point for y
self.Invalidate// fire the paint event
end
when the user has drawn the desired shape and the mouse up event is fired, mouse movement on the canvas no longer draws anything.
mouse up event
PenDown=false// stop drawing
The problem that I am having is that an if then statement is not testing the boolean variable ‘PenDown’ or 'PenDown is testing false. If it is false, I have no clue why because there is nothing between mousedown, mouse move and mouse up that could set it to false. To add to my confusion, the PenDown value does not even appear in the snapshot values if I add a breakpoint.
The PenDown variable is a property of the form the canvas is on, so I know that the scope aspect is ok. I have also tested PenDown
in the mousedown event and it tests true, so I know that it is set in readiness of the mouse move event.
the only two things I am missing in your code are: the “end if” clause and your missing spaces between code and remarks
if PenDown=true then // PenDown must be Testung false because this block does not process
epx=x // end point for x
epy=y // end point for y
self.Invalidate // fire the paint event end if
but without project file it’s hard to say something
Hi Tomas, I shouldn’t need “EndIf” should I? Surely ‘End’ should suffice or else the compiler would raise an error .The remarks were added for clarity on the forum. They don’t actually appear in my code.
Windows or Mac?
I ask because some years ago I had a problem where the mousemove wasnt firing AT ALL under Windows, and I had to subclass the window to catch WM_MOVE events from windows API
So: if you add x = 2 before the IF statement, does the debugger hit that line?
Michel,
I know that it draws because I have already done what you suggested. Never posted a project on a forum before. How do I do that?
Its a Mac project. BTW Jeff, tried your suggestion a bottle of jack Daniels ago.
[quote=135283:@chris benton]Michel,
I know that it draws because I have already done what you suggested. Never posted a project on a forum before. How do I do that?.[/quote]
Put the project file onto your dropbox or on a web site, and simply post the URL to it…
When you return true from MouseDown subsequent events go to MouseDrag, not MouseMove, then MouseUp ends the event series. That’s why that code doesn’t fire, it’s going to MouseDrag.
Maybe you can bypass that effect by not relying on the mouse up event of the canvas (what happens if the user leaves the canvas while drawing?), but draw as long as system.mousedown is true?
Will, Ulrich and Jeff. Indeed you were all correct in your suggestions. The mousedrag is the event to use for this solution.
All seems to be working as planned now. Just another trap for new users of Xojo.