原文:DragControl

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vblegend_2013/article/details/83791159
<UserControl x:Class="ImageView.DragControl"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" xmlns:local="clr-namespace:ImageView"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><UserControl.Resources><Style TargetType="{x:Type Thumb}" x:Key="CornerThumbStyle"><Setter Property="Width" Value="{Binding CornerWidth, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/><Setter Property="Height" Value="{Binding CornerWidth, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/><Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/><Setter Property="BorderThickness" Value="3"/><Setter Property="Background" Value="Transparent"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Thumb}"><Border SnapsToDevicePixels="True"Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"/></ControlTemplate></Setter.Value></Setter></Style><Style TargetType="{x:Type Thumb}" x:Key="AreaThumbStyle"><Setter Property="BorderBrush" Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/><Setter Property="Background" Value="Transparent"/><Setter Property="Padding" Value="0"/><Setter Property="Margin" Value="0"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Thumb}"><Rectangle Margin="0" Fill="{TemplateBinding Background}" SnapsToDevicePixels="True"Stroke="{TemplateBinding BorderBrush}" Stretch="Fill"StrokeThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness.Top, Mode=OneWay}"/></ControlTemplate></Setter.Value></Setter></Style></UserControl.Resources><Grid x:Name="PART_MainGrid"><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="*"/><RowDefinition Height="*"/><RowDefinition Height="*"/></Grid.RowDefinitions><!--MiddleCenter--><Thumb Tag="8"Focusable="True"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"BorderThickness="1"Style="{StaticResource AreaThumbStyle}"  Grid.RowSpan="3" Grid.ColumnSpan="3" Cursor="SizeAll" /><!--TopLeft--><Thumb Tag="7"           Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"BorderThickness="2.5"Margin="-2,-2,0,0"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left"   VerticalAlignment="Top"    Cursor="SizeNWSE"/><!--TopCenter--><Thumb Tag="0"         Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"BorderThickness="2.5"Margin="0,-2,0,0"Style="{StaticResource CornerThumbStyle}"Grid.Row="0" Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Cursor="SizeNS"/><!--TopRight--><Thumb Tag="1"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"Margin="0,-2,-2,0"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"BorderThickness="2.5"Style="{StaticResource CornerThumbStyle}" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right"  VerticalAlignment="Top"    Cursor="SizeNESW"/><!--MiddleLeft--><Thumb Tag="6"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"Margin="-2,0,0,0"BorderThickness="2.5"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left"   VerticalAlignment="Center" Cursor="SizeWE"/><!--MiddleRight--><Thumb Tag="2"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"Margin="0,0,-2,0"BorderThickness="2.5"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right"  VerticalAlignment="Center" Cursor="SizeWE"/><!--BottomLeft--><Thumb Tag="5"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"Margin="-2,0,0,-2"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"BorderThickness="2.5"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left"   VerticalAlignment="Bottom" Cursor="SizeNESW"/><!--BottomCenter--><Thumb Tag="4"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"Margin="0,0,0,-2"BorderThickness="2.5"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Cursor="SizeNS"/><!--BottomRight--><Thumb Tag="3"        Visibility="{Binding ThumbVisible, UpdateSourceTrigger=PropertyChanged}"BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=BorderBrush}"BorderThickness="2.5"Margin="0,0,-2,-2"Opacity="{Binding ThumbOpacity, UpdateSourceTrigger=PropertyChanged}"Style="{StaticResource CornerThumbStyle}" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Cursor="SizeNWSE"/></Grid>
</UserControl>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;namespace ImageView
{/// <summary>/// DragControl.xaml 的交互逻辑/// </summary>public partial class DragControl : UserControl, INotifyPropertyChanged{public DragControl(){InitializeComponent();AddHandler(Thumb.DragStartedEvent, new RoutedEventHandler(Drag_Started));AddHandler(Thumb.DragDeltaEvent, new DragDeltaEventHandler(Drag_Delta));AddHandler(Thumb.DragCompletedEvent, new RoutedEventHandler(Drag_Completed));AddHandler(Thumb.GotFocusEvent, new RoutedEventHandler(OnGotFocus));AddHandler(Thumb.LostFocusEvent, new RoutedEventHandler(OnLostFocus));this.DataContext = this;ThumbOpacity = 1.0f;}private void OnLostFocus(object sender, RoutedEventArgs e){ThumbVisible = Visibility.Collapsed;}private void OnGotFocus(object sender, RoutedEventArgs e){ThumbVisible = Visibility.Visible;}private void Drag_Started(object sender, RoutedEventArgs e){ThumbOpacity = 0.1;this.RaiseEvent( new RoutedEventArgs() { Source = this, RoutedEvent = OnDragStartedEvent });}private void Drag_Delta(object sender, DragDeltaEventArgs e){Thumb thumb = e.OriginalSource as Thumb;if (thumb == null){return;}Int32 DragDirection = -1;if (!Int32.TryParse((String)thumb.Tag, out DragDirection)){return;}double VerticalChange = e.VerticalChange;double HorizontalChange = e.HorizontalChange;Double left = Canvas.GetLeft(this);Double top = Canvas.GetTop(this);Double width = this.Width;Double height = this.Height;switch (DragDirection){case 7:height = this.Height - VerticalChange < 1 ? 1 : this.Height - VerticalChange;top = Canvas.GetTop(this) + (this.Height - height);width = this.Width - HorizontalChange < 1 ? 1 : this.Width - HorizontalChange;left = Canvas.GetLeft(this) + (this.Width - width);break;case 0:height = this.Height - VerticalChange < 1 ? 1 : this.Height - VerticalChange;top = Canvas.GetTop(this) + (this.Height - height);break;case 1:height = this.Height - VerticalChange < 1 ? 1 : this.Height - VerticalChange;top = Canvas.GetTop(this) + (this.Height - height);width = this.Width + HorizontalChange;break;case 6:width = this.Width - HorizontalChange < 1 ? 1 : this.Width - HorizontalChange;left = Canvas.GetLeft(this) + (this.Width - width);break;case 8:left = Canvas.GetLeft(this) + HorizontalChange;top = Canvas.GetTop(this) + VerticalChange;break;case 2:width = this.Width + HorizontalChange;break;case 5:width = this.Width - HorizontalChange < 1 ? 1 : this.Width - HorizontalChange;left = Canvas.GetLeft(this) + (this.Width - width);height = this.Height + VerticalChange;break;case 4:height = this.Height + VerticalChange;break;case 3:width = this.Width + HorizontalChange;height = this.Height + VerticalChange;break;default:break;}width = width < 1 ? 1 : width;this.Width = width;height = height < 1 ? 1 : height;this.Height = height;Canvas.SetTop(this, top);Canvas.SetLeft(this, left);this.RaiseEvent(new RoutedEventArgs() { Source = this, RoutedEvent = OnDragDeltaEvent });//e.Handled = true;}private void Drag_Completed(object sender, RoutedEventArgs e){Rect NewBound = new Rect{Y = Canvas.GetTop(this),X = Canvas.GetLeft(this),Width = this.ActualWidth,Height = this.ActualHeight};this.RaiseEvent(new RoutedEventArgs() { Source = this, RoutedEvent = OnDragCompletedEvent });ThumbOpacity = 1;e.Handled = true;}/// <summary>/// 声明路由事件/// 参数:要注册的路由事件名称,路由事件的路由策略,事件处理程序的委托类型(可自定义),路由事件的所有者类类型/// </summary>public static readonly RoutedEvent OnDragStartedEvent = EventManager.RegisterRoutedEvent("OnDragStarted", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DragControl));/// <summary>/// 处理各种路由事件的方法 /// </summary>public event RoutedEventHandler OnDragStarted{//将路由事件添加路由事件处理程序add { AddHandler(OnDragStartedEvent, value, false); }//从路由事件处理程序中移除路由事件remove { RemoveHandler(OnDragStartedEvent, value); }}/// <summary>/// 声明路由事件/// 参数:要注册的路由事件名称,路由事件的路由策略,事件处理程序的委托类型(可自定义),路由事件的所有者类类型/// </summary>public static readonly RoutedEvent OnDragCompletedEvent = EventManager.RegisterRoutedEvent("OnDragCompleted", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DragControl));/// <summary>/// 处理各种路由事件的方法 /// </summary>public event RoutedEventHandler OnDragCompleted{//将路由事件添加路由事件处理程序add { AddHandler(OnDragCompletedEvent, value, false); }//从路由事件处理程序中移除路由事件remove { RemoveHandler(OnDragCompletedEvent, value); }}/// <summary>/// 声明路由事件/// 参数:要注册的路由事件名称,路由事件的路由策略,事件处理程序的委托类型(可自定义),路由事件的所有者类类型/// </summary>public static readonly RoutedEvent OnDragDeltaEvent = EventManager.RegisterRoutedEvent("OnDragDelta", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(DragControl));/// <summary>/// 处理各种路由事件的方法 /// </summary>public event RoutedEventHandler OnDragDelta{//将路由事件添加路由事件处理程序add { AddHandler(OnDragDeltaEvent, value, false); }//从路由事件处理程序中移除路由事件remove { RemoveHandler(OnDragDeltaEvent, value); }}public Double ThumbOpacity{get{return __thumbopacity;}set{__thumbopacity = value;RaisePropertyChanged("ThumbOpacity");}}private Double __thumbopacity { get; set; }public Visibility ThumbVisible{get{return __thumbvisible;}set{__thumbvisible = value;RaisePropertyChanged("ThumbVisible");}}private Visibility __thumbvisible { get; set; }protected void RaisePropertyChanged(string propertyName){// take a copy to prevent thread issuesPropertyChangedEventHandler handler = PropertyChanged;if (handler != null){handler(this, new PropertyChangedEventArgs(propertyName));}}public event PropertyChangedEventHandler PropertyChanged;}
}

