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

   
    
     


Message Only MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageOnly_Click" Content="Message Only" Margin="5" Name="btnMessageOnly" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnMessageOnly_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A simple MessageBox.");
        }

    }
}

   
    
     


MessageBox with Message and Header

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeader_Click" Content="Message and Header" Margin="5" Name="btnMessageHeader" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void btnMessageHeader_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title.", "WPF");
        }

    }
}

   
    
     


Set Message, Header, and Button for MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeaderButton_Click" Content="Message, Header, and Button" Margin="5" Name="btnMessageHeaderButton" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void btnMessageHeaderButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title and buttons.",  "WPF", MessageBoxButton.YesNoCancel);
        }

    }
}

   
    
     


Customize Message, Header, Button, and Image for MessageBox

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="170" Width="300">
    <StackPanel>
        <Button Click="btnMessageHeaderButtonImage_Click" Content="Message, Header, Button, and Image" Margin="5" Name="btnMessageHeaderButtonImage" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnMessageHeaderButtonImage_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("A MessageBox with a title, buttons, and an icon.", "WPF",
                MessageBoxButton.YesNoCancel,
                MessageBoxImage.Warning);
        }
    }
}