<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.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Namespace RoutedEvents
Public Partial Class MousePosition
Inherits System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub cmdCapture_Click(sender As Object, e As RoutedEventArgs)
Me.[AddHandler](Mouse.LostMouseCaptureEvent, New RoutedEventHandler(AddressOf Me.LostCapture))
Mouse.Capture(rect)
cmdCapture.Content = "Mouse captured"
End Sub
Private Sub MouseMoved(sender As Object, e As MouseEventArgs)
Dim pt As Point = e.GetPosition(Me)
lblInfo.Text = [String].Format("({0},{1}) in window coordinates", pt.X, pt.Y)
End Sub
Private Sub LostCapture(sender As Object, e As RoutedEventArgs)
lblInfo.Text = "Lost capture"
cmdCapture.Content = "Capture the Mouse"
End Sub
End Class
End Namespace