DragControl相关推荐

  1. SilverLight 4页面跳转大全

    http://blog.csdn.net/lihaiyin/article/details/5674766 //Silverlight页面的跳转  //(Application.Current.Roo ...

  2. 漫谈可视化Prefuse(四)---被玩坏的Prefuse API

    这个双12,别人都在抢红包.逛淘宝.上京东,我选择再续我的"漫谈可视化"系列(好了,不装了,其实是郎中羞涩...) 上篇<漫谈可视化Prefuse(三)---Prefuse ...

  3. prefuse学习(二)显示一张图

    1.  把数据以点连线的方式在画面中显示 2.  数据按照数据的性别属性使用不同的颜色 3.  鼠标左键可以把图在画面中拖动 4.  鼠标右键可以把图放大或者缩小 5.  鼠标单击某个数据上,该数据点 ...

  4. SilverLight 4页面跳转大全(转载)

    //Silverlight页面的跳转 //(Application.Current.RootVisual as IContent).Content = new DragControl(); //普通页 ...

  5. c#ovalshape_【原创】C# 实现拖拉控件改变位置与大小(SamWang)(附源代码下载)

    前言: 很多时候我们需要在运行时,动态地改变控件的位置以及大小,以获得更好的布局.比如说实际项目中的可自定义的报表.可自定义的单据等诸如此类.它们有个特点就是允许客户或者二次开发人员设计它们需要的界面 ...

  6. 前端开发者常用的9个JavaScript图表库

    当前,数据可视化已经成为数据科学领域非常重要的一部分.不同网络系统中产生的数据,都需要经过适当的可视化处理,以便更好的呈现给用户读取和分析. 对任何一个组织来说,如果能够充分的获取数据.可视化数据和分 ...

  7. Scroller的使用及解析(滑动删除)

    在学习这个之前,你首先要了解android的消息机制,Android的坐标系统,android View绘制流程 scrollBy 个 scrollTo的区别 scrollTo:相对View的初始位置 ...

  8. html5 canvas文字标签云3D旋转动画特效

    tagcanvas.min.js下载 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  9. 【Autodesk Maya】ptyhon 常用命令

    常用命令 这三个绝对是最常用的命令了 help 列出命令的详细说明 dir 列出对象的属性及方法 print 打印 然后下面是我整理的一些常用命令 这部分是cmds与pymel都可直接调用的 file ...

