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

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="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;
        }
    }
}

   
    
     


Use Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);

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>
        <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();

            Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);
            Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

        }

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

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

    }
}

   
    
     


Use Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

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>
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
          <Ellipse x:Name="myEllipse" Canvas.Left="1" Canvas.Top="1" Width="16" Height="16" Fill="Yellow" 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();

            Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse);
            Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

        }

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

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

    }
}

   
    
     


Display a message box and get the message box return value.

image_pdfimage_print


   
  


<Window x:Class="MessageBoxSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MessageBoxSample" Height="300" Width="500">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0">Associate with Owner Window?</Label>
    <CheckBox Grid.Column="1" Grid.Row="0" Name="ownerCheckBox"></CheckBox>

    <Label Grid.Column="0" Grid.Row="1">messageBoxText:</Label>
    <TextBox Grid.Column="1" Grid.Row="1" Name ="messageBoxText">MessageBoxText</TextBox>

    <Label Grid.Column="0" Grid.Row="2">caption:</Label>
    <TextBox Grid.Column="1" Grid.Row="2" Name="caption">Caption</TextBox>

    <Label Grid.Column="0" Grid.Row="3">button:</Label>
    <ComboBox Grid.Column="1" Grid.Row="3" Name="buttonComboBox">
      <ComboBoxItem IsSelected="True">OK</ComboBoxItem>
      <ComboBoxItem>OKCancel</ComboBoxItem>
      <ComboBoxItem>YesNo</ComboBoxItem>
      <ComboBoxItem>YesNoCancel</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="4">icon:</Label>
    <ComboBox Grid.Column="1" Grid.Row="4" Name="imageComboBox">
      <ComboBoxItem>Asterisk</ComboBoxItem>
      <ComboBoxItem>Error</ComboBoxItem>
      <ComboBoxItem>Exclamation</ComboBoxItem>
      <ComboBoxItem>Hand</ComboBoxItem>
      <ComboBoxItem>Information</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>Question</ComboBoxItem>
      <ComboBoxItem>Stop</ComboBoxItem>
      <ComboBoxItem>Warning</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="5">defaultResult:</Label>
    <ComboBox Grid.Column="1" Grid.Row="5" Name="defaultResultComboBox">
      <ComboBoxItem>Cancel</ComboBoxItem>
      <ComboBoxItem>No</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>OK</ComboBoxItem>
      <ComboBoxItem>Yes</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="6">options</Label>
    <ComboBox Grid.Column="1" Grid.Row="6" Name="optionsComboBox">
      <ComboBoxItem>DefaultDesktopOnly</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>RightAlign</ComboBoxItem>
      <ComboBoxItem>RtlReading</ComboBoxItem>
      <ComboBoxItem>ServiceNotification</ComboBoxItem>
    </ComboBox>

    <Button Grid.Column="1" Grid.Row="7" Name="showMessageBoxButton" Click="showMessageBoxButton_Click">Show MessageBox</Button>

    <StatusBar Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="8" >
      <StatusBarItem>
        <TextBlock Name="resultTextBlock">Ready</TextBlock>
      </StatusBarItem>
    </StatusBar>

  </Grid>

</Window>


//File:Window.xaml.cs
using System;
using System.Windows;

namespace MessageBoxSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void showMessageBoxButton_Click(object sender, RoutedEventArgs e)
        {
            Window owner = ((bool)ownerCheckBox.IsChecked ? this : null);
            string messageBoxText = this.messageBoxText.Text;
            string caption = this.caption.Text;
            MessageBoxButton button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), this.buttonComboBox.Text);
            MessageBoxImage icon = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), this.imageComboBox.Text);
            MessageBoxResult defaultResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), this.defaultResultComboBox.Text);
            MessageBoxOptions options = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), this.optionsComboBox.Text);

            MessageBoxResult result;
            if (owner == null)
            {
                result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
            }
            else
            {
                result = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
            }
            
            resultTextBlock.Text = "Result = " + result.ToString();
        }
    }
}

   
    
     


A line which monitors the mouse entering its area

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WpfApplication1" Height="352" Width="334" WindowStartupLocation="CenterScreen">
    <StackPanel>
      <Line Name ="SimpleLine" X1 ="0" X2 ="50" Y1 ="0" Y2 ="50" 
            Stroke ="DarkOliveGreen" StrokeThickness ="5"
            ToolTip ="This is a line!" MouseEnter ="SimpleLine_MouseEnter"/>
    </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 WpfApplication1
{
  public partial class MainWindow : System.Windows.Window
  {

    public MainWindow()
    {
      InitializeComponent();
    }
    protected void SimpleLine_MouseEnter(object sender, MouseEventArgs args)
    {
      this.Title = String.Format("Mouse entered at: {0}", 
        args.GetPosition(SimpleLine));
    }
  }
}

   
    
     


Respond When the User Rotates the Mouse Wheel

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="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;
            }
        }
    }
}

   
    
     


Mouse Position and TranslateTransform

image_pdfimage_print


   
  

<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);
    }
  }
}