LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How would one make the cursor snap to a grid on a canvas?

How would one make the cursor snap to a grid on a canvas? Also would there be a way to click once and then have a straight line follow the cursor as it is snapping to the grid, until the user clicks again and then the line will be drawn--schematic editor style.

Thanks,
Mark
0 Kudos
Message 1 of 2
(2,995 Views)
Hi Mark,

Your best option would be to program this functionality yourself using a Canvas and Timer control. Set the timer to fire very frequently (i.e, .01 seconds) and in the callback you can use GetRelativeMouseState to obtain the coordinates. Then depending on how you want the snap to work you just use CanvasDrawLineTo to draw the line. To mark your left click as a "pen down" event, it'll be best to use some global flag to say the pen is down and to store its coordinates as some global variable. Then, as the timer ticks you will see a new line formed. One thing to watch out for is that this will probably be writing all the time, so to get it to be like a temporary line until you left-click to end the line, you may have to erase the existing line first and t
hen redraw it with the new ending coordinates. This should make it look like a temporary "floating" line. As for picking the coordinates to make it snap, you'll have to do some arithmetic and rounding to make it snap to every nth pixel or so. Hope this helps!

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 2 of 2
(2,995 Views)