最新文章

  1. php远程文件包含攻击,PHP “is_a()”函数远程文件包含漏洞
  2. kafka 异常:ERROR Failed to clean up log for __consumer_offsets-30 in dir /tmp/kafka-logs due to IOExce
  3. eclipse build慢问题
  4. 《机器学习实战》第二章学习笔记:K-近邻算法(代码详解)
  5. Linux高级编程--06.进程概述
  6. 函数头模板_Python新手爬虫,爬取PPT模板
  7. 十六进制转字符串 java_JAVA十六进制与字符串的转换方法
  8. 37,字节跳动Python面试
  9. 如何向icloud上传文件_扔掉百度网盘,这两招教你用好 iCloud 分享文件
  10. 【旧文章搬运】暴力的查进程方法
  11. 计算机屏幕出现条纹w7,电脑重装win7后屏幕出现条纹怎么办
  12. package.json 文件详解
  13. 【图像加密】基于多种混沌序列Logistic+Tent+Kent+Hent实现图像加密与解密含Matlab源码
  14. wireshark抓包:分析阿里小蜜网络通信方式
  15. 谈谈一只菜鸟转行Erlang游戏服务端的经历(希望大佬指导,也希望我的经历能给一些还未毕业的同学或者正在迷茫自己工作内容的同学一些感触)
  16. 微信公众号开发----生成带参数的临时二维码
  17. Springcloud个人总结
  18. 怎么做接口自动化的?
  19. 安卓海豚wii模拟器_dolphin模拟器安卓版
  20. iftop监控网卡实时流量

热门文章

  1. require()与 require_once()、 include与include_once()
  2. EL表达式(Exprission language)
  3. 幻想英雄2-战神再起折扣号新手入门攻略
  4. getComputedStyle与currentStyle获取样式(style/class)
  5. Bitmap简单使用及简单解析
  6. Altium Designer中PCB关于Import Changes报错最终解决方案
  7. Mybatis-generator,自动生成文件.问题及解决
  8. 清华大学崔鹏:因果推断技术最新的发展趋势及在推荐系统中的应用
  9. 通俗易懂!视觉slam第二部分——salm过程简介
  10. 机器学习-python的工作目录