捕获鼠标位置

cs代码:

        private void Rectangle_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            TextBlock1.Text = "当前坐标(" + (int)pt.X+","+(int)pt.Y+ ")";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Mouse.Capture(this);
            button.Content = "鼠标被捕获";
        }

xaml代码:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Rectangle x:Name="rect" Fill="LightBlue" PreviewMouseMove="Rectangle_PreviewMouseMove"></Rectangle>
        <Button x:Name="button" Grid.Row="1" Click="Button_Click">捕获鼠标位置</Button>
        <TextBlock x:Name="TextBlock1" Grid.Row="2"></TextBlock>
    </Grid>

 

posted @ 2019-06-05 17:43  夕西行  阅读(340)  评论(0编辑  收藏  举报