WPF獲取相對位置、坐標的方法


1.獲取鼠標在控件中的坐標:

 1 private void item_MouseDown(object sender, MouseButtonEventArgs e)
 2 {
 3         Point point = e.GetPosition(lbl);   
 4 
 5  }
 6 
 7 //或者直接使用Mouse類的靜態方法GetPosition(),
 8 //需要注意的是參數為IInputElement類型,也就是說要是能輸入的控件
 9 Point point2 = Mouse.GetPosition(lbl2);
10 lbl2.Content = "(" + point2.X + ", " + point2.Y + ")";
View Code

完整例子代碼:

   XAML代碼

 1 <Window x:Class="WpfGetPointDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Grid x:Name="grid" MouseDown="item_MouseDown">
 6         <Label Background="Red" x:Name="lbl" Margin="293.855,59.398,66.145,77.319"/>
 7         <Label Background="GreenYellow" x:Name="lbl2" Margin="29.488,59.398,327.512,69.969"/>
 8         <Label Background="blue" x:Name="lbl3" HorizontalAlignment="Left" Margin="133.048,268.187,0,0" VerticalAlignment="Top" Width="250.952" Height="51.813"/>
 9         <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"/>
10     </Grid>
11 </Window>
View Code

   后台C#代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 
15 namespace WpfGetPointDemo
16 {
17     /// <summary>
18     /// Interaction logic for MainWindow.xaml
19     /// </summary>
20     public partial class MainWindow : Window
21     {
22         public MainWindow()
23         {
24             InitializeComponent();
25         }
26 
27         private void item_MouseDown(object sender, MouseButtonEventArgs e)
28         {
29             Point point = e.GetPosition(lbl);   
30             lbl.Content = "("+point.X+", "+point.Y+")";
31 
32             Point point2 = Mouse.GetPosition(lbl2);
33             lbl2.Content = "(" + point2.X + ", " + point2.Y + ")";
34 
35             Point point3 = Mouse.GetPosition(grid);
36             lbl3.Content = "(" + point3.X + ", " + point3.Y + ")";
37 
38             Point point4 = Mouse.GetPosition(btn);
39             btn.Content = "(" + point4.X + ", " + point4.Y + ")";
40         }
41     }
42 }
View Code

   運行結果:

    

2.獲取控件相對於兩一個控件的坐標:

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

1 Point point = rectangle.TranslatePoint(new Point(),canvas);   
2  
View Code

  2.2.獲取控件在Window中的坐標

1 Window window =  Window.GetWindow(canvas);  
2 Point  point  =  canvas.TransformToAncestor(window).Transform(new Point(0, 0));  
View Code

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM