Make an object follow the mouse pointer as it moves on the screen.


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="mouseMoveWithPointer" Height="400" Width="500">
  <Canvas MouseMove="MouseMoveHandler" Background="Red">
    <Ellipse Name="ellipse" Fill="LightBlue" Width="100" Height="100"/>
  </Canvas>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Input;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }
        private void MouseMoveHandler(object sender, MouseEventArgs e)
        {
            // Get the x and y coordinates of the mouse pointer.
            Point position = e.GetPosition(this);
            double pX = position.X;
            double pY = position.Y;

            ellipse.Width = pX;
            ellipse.Height = pY;
        }
    }
}

   
    
     


Mouse Enter and leave a Border


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="mouseEnterLeave" Height="400" Width="500">

  <StackPanel>
    <Border MouseEnter="OnMouseEnterHandler"
            MouseLeave="OnMouseLeaveHandler"
            Name="border1" Margin="10"
            BorderThickness="1"
            BorderBrush="Black"
            VerticalAlignment="Center"
            Width="300" Height="100">
      <Label Margin="10" FontSize="14"
             HorizontalAlignment="Center">Move Cursor Over Me</Label>
    </Border>
  </StackPanel>
</Window>


//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void OnMouseEnterHandler(object sender, MouseEventArgs e)
        {
            border1.Background = Brushes.Red;
        }
        void OnMouseLeaveHandler(object sender, MouseEventArgs e)
        {
            border1.Background = Brushes.White;
        }
    }
 
}

   
    
     


Handler MouseLeftButtonDown and MouseLeftButtonUp events


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DetectMouseButtonState" Height="400" Width="400">

  <StackPanel Height="100" Width="100" 
      MouseLeftButtonDown="HandleButtonDown" 
      MouseLeftButtonUp="HandleButtonDown" 
      Background="Red"
      DockPanel.Dock="Left">
    <TextBlock>Click on Me</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void HandleButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel sourceStackPanel = e.Source as StackPanel;

            if (e.ButtonState == MouseButtonState.Pressed)
            {
                sourceStackPanel.Width = 200;
                sourceStackPanel.Height = 200;
            }else if (e.ButtonState == MouseButtonState.Released)
            {
                sourceStackPanel.Width = 100;
                sourceStackPanel.Height = 100;
            }
        }
    }
}

   
    
     


Detect whether the mouse button is pressed or released using the MouseButtonState property.


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DetectMouseButtonState" Height="400" Width="400">

  <StackPanel Height="100" Width="100" 
      MouseLeftButtonDown="HandleButtonDown" 
      MouseLeftButtonUp="HandleButtonDown" 
      Background="Red"
      DockPanel.Dock="Left">
    <TextBlock>Click on Me</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        void HandleButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel sourceStackPanel = e.Source as StackPanel;

            if (e.ButtonState == MouseButtonState.Pressed)
            {
                sourceStackPanel.Width = 200;
                sourceStackPanel.Height = 200;
            }else if (e.ButtonState == MouseButtonState.Released)
            {
                sourceStackPanel.Width = 100;
                sourceStackPanel.Height = 100;
            }
        }
    }
}

   
    
     


Mouse Position


   
  
<Grid 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="InputExamples.MousePosition" 
  Width="640" Height="480">
  <Label d:LayoutOverrides="Height" x:Name="firstCoordinates"/>
  <Rectangle Stroke="#FF000000" Fill="#FFFFFFFF" x:Name="Rectangle"/>
  <Ellipse Stroke="#FF000000" Fill="#FFFFFFFF" x:Name="secondEllipse"/>
  <Ellipse d:LayoutOverrides="Height" Stroke="#FF000000" Width="14" Height="14" x:Name="firstEllipse"/>
  <Ellipse d:LayoutOverrides="Width" Stroke="#FF000000" Width="14" Height="14" x:Name="fourthEllipse"/>
  <Ellipse d:LayoutOverrides="Width" Stroke="#FF000000" Width="14" Height="14" x:Name="thirdEllipse"/>
  <Label d:LayoutOverrides="Width, Height" Width="66" Height="22" x:Name="secondCoordinates"/>
  <Label d:LayoutOverrides="Width, Height" Width="66" Height="22" x:Name="thirdCoordinates"/>
  <Label d:LayoutOverrides="Width, Height" Width="66" Height="22" x:Name="fourthCoordinates"/>
  <Ellipse Fill="Red" Width="16" Height="16" x:Name="DragEllipse"/>
