VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/82909114

此方法只是鼠标相对于当前窗口的坐标数值,而不是鼠标在电脑屏幕的坐标值。

效果

 

1、前台增加一个用于显示的文本控件

注意给控件命名

          <TextBlock Name="Textblock2"
                       FontSize="20"
                       Width=" 120"
                       Height=" 50"
                       FontStretch="Expanded"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center">
                
            </TextBlock>

2、为整个界面定义鼠标移动事件

 

3.在后台鼠标的移动事件中,增加代码

       private void Window_MouseMove(object sender, MouseEventArgs e)

        {
            Point pp = e.GetPosition(this);
            Textblock2.Text = string.Format("坐标{0},{1}", pp.X, pp.Y);
        }

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/82909114