Retrieving the mouse position relative to controls on a Window


   
  

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


        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);


            Point positionRelativeToEllipse = Mouse.GetPosition(myEllipse);

            Debug.WriteLine("Window.MouseMove: " + positionRelativeToEllipse);
        }


    }
}

   
    
     


Window On Mouse move event


   
  


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


        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);


            Point positionRelativeToEllipse = Mouse.GetPosition(myEllipse);

            Debug.WriteLine("Window.MouseMove: " + positionRelativeToEllipse);
        }


    }
}

   
    
     


Window On Mouse up event


   
  



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

    }
}

   
    
     


Display window as dialog


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout Examples" Height="300" Width="300">
    <StackPanel>
        <Button Height="23" Margin="10,10,10,0" Name="canvasWindowButton" Click="canvasWindowButton_Click">Canvas</Button>
    </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 canvasWindowButton_Click(object sender, RoutedEventArgs e)
        {
            //CanvasWindow window = new CanvasWindow();
            //window.ShowDialog();
        }
    }
}

   
    
     


Load resource from Window Resources


   
  
<Window x:Class="ControlStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="ControlStyles" Height="300" Width="300">
  <Window.Resources>
    <Style x:Key="BigLettersStyle">
      <Setter Property="Control.FontSize" Value="20" />
      <Setter Property="Control.FontWeight" Value="Bold" />
      <Setter Property="Control.BorderThickness" Value="2" />
    </Style>
    
    <Style x:Key="SmallLettersStyle">
      <Setter Property="Control.FontSize" Value="10" />
      <Setter Property="Control.FontWeight" Value="Normal" />
      <Setter Property="Control.BorderThickness" Value="1" />
    </Style>

  </Window.Resources>

  <Grid>
    <Button Click="MyClickEvent"
      Style="{StaticResource SmallLettersStyle}"
      VerticalAlignment="Top"
      HorizontalAlignment="Left"
      Grid.Column="0"
      Grid.ColumnSpan="1"
      Grid.Row="0"
      Grid.RowSpan="1"
      Margin="43"
      Width="75"
      Height="23"
      Name="btnGo">Go</Button>

  </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;

namespace ControlStyles
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            btnGo.Style = (Style)FindResource("BigLettersStyle");
        }
    }
}

   
    
     


Transparent Window


   
  
<Window x:Class="Windows.ModernWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ModernWindow" Height="300" Width="300"
        AllowsTransparency="True" Background="Transparent"
        WindowStyle="None" ResizeMode="CanResizeWithGrip">
   <Border Width="Auto" Height="Auto" Name="windowFrame"
          BorderBrush="#395984"
          BorderThickness="1"
          CornerRadius="0,20,30,40" >
    <Border.Background>
      <LinearGradientBrush >
        <GradientBrush.GradientStops>
          <GradientStopCollection>
            <GradientStop Color="#E7EBF7" Offset="0.0"/>
            <GradientStop Color="#CEE3FF" Offset="0.5"/>
  
          </GradientStopCollection>
        </GradientBrush.GradientStops>
      </LinearGradientBrush>
    </Border.Background>
    <StackPanel>
      <TextBlock Text="Title Bar" Margin="1" Padding="5" MouseLeftButtonDown="titleBar_MouseLeftButtonDown"></TextBlock>
      <Button Click="cmdClose_Click">Close</Button>
      <Rectangle Cursor="SizeWE" 
                 MouseLeftButtonDown="window_initiateWiden"
                 MouseLeftButtonUp="window_endWiden"
                 MouseMove="window_Widen"/>

      </StackPanel>
    
    
  </Border>
</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 Windows
{
    public partial class ModernWindow : System.Windows.Window
    {
        public ModernWindow()
        {
            InitializeComponent();
        }

        bool isWiden = false;
        private void window_initiateWiden(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isWiden = true;
        }
        private void window_endWiden(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isWiden = false;

            Rectangle rect = (Rectangle)sender;
            rect.ReleaseMouseCapture();
        }

        private void window_Widen(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            if (isWiden)
            {                
                rect.CaptureMouse();
                double newWidth = e.GetPosition(this).X + 5;
                if (newWidth > 0) this.Width = newWidth;
            }            
        }

        private void titleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void cmdClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

   
    
     


Animated Video Window


   
  

<Window x:Class="SoundAndVideo.AnimatedVideoWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimatedVideoWindow" Height="450" Width="400">

  <Window.Resources>
    <Storyboard x:Key="MediaStoryboardResource">
      <MediaTimeline Storyboard.TargetName="media" Source="test.mpg" FillBehavior="HoldEnd"
                      RepeatBehavior="Forever"></MediaTimeline>
      <DoubleAnimation Storyboard.TargetName="media" Storyboard.TargetProperty="(MediaElement.RenderTransform).(RotateTransform.Angle)"
    To="360" Duration="0:0:2" RepeatBehavior="Forever"></DoubleAnimation>
    </Storyboard>
  </Window.Resources>

  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdPlay">
        <EventTrigger.Actions>
          <BeginStoryboard Name="MediaStoryboard" Storyboard="{StaticResource MediaStoryboardResource}"/>
        </EventTrigger.Actions>
      </EventTrigger>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdStop">
        <EventTrigger.Actions>
          <StopStoryboard BeginStoryboardName="MediaStoryboard"/>
        </EventTrigger.Actions>
      </EventTrigger>      
    </StackPanel.Triggers>

      <Button Name="cmdPlay">Play (Declarative)</Button>
      <Button Name="cmdStop">Stop (Declarative)</Button>
      <Button Click="cmdPlayCode_Click">Play (Code)</Button>
    <MediaElement Name="media" Grid.Row="1" Stretch="None" RenderTransformOrigin="0.5,0.5" >
      <MediaElement.RenderTransform>
        <RotateTransform></RotateTransform>
      </MediaElement.RenderTransform>
    </MediaElement>
  </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;
using System.Windows.Media.Animation;

namespace SoundAndVideo
{
    public partial class AnimatedVideoWindow : System.Windows.Window
    {
        public AnimatedVideoWindow()
        {
            InitializeComponent();
        }
        private void cmdPlayCode_Click(object sender, RoutedEventArgs e)
        {
            MediaTimeline timeline = new MediaTimeline(new Uri("test.mpg", UriKind.Relative));            
            timeline.RepeatBehavior = RepeatBehavior.Forever;

            MediaClock clock = timeline.CreateClock();
            MediaPlayer player = new MediaPlayer();
            player.Clock = clock;

            VideoDrawing videoDrawing = new VideoDrawing();
            videoDrawing.Rect = new Rect(150, 0, 100, 100);
            videoDrawing.Player = player;

            DrawingBrush brush = new DrawingBrush(videoDrawing);
            this.Background = brush;

            clock.Controller.Begin();
        }
    }
}