</Grid>
//File:Window.xaml.cs


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InputExamples
{
  public partial class MousePosition
  {
    private TranslateTransform ellipseTransform = new TranslateTransform();
    
    public MousePosition()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      
      DragEllipse.RenderTransform = ellipseTransform;
      CompositionTarget.Rendering += this.CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
          Point mouse1 = Mouse.GetPosition(firstEllipse);
          Point mouse2 = Mouse.GetPosition(secondEllipse);
          Point mouse3 = Mouse.GetPosition(thirdEllipse);
          Point mouse4 = Mouse.GetPosition(fourthEllipse);
          
          firstCoordinates.Content = mouse1.ToString();
          secondCoordinates.Content = mouse2.ToString();
          thirdCoordinates.Content = mouse3.ToString();
          fourthCoordinates.Content = mouse4.ToString();
      
          Point position = Mouse.GetPosition(DragEllipse);
          ellipseTransform.X += position.X - (DragEllipse.Width / 2);
          ellipseTransform.Y += position.Y - (DragEllipse.Height / 2);
    }
  }
}

   
    
     


Mouse Position and TranslateTransform


   
  

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="InputExamples.MousePosition" Width="640" Height="480">
  <Rectangle Width="Auto" Height="Auto" x:Name="Rectangle" StrokeDashCap="Square" />
  <Ellipse Width="14" Height="14" x:Name="secondEllipse"/>
  <Ellipse d:LayoutOverrides="Height" Margin="20" Width="14" Height="14" x:Name="firstEllipse"/>
  <Ellipse d:LayoutOverrides="Width" Margin="30" Width="14" Height="14" x:Name="fourthEllipse"/>
  <Ellipse d:LayoutOverrides="Width" Margin="20" Width="14" Height="14" x:Name="thirdEllipse"/>
  <Ellipse Fill="Red" Margin="10" Width="16" Height="16" x:Name="DragEllipse"/>
</Grid>

//File:Window.xaml.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InputExamples
{
  public partial class MousePosition
  {
    private TranslateTransform ellipseTransform = new TranslateTransform();
    
    public MousePosition()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      
      DragEllipse.RenderTransform = ellipseTransform;
      CompositionTarget.Rendering += this.CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
          Point mouse1 = Mouse.GetPosition(firstEllipse);
          Point mouse2 = Mouse.GetPosition(secondEllipse);
          Point mouse3 = Mouse.GetPosition(thirdEllipse);
          Point mouse4 = Mouse.GetPosition(fourthEllipse);
              
          Console.WriteLine(mouse1.ToString());
          Console.WriteLine(mouse2.ToString());
          Console.WriteLine(mouse3.ToString());
          Console.WriteLine(mouse4.ToString());
          
          Point position = Mouse.GetPosition(DragEllipse);
          ellipseTransform.X += position.X - (DragEllipse.Width / 2);
          ellipseTransform.Y += position.Y - (DragEllipse.Height / 2);
    }
  }
}

   
    
     


Respond When the User Rotates the Mouse Wheel


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="300">
    <Canvas>
        <Slider Canvas.Top="10" Canvas.Left="20" Name="sldSlider" 
                Minimum="0" Maximum="1000" Value="500"
                Width="250" MouseWheel="Slider_MouseWheel"/>
        <RichTextBox Canvas.Top="50" Canvas.Left="20" Width="250" Height="100" VerticalScrollBarVisibility="Visible">
            <FlowDocument>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
                <Paragraph FontSize="12">
                    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
                    sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
                    magna aliquam erat volutpat.
                </Paragraph>
                <Paragraph FontSize="15">
                    Ut wisi enim ad minim veniam, quis nostrud exerci tation 
                    ullamcorper suscipit lobortis nisl ut aliquip ex ea 
                    commodo consequat. Duis autem vel eum iriure.
                </Paragraph>
                <Paragraph FontSize="18">A List</Paragraph>
                <List>
                    <ListItem>
                        <Paragraph>
                            <Bold>Bold List Item</Bold>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Italic>Italic List Item</Italic>
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            <Underline>Underlined List Item</Underline>
                        </Paragraph>
                    </ListItem>
                </List>
            </FlowDocument>
        </RichTextBox>
        <Rectangle Canvas.Top="160" Canvas.Left="20" Name="shpRectangle"
                   Fill="LightBlue" Width="50" Height="50" 
                   MouseWheel="Rectangle_MouseWheel">
        </Rectangle>
    </Canvas>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Slider_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            sldSlider.Value += (e.Delta > 0) ? 5 : -5;
        }
        private void Rectangle_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                double newWidth = shpRectangle.Width += (e.Delta > 0) ? 5 : -5;
                if (newWidth < 10) newWidth = 10;
                if (newWidth > 200) newWidth = 200;

                shpRectangle.Width = newWidth;
            }else{
                double newHeight = shpRectangle.Height += (e.Delta > 0) ? 5 : -5;
                if (newHeight < 10) newHeight = 10;
                if (newHeight > 200) newHeight = 200;
                shpRectangle.Height = newHeight;
            }
        }
    }
}