原文: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 issues
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
}

 

 

 

 

DragControl的更多相关文章

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

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

  2. 漫谈可视化Prefuse(三)---Prefuse API数据结构阅读有感

    前篇回顾:上篇<漫谈可视化Prefuse(二)---一分钟学会Prefuse>主要通过一个Prefuse的具体实例了解了构建一个Prefuse application的具体步骤.一个Pre ...

  3. 漫谈可视化Prefuse(二)---一分钟学会Prefuse

    前篇<漫谈可视化Prefuse(一)---从SQL Server数据库读取数据>主要介绍了prefuse如何连接数据库sql server并读取数据进行可视化展现. 回头想想还是应该好好捋 ...

  4. 漫谈可视化Prefuse(一)---从SQL Server数据库读取数据

    上篇<可视化工具solo show-----Prefuse自带例子GraphView讲解>主要介绍了整个Prefuse工具集具有的一些特征.框架的运行流程,分析并展现了官方提供的例子Gra ...

  5. 可视化工具solo show-----Prefuse自带例子GraphView讲解

    2014.10.15日以来的一个月,挤破了头.跑断了腿.伤透了心.吃够了全国最大餐饮连锁店——沙县小吃.其中酸甜苦辣,绝不是三言两语能够说得清道的明的.校招的兄弟姐妹们,你们懂得…… 体会最深的一句话 ...

  6. SilverLight页面跳转(转载)

    // Silverlight页面的跳转 // (Application.Current.RootVisualasIContent).Content=newDragControl(); //Silver ...

  7. TChromeTabs 使用日记

    1.如何让 Tab 在拖放时,拖放图形中带有 TabControl 的内容. 增加 ChromeTabs 的 NeedDragImageControl 事件,并在代码中设置 DragControl 为 ...

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

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

  9. DIV 实现可拖拽 功能(留档)

    //可拖拽 功能 $.fn.extend({    //用法:$(element).jqDrag();    //element需要具备定位属性,需要手动调整层叠样式,这里只是修改鼠标拖动效果    ...

随机推荐

  1. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  2. Python 爬虫从入门到进阶之路(五)

    在之前的文章中我们带入了 opener 方法,接下来我们看一下 opener 应用中的 ProxyHandler 处理器(代理设置). 使用代理IP,这是爬虫/反爬虫的第二大招,通常也是最好用的. 很 ...

  3. SpringBoot学习:获取yml和properties配置文件的内容(转)

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...

  4. Java Tread多线程(2)多线程安全问题

    作者 :卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39348093 本文演示,Tread多线程安全问题,以及几种解决多线程安全方式 ...

  5. 17、网卡驱动程序-DM9000举例

    (参考:cs89x0.c可以参考) DM9000 芯片实现网络功能的基础,在接收数据时采用中断方式,即当有数据到来并在 DM9000 内部 CRC 校验通过后会产生一个接收中断: 网卡驱动程序框架: ...

  6. 对象模型图(OMD)阅读指南

    樱木 原文 对象模型图(OMD)阅读指南(转载) 补充几个名词概念: UML:Unified Modeling Language 统一建模语言,是用来对软件密集系统进行可视化建模的一种语言.UML为面 ...

  7. 《今天你买到票了吗?——从铁道部12306.cn站点漫谈电子商务站点的“海量事务快速处理”系统》

    <今天你买到票了吗?--从铁道部12306.cn站点漫谈电子商务站点的"海量事务快速处理"系统> 首发地址: http://bbs.hpx-party.org/thre ...

  8. Java文档上传问题设计

    近期公司让做一个文档上传的功能,功能描写叙述大概是这样子滴 书籍名称.书籍定价.书籍封面图片(须要上传).文档内容 (须要上传) .还有其它相关的描写叙述信息. 我的设计  表 A  包括以上字段 , ...

  9. Batch Normalization 反向传播(backpropagation )公式的推导

    What does the gradient flowing through batch normalization looks like ? 反向传播梯度下降权值参数更新公式的推导全依赖于复合函数求 ...

  10. [D3] Create Labels from Non-numeric Data with Ordinal Scales in D3 v4

    When your data contains discrete, non-numeric property values that you need to format or convert bef ...