WPF获取相对位置、坐标的方法
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获取相对位置、坐标的方法的更多相关文章
- WPF 获取程序路径的一些方法,根据程序路径获取程序集信息
一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...
- WPF 获取系统 DPI 的多种方法
原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...
- js获取鼠标位置的各种方法
在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...
- CGRectGet *** 获取控件坐标的方法
CGRectGetHeight返回label本身的高度 CGRectGetMinY返回label顶部的坐标 CGRectGetMaxY 返回label底部的坐标 CGRectGetMinX 返回lab ...
- WPF获取程序版本号(Version)的方法
1.第一种:通过System来获取 public static Version GetEdition() { return System.Reflection.Assembly.GetExecutin ...
- wpf获取webbroswer的两个方法
//跳转前的地址 private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref ...
- WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法
一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...
- WPF获取和设置鼠标位置与progressbar的使用方法
一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...
- WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)
原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...
随机推荐
- hdu 4268 贪心+set lower_bound用法
http://acm.hdu.edu.cn/showproblem.php?pid=4268 A想用手里的牌尽量多地覆盖掉B手中的牌.. 牌有h和w 问A手中的牌最多能覆盖B多少张牌 iterator ...
- Billman_ford货币升值——正权回路
2240和1860那个题目很像啊 都是问货币能不能增多,钻社会制度得空子啊哈哈 唯一不同得是你的起点是任意一个点,这个比较麻烦了,多了一层循环嘞 处理货币名可以用map分配id 然后就是老套的Bill ...
- git提交提示workspace.xml出现conflicted
问题:在github上管理项目,多次提交以后提交提示workspace.xml出现conflicted原因:Android项目在根目录的.gitignore文件中没有添加.idea文件夹忽略. 解决办 ...
- Windows / Windows Phone 8.1 预留应用名称及应用上传
最近比较懒好久没有来这里跟大家聊了,WP 8.1 的 preview 发布已经有一阵子了,并且商店支持 8.1 应用也有一段时间了.我就把这篇 8.1 的应用商店预留提交作为 8.1 的一个开始吧. ...
- c# 协变和逆变的理解
1. 是什么 1.1 协变 协变指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型.如 string 到 object 的转换.多见于类型参数用作方法的返回值. 1.2 逆变 逆变指能够 ...
- 自适应XAML布局经验总结 (二) 局部布局设计模式1
本系列对实际项目中的XAML布局场景进行总结,给出了较优化的自适应布局解决方案,希望对大家有所帮助. 下面开始介绍局部布局设计模式. 1. 工具栏模式 适用于工具栏,标题等的布局. 此块布局区域外层使 ...
- MongoDB与SqlSugar与Ado.Net对比
SqlSugar NET 4.+ & .NET CORE 高性能 轻量级 ORM框架,众多.NET框架中最容易使用的数据库访问技术 MongoDB与MySql的安装省略...虽然遇到了一些意外 ...
- SignalR简单封装
需求:Asp.Net MVC 开发客户端,实现与服务器端实时通信. 众所周知,Web开发是基于http的请求响应模型,每次刷新都需要客户端(浏览器)主动发起请求,那么,这个问题怎么解?Asp.Net ...
- A Tool To Plot Mathematical Function
Plot.cs using Microsoft.ClearScript; using Microsoft.ClearScript.V8; using Microsoft.Win32; using Sy ...
- 静态分析第三发 so文件分析(小黄人快跑)
本文作者:i春秋作家——HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/downl ...