Mouse Position

Just getting started and trying to rapidly learn things. I’m not new to programming, but confused by some things that seem they should be simple and accessible that I’m having to dive for.

I’m trying to build sort of an inventory type system - I have a canvas, and I’ve been able to draw a semi-transparent grid. I’ve been able to adjust the transparency and color of the grid. Awesome.

Now, I just want to draw a selection rectangle around the current cell on the 32x32px grid, which would be wherever the mouse is placed.

I’ve tried individual integers and Point for the trackingPosition x/y. I tried adding a mouse move event in my canvas and updating tracking position there, then in the paint event, draw the rectangle based on the tracking variables. They are always zero.

I guess my issue is something simple - how do I detect the mouse position relative to the canvas to populate those trackingPosition variables so that I can draw a selection rectangle under the mouse? I feel like I’m missing something incredibly elementary here.

Thanks.

If I understand the problem correctly, you want to create a selection rectangle. So you need start (StartX, StartY As Integer) and end (EndX, EndY) co-ordinates at least at window level

Create a canvas inb your window

In the MouseDown event:

StartX = x
StartY = y
EndX = x
EndY = y
Return True

In the MouseDrag event:

EndX = x
EndY = y
Me.Refresh 

In the Paint Event:

g.FillRectangle(StartX, StartY, EndX - StartX, EndY - StartY)

For completeness, In MouseUp event:

EndX = x
EndY = y

'And you have everything you need to continue with the selection logic

If you only want to select with the left mouse click, just use the IsContextualClick in the MouseDown event:

If Not IsContextualClick Then
  StartX = x
  StartY = y
  EndX = x
  EndY = y
  Return True
End If

I’m generally starting even simpler than that. I just eventually want to draw a rectangle at the 32x32px grid cell over which the mouse hovers as a starting point. Right now, I’m just trying to draw a 32x32 pixel rectangle at the mouse position on the canvas, sort of following if my mouse is in the canvas, and I’ll do the math to snap to normalized position once I get anything happening. But right now, nothing draws.

What event can I use to be checking if the mouse is with in the canvas like this, and get its position which I can then pass to the Paint event?

Thanks so much for the quick replies!

Ah ok, then all you need is a single coordinate MouseX, MouseY As Integer and the MouseMove event of the canvas

MouseX = x
MouseY = y
Me.Refresh 

Obviously the names of the variables I chose them at random

You know, that’s the first thing I tried. I wonder if I’d never put the Refresh. Anyhow, now I have it working as expected. Thanks!

The usual reason, at least for people new to Xojo, is Scope. People typically declare local variables in their event code and set them. Those variables go out of scope and are destroyed as soon as the event finishes. You need to add properties to the window and use those.

Another common error is to have properties on the window, but then define variables with the same name in the event code. In that case, since the local variables are closer in scope than the window properties, the local variables are used by the event code, leaving the window properties unchanged.

Just a wild guess based on your description of the problem.