Mouse cursor override and clear

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void StartSlowWork()
        {
            Mouse.OverrideCursor = Cursors.AppStarting;
        }

        private void SlowWorkCompleted()
        {
            Mouse.OverrideCursor = null;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Source != myEllipse)
            {
                StartSlowWork();
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            SlowWorkCompleted();
        }

    }
}

   
    
     


Check the mouse event source

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void StartSlowWork()
        {
            Mouse.OverrideCursor = Cursors.AppStarting;
        }

        private void SlowWorkCompleted()
        {
            Mouse.OverrideCursor = null;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Source != myEllipse)
            {
                StartSlowWork();
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            SlowWorkCompleted();
        }

    }
}

   
    
     


Use the Mouse Wheel action methods that are defined by the IScrollInfo interface

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ScrollViewer_Methods.Window1"
    Title="ScrollViewer IScrollInfo Sample"
    Loaded="onLoad">
<DockPanel>
<TextBlock DockPanel.Dock="Top" FontSize="20" FontWeight="Bold" Margin="10">IScrollInfo Interface Methods</TextBlock>
<StackPanel DockPanel.Dock="Left" Width="150">
    <Button Click="spMouseWheelDown">MouseWheelDown</Button>
    <Button Click="spMouseWheelUp">MouseWheelUp</Button>
    <Button Click="spMouseWheelLeft">MouseWheelLeft</Button>
    <Button Click="spMouseWheelRight">MouseWheelRight</Button>
</StackPanel>  
<Border BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
    <ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <StackPanel Name="sp1">
            <Button>Button 1</Button>
            <Button>Button 5</Button>
            <Rectangle Width="700" Height="500" Fill="Green"/>
            <TextBlock>Rectangle 3</TextBlock>
        </StackPanel> 
    </ScrollViewer>
</Border>
</DockPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Text;

namespace ScrollViewer_Methods
{
    public partial class Window1 : Window
    {
        private void onLoad(object sender, System.EventArgs e)
        {
            ((IScrollInfo)sp1).CanVerticallyScroll = true;
            ((IScrollInfo)sp1).CanHorizontallyScroll = true;
            ((IScrollInfo)sp1).ScrollOwner = sv1;
        }
        private void spMouseWheelDown(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelDown();
        }
        private void spMouseWheelUp(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelUp();
        }
        private void spMouseWheelLeft(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelLeft();
        }
        private void spMouseWheelRight(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).MouseWheelRight();
        }
    }
}

   
    
     


Mouse.LostMouseCaptureEvent

image_pdfimage_print


   
  



<Window x:Class="RoutedEvents.MousePosition"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MousePosition" Height="300" Width="300">
  <StackPanel>
    <Rectangle Name="rect" MouseMove="MouseMoved" Fill="LightBlue" ></Rectangle>
    <Button Name="cmdCapture" Click="cmdCapture_Click">Capture the Mouse</Button>
    <TextBlock Name="lblInfo"></TextBlock>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class MousePosition : System.Windows.Window
    {
        public MousePosition()
        {
            InitializeComponent();
        }
        private void cmdCapture_Click(object sender, RoutedEventArgs e)
        {
            this.AddHandler(Mouse.LostMouseCaptureEvent,new RoutedEventHandler(this.LostCapture));
            Mouse.Capture(rect);

            cmdCapture.Content = "Mouse captured";
        }

        private void MouseMoved(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            lblInfo.Text = String.Format("({0},{1}) in window coordinates",pt.X, pt.Y);            
        }

        private void LostCapture(object sender, RoutedEventArgs e)
        {
            lblInfo.Text = "Lost capture";
            cmdCapture.Content = "Capture the Mouse";
        }
    }
}

   
    
     


Use Mouse.Capture to let a Control capture an event

image_pdfimage_print


   
  



<Window x:Class="RoutedEvents.MousePosition"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MousePosition" Height="300" Width="300">
  <StackPanel>
    <Rectangle Name="rect" MouseMove="MouseMoved" Fill="LightBlue" ></Rectangle>
    <Button Name="cmdCapture" Click="cmdCapture_Click">Capture the Mouse</Button>
    <TextBlock Name="lblInfo"></TextBlock>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class MousePosition : System.Windows.Window
    {
        public MousePosition()
        {
            InitializeComponent();
        }
        private void cmdCapture_Click(object sender, RoutedEventArgs e)
        {
            this.AddHandler(Mouse.LostMouseCaptureEvent,new RoutedEventHandler(this.LostCapture));
            Mouse.Capture(rect);

            cmdCapture.Content = "Mouse captured";
        }

        private void MouseMoved(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            lblInfo.Text = String.Format("({0},{1}) in window coordinates",pt.X, pt.Y);            
        }

        private void LostCapture(object sender, RoutedEventArgs e)
        {
            lblInfo.Text = "Lost capture";
            cmdCapture.Content = "Capture the Mouse";
        }
    }
}

   
    
     


Capture Mouse Ellipse

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

            myEllipse.MouseDown += myEllipse_MouseDown;

        }

        void myEllipse_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(myEllipse);
        }


    }
}

   
    
     


Use Mouse.PreviewMouseDown /Mouse.MouseDown Attribute from Grid element

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.AttachedEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Event Examples" Height="300" Width="300">
  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button>
      <Grid Mouse.PreviewMouseDown="PreviewMouseDownGrid" Mouse.MouseDown="MouseDownGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>

          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" Stroke="Black" />

          <Ellipse Canvas.Left="4.5" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
          <Ellipse Canvas.Left="11" Canvas.Top="5" Width="2.5" Height="3" Fill="Black" />
          <Path Data="M 5,10 A 3,3 0 0 0 13,10" Stroke="Black" />
    
        <TextBlock Grid.Column="1">Click!</TextBlock>
      </Grid>
    </Button>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class AttachedEvents : System.Windows.Window
    {

        public AttachedEvents()
        {
            InitializeComponent();

        }
        void PreviewMouseDownGrid(
          object sender, RoutedEventArgs e)
        { Debug.WriteLine("PreviewMouseDownGrid"); }

        void MouseDownGrid(object sender, RoutedEventArgs e)
        { Debug.WriteLine("MouseDownGrid"); }

    }
}