Skip to main content

Microsoft Silverlight

Answered Question mouse leaves object while movingRSS Feed

(0)

v_georgiev
v_georgiev

Member

Member

4 points

13 Posts

mouse leaves object while moving

I made my object moving when my mouse downs on it, but when I move the mouse faster it leaves the object and I have to go back to pick it up. Why is that? Here' s the code:

XAML:

1    <UserControl
2    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4    	x:Class="HideShowTest.MainPage"
5    	Width="640" Height="480">
6    
7    	<Canvas x:Name="LayoutRoot" Background="#FF636363">
8    		<Ellipse Fill="#FF4BC18E" Height="87" Width="87" Canvas.Left="88" Canvas.Top="300" MouseLeftButtonDown="down" MouseMove="move"/>
9    	</Canvas>
10   </UserControl>

C#:

1    bool isMouseCaptured = false;
2    double mouseVerticalPosition;
3    double mouseHorizontalPosition;
4    
5    ...
6    
7    private void down(object sender, MouseButtonEventArgs e)
8    {
9        FrameworkElement item = sender as FrameworkElement;
10   
11       if (isMouseCaptured == false)
12       {
13           mouseVerticalPosition = e.GetPosition(null).Y;
14           mouseHorizontalPosition = e.GetPosition(null).X;
15           isMouseCaptured = true;
16           item.CaptureMouse();
17       }
18       else
19       {
20           isMouseCaptured = false;
21       }
22    }
23   
24   private void move(object sender, MouseEventArgs e)
25   {
26       FrameworkElement item = sender as FrameworkElement;
27   
28       if (isMouseCaptured == true)
29       {
30           double deltaV = e.GetPosition(null).Y - mouseVerticalPosition;
31           double deltaH = e.GetPosition(null).X - mouseHorizontalPosition;
32           double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
33           double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
34   
35           item.SetValue(Canvas.TopProperty, newTop);
36           item.SetValue(Canvas.LeftProperty, newLeft);
37   
38           mouseVerticalPosition = e.GetPosition(null).Y;
39           mouseHorizontalPosition = e.GetPosition(null).X;
40       }
41   }

Thanks!

agaac
agaac

Member

Member

20 points

10 Posts

Re: mouse leaves object while moving

I can see you're using CaptureMouse but I cannot see you releasing it anywhere. CaptureMouse also returns a bool value if it fails - you should check if the mouse is really captured.

Another thing: your application is not full screen, only 640x480, so if you move mouse quickly, you may move the mouse pointer outside the application area. In this case it will no longer receive the mouse move event and the dragged element will drop at the edge. You should use the LostMouseCapture event to detect this situation.

FuryDiamond
FuryDiamond

Contributor

Contributor

3870 points

766 Posts

Re: mouse leaves object while moving

Did you add a MouseLeave and MouseLeftButton up events to release the mouse controls?

XAML:  

<Canvas x:Name="LayoutRoot" Background="#FF636363">
    <Ellipse Fill="#FF4BC18E" Height="87" Width="87" Canvas.Left="88" Canvas.Top="300" MouseLeftButtonDown="down" MouseLeftButtonUp="up" MouseMove="move" MouseLeave="leave"/>
</Canvas>
 

C#:

 

private void up(object sender, MouseButtonEventArgs e)
{
    FrameworkElement item = sender as FrameworkElement;
    item.ReleaseMouseCapture();

    isMouseCaptured = false;
}

private void leave(object sender, MouseEventArgs e)
{
    FrameworkElement item = sender as FrameworkElement;
    item.ReleaseMouseCapture();

    isMouseCaptured = false;
}
 

Please "Mark as Answer" if this post answered your question. :)

v_georgiev
v_georgiev

Member

Member

4 points

13 Posts

Re: mouse leaves object while moving

But I want to release the object when I down again, not when up.

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3376 points

377 Posts

Answered Question

Re: mouse leaves object while moving

Hi,

   I am afraid there are some limitations about CaptureMouse method. Ouoted from MSDN Document:

The mouse can be captured when all of the following conditions are true:

        The mouse pointer is over the Silverlight plug-in content area.

        No other Silverlight object has captured the mouse.

        No other non-Silverlight object has captured the mouse at a native or scripting level (this is possible if the mouse pointer exited into the non-Silverlight area of the browser and was captured by scripting).

        The left mouse button is in a pressed (down) state.

If one of these conditions is false, the CaptureMouse return value is false.

     Here is the link : http://msdn.microsoft.com/en-us/library/system.windows.uielement.capturemouse(VS.95).aspx

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities