[WPF系列]Adorner应用-自定义控件ImageHotSpot
引言
项目中有时需要在图片上标注热区,在HTML中有<area>标签,但在WPF中目前还没现成的控件来实现这这一功能。至于图片热区功能有道词典的【图解词典】是个不错的例子,如图1:

图 1
什么是Adorner?
Adoner 是一个绑定到某个UIElement自定义的FrameWorkElemnt 。Adoner被渲染在AdornerLayer,而AdornerLayer又被渲染在被装饰的Element或者Element集合上面。Adorner的渲染独立于它所装饰的UIElement。Adoner通常使用相对坐标来定位到它所装饰的Element上。
Adorner应用场景:
- 为UIElement添加功能句柄,从而使用户能够通过某种方式操作改Element(如Resize,Rotate,Reposition等)
- 可以提供多种状态提示或者响应许多事件
- 在UIElement上叠加一些可视修饰(外观修饰)
- 遮罩UIElement的部分或者全部
本文所涉及的功能一方面是要添加一个装饰元素去标记热区另一方面就是为该热区添加响应事件。
ImageWithHotSpot
先看下效果图:

当用户在图片上点击红色圆圈内部是,将会触发一个点击事件。
实现思路:
首先新建一个用户自定义控件ImageWithHotSpots.xaml并在其中添加一个Image控件来存放要添加热点的图片,为了在Image控件上添加热点Adorner,我新建了个ImageHotSpotAdorner类,为了方便控制热点的形状外观我独立定义了个ImageHotSpot类,另一方面在我们引用该自定义控件时不用关注具体的Adorner,而只需要关注热点本身就可以了。
代码结构

