图片点击事件

15 篇文章 0 订阅

图片点击事件

方法1:

界面:

<Window x:Class="ImageClick.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>
        <Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest1" Stretch="Fill" VerticalAlignment="Top" Width="203"  />
        <!--<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest2" Stretch="Fill" VerticalAlignment="Top" Width="203" Source="/ImageClick;component/IMG/imgTest.png" />-->
    </Grid>
</Window>


逻辑代码:

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;


namespace ImageClick
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public event MouseButtonEventHandler imageMouseUp;
#if false
      
#else
        public MainWindow()
        {
            InitializeComponent();


            BitmapImage bi = new BitmapImage();
            // BitmapImage.UriSource must be in a BeginInit/EndInit block.
            bi.BeginInit();
            bi.UriSource = new Uri(@"IMG/imgTest.png", UriKind.RelativeOrAbsolute);
            bi.EndInit();
            imageTest1.Source = bi;

            EmulateClickEvent(imageTest1, ImageOnClick);

        }


        /// <summary>
        /// 为控件附加模拟鼠标单击事件
        /// </summary>
        /// <param name="control">需要附加事件的控件</param>
        /// <param name="handler">鼠标单击事件的处理函数</param>
        /// <remarks>
        /// 在控件上MouseDown->MouseLeave->MouseEnter->MouseUp同样有效
        /// </remarks>
        private bool EmulateClickEvent(FrameworkElement control, MouseButtonEventHandler handler)
        {
            if (control == null || handler == null) return false;
            int status = 0;
            control.MouseEnter += delegate { if (status == 0) status = -1; };
            control.MouseLeave += delegate { status = 0; };
            control.MouseLeftButtonDown += delegate { status = 1; };
            control.MouseLeftButtonUp += delegate(object sender, MouseButtonEventArgs e) { if (status > 0) handler(sender, e); status = 0; };
            return true;
        }


        void ImageOnClick(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Method1!");
        }
#endif

    }
}



方法2


界面:

<Window x:Class="ImageClick.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>
        <Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest1" Stretch="Fill" VerticalAlignment="Top" Width="203" MouseLeftButtonUp="imageTest1_MouseLeftButtonUp" />
        <!--<Image Height="227" HorizontalAlignment="Left" Margin="92,42,0,0" Name="imageTest2" Stretch="Fill" VerticalAlignment="Top" Width="203" Source="/ImageClick;component/IMG/imgTest.png" />-->
    </Grid>
</Window>


逻辑代码:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;


namespace ImageClick
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public event MouseButtonEventHandler imageMouseUp;
#if true
        public MainWindow()
        {
            InitializeComponent();


            BitmapImage bi = new BitmapImage();
            // BitmapImage.UriSource must be in a BeginInit/EndInit block.
            bi.BeginInit();
            bi.UriSource = new Uri(@"IMG/imgTest.png", UriKind.RelativeOrAbsolute);
            bi.EndInit();
            imageTest1.Source = bi;

            imageMouseUp += new MouseButtonEventHandler(imageTest1_MouseLeftButtonUp);
            //imageTest1.AddHandler(Image.MouseDownEvent, this.imageMouseDown);
            imageTest1.AddHandler(Image.MouseLeftButtonUpEvent, imageMouseUp, true);

        }


        private void imageTest1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Method2!");
        }


#else
     
#endif


    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值