如何让事件处理的图像点击鼠标的坐标? [英] How to get the coordinates of an image mouse click in the event handler?
本文介绍了如何让事件处理的图像点击鼠标的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的 WPF 应用,我有一个 ContentControl中的图片即可。当图片用户点击,我怎么能得到的的其中鼠标单击图像的 x / y坐标?
XAML:
<窗口x:类=TestClick828374.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP://模式。 microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
< StackPanel的保证金=10>
< ContentControl中CONTENT ={结合TheImage}的MouseDown =ContentControl_MouseDown/>
< / StackPanel的>
< /窗GT;
代码隐藏:
使用系统;使用System.Windows
;使用System.Windows.Controls的
;使用System.Windows.Media
;
使用System.Windows.Media.Imaging;
使用System.ComponentModel;
命名空间TestClick828374
{
公共部分类窗口1:窗口,INotifyPropertyChanged的
{
#地区ViewModelProperty:TheImage
私人图片_theImage;
公众形象TheImage
{
得到
{
返回_theImage;
}
组
{
_theImage =价值;
OnPropertyChanged(TheImage);
}
}
#endregion
公共窗口1()
{
的InitializeComponent();
的DataContext =这一点;
TheImage =新的图像();
TheImage.Source =新的BitmapImage(新的URI(@C:\test\rectangle.png));
TheImage.Stretch = Stretch.None;
TheImage.HorizontalAlignment = HorizontalAlignment.Left;
}
#地区INotifiedProperty座
公共事件PropertyChangedEventHandler的PropertyChanged;
保护无效OnPropertyChanged(字符串propertyName的)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
#endregion
私人无效ContentControl_MouseDown(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
{
/ /如何获得鼠标的坐标请点击图片?
}
}
}
解决方案
发现:
点clickPoint = e.GetPosition(TheImage);
下面的显示解决方案我完整的代码示例:
http://www.tanguay.info/web/index.php?pg = codeExamples和ID = 364
In the following WPF app, I have an Image in a ContentControl. When the user clicks on the image, how can I get the x/y coordinates of where the mouse clicked on the image?
XAML:
<Window x:Class="TestClick828374.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="10">
<ContentControl Content="{Binding TheImage}" MouseDown="ContentControl_MouseDown"/>
</StackPanel>
</Window>
Code-Behind:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
namespace TestClick828374
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: TheImage
private Image _theImage;
public Image TheImage
{
get
{
return _theImage;
}
set
{
_theImage = value;
OnPropertyChanged("TheImage");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
TheImage = new Image();
TheImage.Source = new BitmapImage(new Uri(@"c:\test\rectangle.png"));
TheImage.Stretch = Stretch.None;
TheImage.HorizontalAlignment = HorizontalAlignment.Left;
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//how to get the coordinates of the mouse click here on the image?
}
}
}
解决方案
Found it:
Point clickPoint = e.GetPosition(TheImage);
Here's my full code example showing the solution:
http://www.tanguay.info/web/index.php?pg=codeExamples&id=364
这篇关于如何让事件处理的图像点击鼠标的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文