WPF_显示鼠标所在像素点的位置

1.MouseEvent.xaml

<Window x:Class="WPF1.MouseEvent"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF1"
        mc:Ignorable="d"
        Title="MouseEvent" Height="300" Width="300"
        MouseMove="Grid_MouseMove">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Name="tbText" Grid.Row="1" Margin="5"></TextBlock>
    </Grid>
</Window>

 

2.4.MouseEvent.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPF1
{
    /// <summary>
    /// _4.xaml 的交互逻辑
    /// </summary>
    public partial class MouseEvent : Window
    {
        public MouseEvent()
        {
            InitializeComponent();
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            tbText.Text = string.Format("You are at ({0},{1}) in window coordinates.", pt.X, pt.Y);
        }
        //任何时候都可以使用 Application.Shutdown() 方法来结束 App
        //这会使,Application.Run 立刻返回。
        //    
    }   
}

 

 

来自:https://github.com/chanchancl/LearnWPF

posted @ 2022-06-01 15:17  驼七  阅读(132)  评论(0)    收藏  举报