Canvas color change on mouse exit not happening

I have a number of DesktopCanvas controls acting as buttons. I indicate “button pressed” by changing colors. When users mouse down but then drag the cursor off the control before releasing, that cancels the “button press”. This works fine, but I need the button’s color to change back to “unpressed” as soon as the cursor exits the control, and it just isn’t working, only changing to “unpressed” upon releasing the mouse button. The below code uses timers for a couple different purposes, but the color change also doesn’t happen for controls that don’t use a timer.

Sub Paint(g As Graphics, areas() As Rect)
  If (Me.Name = MouseOverXY) And Not(EStop_Pressed) Then
    g.DrawingColor = Color.Blue
  Else
    g.DrawingColor = Color.White
  End If
  g.FillRoundRectangle(0, 0, g.Width, g.Height, 10, 10)

  If (Me.Name = MouseDownXY) And Not(EStop_Pressed) Then
    g.DrawingColor = Color.Green
  Else
    g.DrawingColor = Color.Orange
  End If
  g.FillRoundRectangle(3, 3, g.Width - 6, g.Height - 6, 10, 10)

  g.DrawingColor = Color.Black
  g.DrawRoundRectangle(0, 0, g.Width, g.Height, 10, 10)
  g.DrawRoundRectangle(3, 3, g.Width - 6, g.Height - 6, 10, 10)
  g.FontSize = 28
  g.DrawText("X+", 5, 32)
End Sub

Sub MouseEnter()
  If Not(EStop_Pressed) Then
    MouseOverXY = Me.Name
    Me.Refresh
  End If
End Sub

Sub MouseExit()
  'code goes here to stop movement
  MouseOverXY = ""
  Me.Refresh
End Sub

Function MouseDown(x As Integer, y As Integer) As Boolean
  If Not(EStop_Pressed) Then
    XSpeed_Going = ""
    MouseDownXY = Me.Name
    bump = True
    TimerBump.RunMode = Timer.RunModes.Single
    SimStep = 1
    TimerSim.Period = Slider_Jog_XSpeed.Value
    TimerSim.RunMode = Timer.RunModes.Multiple
    Me.Refresh
  End If
  Return True
End Function

Sub MouseUp(x As Integer, y As Integer)
  MouseDownXY = ""
  TimerBump.RunMode = Timer.RunModes.Off
  TimerSim.RunMode = Timer.RunModes.Off
  bump = False
  Me.Refresh
End Sub

Sub DragExit(obj As DragItem, action As DragItem.Types)
  MouseDownXY = ""
  MouseOverXY = ""
  Me.Refresh
  TimerBump.RunMode = Timer.RunModes.Off
End Sub

Function DragEnter(obj As DragItem, action As DragItem.Types) As Boolean
  MouseDownXY = Me.Name
  MouseOverXY = Me.Name
  Me.Refresh
  TimerBump.RunMode = Timer.RunModes.Off
End Function

Hi Erik,

I think you want to change your MouseUp event to include the x and y parameters to check for when the mouse is released, whether the mouse is still over top of your canvas. Something like the following:

Sub MouseUp(x As Integer, y As Integer)

  If x >= 0 And x <= Self.Width And y >= 0 And y <= Self.Height Then
    // if the mouse is within the Canvas area
    // - then it is a proper click
    MouseDownXY = ""
  End If

  TimerBump.RunMode = Timer.RunModes.Off
  TimerSim.RunMode = Timer.RunModes.Off
  bump = False
  Me.Refresh
End Sub

I’m attaching an old example I made of a subclassed DesktopCanvas button. It doesn’t use the same properties as your example, but hopefully it helps to give you an idea of what I mean.

CanvasFocusRingExample.xojo_binary_project.zip (8.4 KB)

Edited to add: Use the Debug Message panel to verify whether the ButtonPressed event was called, or not.

I think because the MouseDown event is active while the mouse is moving, the MouseExit event cannot also fire.

I appreciate that, but what I need is for the color of the canvas to revert to the “unclicked” color the instant the mouse is no longer over the control, regardless of the state of the mouse button. (Also, I read up on the “Drag” functions; I misunderstood those and they are no longer in my project.)

Any suggestions?

Not 100% what you are looking for or what doesn’t work.

Here is a test I came up with assuming I understood what you are looking for - a button press is released if the mouse is dragged off while the button is still down:

button.zip (4.6 KB)

Hope that helps.

Playing with this a bit more, here is a more complete example:

button.zip (5.4 KB)

1 Like

Perfect! Thank you very much, Indy G, it works perfectly.

And thank you also for helping, Scott C.

2 Likes