图片点击事件
2015-07-27 09:33
381 查看
图片点击事件
方法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>
逻辑代码:
方法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 += n 4000 ew 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 } }
相关文章推荐
- php使用Image Magick将PDF文件转换为JPG文件的方法
- JavaScript与Image加载事件(onload)、加载状态(complete)
- Track Image Loading效果代码分析
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- js的image onload事件使用遇到的问题
- javascript从image转换为base64位编码的String
- Thinkphp调用Image类生成缩略图的方法
- asp.net显示图片到指定的Image控件中 具体实现
- javascript下拉框选项单击事件的例子分享
- python使用Image处理图片常用技巧分析
- Image Upload based on jQuery
- import com.sun.image.codec.jpeg.JPEGCodec;
- SCOM PowerShell 命令使用指南 - 03 (ManagementServer)
- SCOM PowerShell 命令使用指南 - 06 (Agent)
- Uploading an image to a webserver – Android/Java
- iOS图片拉伸技巧—— resizableImageWithCapInsets
- slide-show image from MongoDB GridFS chunks @ D...
- image lazyload
- 图像的放大与缩小(2)——双线性插值放大与均值缩小
- 用JWidow写一个可以在桌面上拖动的小时钟