WPF之检测鼠标位置
来源:互联网 发布:网络运营部职责 编辑:程序博客网 时间:2024/04/28 15:30
<Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Rectangle Name="rect" MouseMove="MouseMoved" Fill="LightBlue" ></Rectangle> <Button Grid.Row="1" Name="cmdCapture" Click="cmdCapture_Click">Capture the Mouse</Button> <TextBlock Name="lblInfo" Grid.Row="2"></TextBlock> </Grid>
private void cmdCapture_Click(object sender, RoutedEventArgs e) { this.AddHandler( Mouse.LostMouseCaptureEvent, new RoutedEventHandler(this.LostCapture)); Mouse.Capture(rect); cmdCapture.Content = "[ Mouse is now captured ... ]"; } private void MouseMoved(object sender, MouseEventArgs e) { Point pt = e.GetPosition(this); lblInfo.Text = String.Format("You are at ({0},{1}) in window coordinates", pt.X, pt.Y); } private void LostCapture(object sender, RoutedEventArgs e) { MessageBox.Show("Lost capture"); cmdCapture.Content = "Capture the Mouse"; }