代码:
ImageHotSpot Class code:
public class ImageHotSpot
{
public int Id { get; set; }
public Point Location { get; set; } public double With { get; set; } public double Height { get; set; } public Color BorderColor { get; set; } public double BorderThickness { get; set; } public event EventHandler MouseClick; public void InvokeMouseClickEvent()
{
if (MouseClick != null)
{
MouseClick(this, EventArgs.Empty);
}
}
}
ImageWithHotSpots.xaml
<UserControl x:Class="WPFImageHotSopts.ImageWithHotSpots" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Image Name="ImageForHotSpot"/> </Grid> </UserControl>
ImageWithHotSpots.cs
/// <summary>
/// Interaction logic for ImageWithHotSpots.xaml
/// </summary>
public partial class ImageWithHotSpots : UserControl
{
private List<ImageHotSpot> _imageHotSpotsList = new List<ImageHotSpot>(); public ImageWithHotSpots()
{
InitializeComponent();
Loaded += OnLoaded;
} private void OnLoaded(object sender, RoutedEventArgs args)
{
foreach (var imageHotSpot in _imageHotSpotsList)
{
AddImageHotSpot(imageHotSpot);
}
} public ImageSource Source
{
get { return ImageForHotSpot.Source; }
set { ImageForHotSpot.Source = value; }
} public List<ImageHotSpot> ImageHotSpots {
get { return _imageHotSpotsList; }
} public void AddImageHotSpot(ImageHotSpot hotSpot)
{
var adorner = new ImageHotSpotAdorner(ImageForHotSpot,hotSpot);
_imageHotSpotsList.Add(hotSpot);
var layer = AdornerLayer.GetAdornerLayer(ImageForHotSpot);
layer.Add(adorner);
} public void RemoveImageHotSpot(ImageHotSpot hotSpot)
{
var layer = AdornerLayer.GetAdornerLayer(ImageForHotSpot);
var adorners=layer.GetAdorners(ImageForHotSpot);
if (adorners != null)
{
var adorner = adorners.FirstOrDefault(a => ((ImageHotSpot) a.Tag).Id == hotSpot.Id);
layer.Remove(adorner);
}
} }
ImageHotSpotAdorner.cs
public class ImageHotSpotAdorner : Adorner
{
#region Data
private Ellipse _control;
private Point _location;
private ArrayList _logicalChildren;
#endregion Data
#region Constructor
public ImageHotSpotAdorner(Image adornedImage, ImageHotSpot hotSpot)
: base(adornedImage)
{
if (hotSpot == null)
throw new ArgumentNullException("hotSpot is null");
_location = hotSpot.Location;
_control = new Ellipse();
_control.Height = 0.1 * adornedImage.ActualHeight;
_control.Width = 0.1 * adornedImage.ActualWidth;
_control.Stroke = new SolidColorBrush(hotSpot.BorderColor);
_control.StrokeThickness = hotSpot.BorderThickness;
_control.Fill=new SolidColorBrush(Colors.Transparent);
_control.MouseLeftButtonUp += _control_MouseLeftButtonUp;
_control.MouseEnter += _control_MouseEnter;
_control.MouseLeave += _control_MouseLeave;
_control.Tag = hotSpot;
base.AddLogicalChild(_control);
base.AddVisualChild(_control);
}
void _control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
this.Cursor=Cursors.Arrow;
}
void _control_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
this.Cursor = Cursors.Hand;
}
private void _control_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (_control != null && e.ClickCount == )
{
var hotSpot = (ImageHotSpot)_control.Tag;
hotSpot.InvokeMouseClickEvent();
}
}
#endregion Constructor
#region UpdateTextLocation
public void UpdateTextLocation(Point newLocation)
{
_location = newLocation;
_control.InvalidateArrange();
}
#endregion UpdateTextLocation
#region Measure/Arrange
/// <summary>
/// Allows the control to determine how big it wants to be.
/// </summary>
/// <param name="constraint">A limiting size for the control.</param>
protected override Size MeasureOverride(Size constraint)
{
_control.Measure(constraint);
return _control.DesiredSize;
}
/// <summary>
/// Positions and sizes the control.
/// </summary>
/// <param name="finalSize">The actual size of the control.</param>
protected override Size ArrangeOverride(Size finalSize)
{
Rect rect = new Rect(_location, finalSize);
_control.Arrange(rect);
return finalSize;
}
#endregion Measure/Arrange
#region Visual Children
/// <summary>
/// Required for the element to be rendered.
/// </summary>
protected override int VisualChildrenCount
{
get { return ; }
}
/// <summary>
/// Required for the element to be rendered.
/// </summary>
protected override Visual GetVisualChild(int index)
{
if (index != )
throw new ArgumentOutOfRangeException("index");
return _control;
}
#endregion Visual Children
#region Logical Children
/// <summary>
/// Required for the displayed element to inherit property values
/// from the logical tree, such as FontSize.
/// </summary>
protected override IEnumerator LogicalChildren
{
get
{
if (_logicalChildren == null)
{
_logicalChildren = new ArrayList();
_logicalChildren.Add(_control);
}
return _logicalChildren.GetEnumerator();
}
}
#endregion Logical Children
}
参考文档
MSDN Adorners Overview http://msdn.microsoft.com/en-us/library/ms743737(v=vs.110).aspx
Annotating an Image in WPF http://www.codeproject.com/Articles/20457/Annotating-an-Image-in-WPF
源码下载:WPFImageHotSopts.rar
[WPF系列]Adorner应用-自定义控件ImageHotSpot的更多相关文章
- [WPF系列]-Adorner
简介 通常我们想对现有的控件,做些修饰时我们就会想到一个装饰模式.WPF中也提供了这样的实现思路:通过将Adorner添加到AdornerLayer中来实现装饰现有控件的效果.如图示: 本来T ...
- [WPF系列]从基础起步学习系列计划
引言 WPF技术已经算不什么新技术,一搜一大把关于WPF基础甚至高级的内容.之前工作中一直使用winform所以一直没有深入学习WPF,这次因项目中使用了WPF技术来实现比较酷的展示界面.我在这里只是 ...
- WPF系列 —— 控件添加依赖属性(转)
WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...
- [WPF系列]-ListBox
引言 本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义. Listbox平滑滚动 <ListBox Ite ...
- [WPF系列]-数据邦定之DataTemplate 对分层数据的支持
到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- [WPF系列]-TreeView的常用事项
引言 项目经常会用Treeview来组织一些具有层级结构的数据,本节就将项目使用Treeview常见的问题作一个总结. DataBinding数据绑定 DataTemplate自定义 <Hier ...
- WPF系列教程——(三)使用Win10 Edge浏览器内核 - 简书
原文:WPF系列教程--(三)使用Win10 Edge浏览器内核 - 简书 在需要显示一些 H5网站的时候自带的WebBrowser总是显示不了,WebBrowser使用的是IE内核,许多H5新特性都 ...
- WPF系列教程——(一)仿TIM QQ界面 - 简书
原文:WPF系列教程--(一)仿TIM QQ界面 - 简书 TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM QQ 1. 准备 阅读本文假设你已经有XAML布局的基 ...
随机推荐
- 关于log4net日志的配置流程
最近又重新整理一下log4net日志的配置,现在记录一下流程和一些遇到的问题,以备后续使用,具体的配置参数等信息.此文无,见谅! 1. 下载log4net.dll文件(网上很多,随便找一个!) 2. ...
- Extjs.Button 按钮
Extjs Button 的简单使用 ,同时调用Ajax服务 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh ...
- sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)
sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...
- 浅谈SQL Server中的三种物理连接操作
简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...
- 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令
[源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...
- 连接输出 如果存在在php中多次echo输出js的时候
- 低调奢华 CSS3 transform-style 3D旋转
点击这里查看效果:http://keleyi.com/a/bjad/s89uo4t1.htm 效果图: CSS3 transform-style 属性 以下是代码: <!DOCTYPE html ...
- js 数组去重(7种)
第一次写技术博客,之前总是认为写这些会很浪费时间,还不如多看几篇技术博文.但...但昨天不知道受了什么刺激,好像有什么在驱使着自己要写一样,所以才有了今天的第一篇博文.总觉得应该要坚持这样写下去.初次 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q136-Q138)
Question 136You need to create a custom content type and specify the content type ID.What should you ...
- iOS 系统根据导航栏和状态栏自动修改布局
问题 条件:1.有一个全屏大小的带导航的controller 2.隐藏导航栏,最顶上还会留出状态栏的位置,而不是全屏显示 解决方法 self.automaticallyAdjustsScrollVie ...