如何在事件处理程序中获取图像鼠标单击的坐标?

发布于 2024-08-25 16:06:33 字数 2273 浏览 4 评论 0原文

在以下 WPF 应用程序中,我在 ContentControl 中有一个图像。当用户点击图像时,如何获取鼠标点击图像的x/y坐标

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>

代码隐藏:

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?
        }

    }
}

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?
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-09-01 16:06:33

找到它:

Point clickPoint = e.GetPosition(TheImage);

这是我显示解决方案的完整代码示例:

http:// /www.tanguay.info/web/index.php?pg=codeExamples&id=364

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文