1.获取鼠标在控件中的坐标:

 private void item_MouseDown(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(lbl); } //或者直接使用Mouse类的静态方法GetPosition(),
//需要注意的是参数为IInputElement类型,也就是说要是能输入的控件
Point point2 = Mouse.GetPosition(lbl2);
lbl2.Content = "(" + point2.X + ", " + point2.Y + ")";

完整例子代码:

XAML代码

 <Window x:Class="WpfGetPointDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="grid" MouseDown="item_MouseDown">
<Label Background="Red" x:Name="lbl" Margin="293.855,59.398,66.145,77.319"/>
<Label Background="GreenYellow" x:Name="lbl2" Margin="29.488,59.398,327.512,69.969"/>
<Label Background="blue" x:Name="lbl3" HorizontalAlignment="Left" Margin="133.048,268.187,0,0" VerticalAlignment="Top" Width="250.952" Height="51.813"/>
<Button x:Name="btn" HorizontalAlignment="Left" Margin="177.325,10,0,0" VerticalAlignment="Top" Width="135.09" RenderTransformOrigin="0.012,0.547" Height="43.252"/>
</Grid>
</Window>

后台C#代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes; namespace WpfGetPointDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void item_MouseDown(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(lbl);
lbl.Content = "("+point.X+", "+point.Y+")"; Point point2 = Mouse.GetPosition(lbl2);
lbl2.Content = "(" + point2.X + ", " + point2.Y + ")"; Point point3 = Mouse.GetPosition(grid);
lbl3.Content = "(" + point3.X + ", " + point3.Y + ")"; Point point4 = Mouse.GetPosition(btn);
btn.Content = "(" + point4.X + ", " + point4.Y + ")";
}
}
}

运行结果:

2.获取控件相对于两一个控件的坐标:

2.1. 直接使用  control1.TranslatePoint(new Point(), control2)

 Point point = rectangle.TranslatePoint(new Point(),canvas);
  

2.2.获取控件在Window中的坐标

 Window window =  Window.GetWindow(canvas);
Point point = canvas.TransformToAncestor(window).Transform(new Point(, ));

引用:http://www.fengfly.com/plus/view-210427-1.html

WPF获取相对位置、坐标的方法的更多相关文章

  1. WPF 获取程序路径的一些方法,根据程序路径获取程序集信息

    一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...

  2. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

  3. js获取鼠标位置的各种方法

    在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...

  4. CGRectGet *** 获取控件坐标的方法

    CGRectGetHeight返回label本身的高度 CGRectGetMinY返回label顶部的坐标 CGRectGetMaxY 返回label底部的坐标 CGRectGetMinX 返回lab ...

  5. WPF获取程序版本号(Version)的方法

    1.第一种:通过System来获取 public static Version GetEdition() { return System.Reflection.Assembly.GetExecutin ...

  6. wpf获取webbroswer的两个方法

    //跳转前的地址 private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref ...

  7. WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法

    一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...

  8. WPF获取和设置鼠标位置与progressbar的使用方法

    一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...

  9. WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)

    原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...

随机推荐

  1. hdu 5003 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5003 记得排序后输出 #include <cstdio> #include <cstring& ...

  2. 集合(三)CopyOnWriteArrayList

    第一次见到CopyOnWriteArrayList,是在研究JDBC的时候,每一个数据库的Driver都是维护在一个CopyOnWriteArrayList中的,为了证明这一点,贴两段代码,第一段在c ...

  3. STS中配置MyBatis代码生成器

    1.STS工具菜单项Help > Eclipse Marketplace... 2.输入“mybatis”关键字搜索 3.选择MyBatis Generator 1.3.7进行安装 4.安装成功 ...

  4. 使用 X-Frame-Options 防止被iframe 造成跨域iframe 提交挂掉

     Refused to display 'http://www.***.com/login/doLogin.html' in a frame because it set 'X-Frame-Optio ...

  5. PostgreSQL 用户和权限管理

    PostgreSQL 用户和权限管理 创建 CREATE ROLE rolename;CREATE USER username;CREATE USER和CREATE ROLE的区别在于,CREATE ...

  6. django分页及搜索后如何翻页

    django自带了Pagnator 导入 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage 分页 def ...

  7. 2D Circular Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction The goal of the circular kernel is to offer to the user a large set of functionalitie ...

  8. MongoDB下载及安装

    MongoDB的下载及安装 1.下载: MongoDB的官网是:http://www.mongodb.org/ 2.安装: 方案一:(程序启动方式) 1> 创建文件夹:MongoDB    在D ...

  9. 20164317《网络对抗技术》Exp1 PC平台逆向破解

    实验目的: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getSh ...

  10. Centos 7 GCC 7.3编译器安装方法及C++17标准测试示例

    1.下载gcc-7.3.0源码 http://mirror.linux-ia64.org/gnu/gcc/releases/gcc-7.3.0/ 2.下载编译依赖 [root@localhost ~] ...