DragControl

简介: 原文:DragControl 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vblegend_2013/article/details/83791159 ...
原文: 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;
    }
}

 

 

 

 

目录
相关文章
|
13天前
|
存储 人工智能 弹性计算
阿里云弹性计算_加速计算专场精华概览 | 2024云栖大会回顾
2024年9月19-21日,2024云栖大会在杭州云栖小镇举行,阿里云智能集团资深技术专家、异构计算产品技术负责人王超等多位产品、技术专家,共同带来了题为《AI Infra的前沿技术与应用实践》的专场session。本次专场重点介绍了阿里云AI Infra 产品架构与技术能力,及用户如何使用阿里云灵骏产品进行AI大模型开发、训练和应用。围绕当下大模型训练和推理的技术难点,专家们分享了如何在阿里云上实现稳定、高效、经济的大模型训练,并通过多个客户案例展示了云上大模型训练的显著优势。
|
17天前
|
存储 人工智能 调度
阿里云吴结生:高性能计算持续创新,响应数据+AI时代的多元化负载需求
在数字化转型的大潮中,每家公司都在积极探索如何利用数据驱动业务增长,而AI技术的快速发展更是加速了这一进程。
|
8天前
|
并行计算 前端开发 物联网
全网首发!真·从0到1!万字长文带你入门Qwen2.5-Coder——介绍、体验、本地部署及简单微调
2024年11月12日,阿里云通义大模型团队正式开源通义千问代码模型全系列,包括6款Qwen2.5-Coder模型,每个规模包含Base和Instruct两个版本。其中32B尺寸的旗舰代码模型在多项基准评测中取得开源最佳成绩,成为全球最强开源代码模型,多项关键能力超越GPT-4o。Qwen2.5-Coder具备强大、多样和实用等优点,通过持续训练,结合源代码、文本代码混合数据及合成数据,显著提升了代码生成、推理和修复等核心任务的性能。此外,该模型还支持多种编程语言,并在人类偏好对齐方面表现出色。本文为周周的奇妙编程原创,阿里云社区首发,未经同意不得转载。
|
13天前
|
人工智能 运维 双11
2024阿里云双十一云资源购买指南(纯客观,无广)
2024年双十一,阿里云推出多项重磅优惠,特别针对新迁入云的企业和初创公司提供丰厚补贴。其中,36元一年的轻量应用服务器、1.95元/小时的16核60GB A10卡以及1元购域名等产品尤为值得关注。这些产品不仅价格亲民,还提供了丰富的功能和服务,非常适合个人开发者、学生及中小企业快速上手和部署应用。
|
3天前
|
云安全 存储 弹性计算
|
5天前
|
云安全 人工智能 自然语言处理
|
8天前
|
人工智能 自然语言处理 前端开发
用通义灵码,从 0 开始打造一个完整APP,无需编程经验就可以完成
通义灵码携手科技博主@玺哥超carry 打造全网第一个完整的、面向普通人的自然语言编程教程。完全使用 AI,再配合简单易懂的方法,只要你会打字,就能真正做出一个完整的应用。本教程完全免费,而且为大家准备了 100 个降噪蓝牙耳机,送给前 100 个完成的粉丝。获奖的方式非常简单,只要你跟着教程完成第一课的内容就能获得。
|
24天前
|
自然语言处理 数据可视化 前端开发
从数据提取到管理:合合信息的智能文档处理全方位解析【合合信息智能文档处理百宝箱】
合合信息的智能文档处理“百宝箱”涵盖文档解析、向量化模型、测评工具等,解决了复杂文档解析、大模型问答幻觉、文档解析效果评估、知识库搭建、多语言文档翻译等问题。通过可视化解析工具 TextIn ParseX、向量化模型 acge-embedding 和文档解析测评工具 markdown_tester,百宝箱提升了文档处理的效率和精确度,适用于多种文档格式和语言环境,助力企业实现高效的信息管理和业务支持。
3980 5
从数据提取到管理:合合信息的智能文档处理全方位解析【合合信息智能文档处理百宝箱】
|
12天前
|
数据采集 人工智能 API
Qwen2.5-Coder深夜开源炸场,Prompt编程的时代来了!
通义千问团队开源「强大」、「多样」、「实用」的 Qwen2.5-Coder 全系列,致力于持续推动 Open Code LLMs 的发展。
|
13天前
|
算法 安全 网络安全
阿里云SSL证书双11精选,WoSign SSL国产证书优惠
2024阿里云11.11金秋云创季活动火热进行中,活动月期间(2024年11月01日至11月30日)通过折扣、叠加优惠券等多种方式,阿里云WoSign SSL证书实现优惠价格新低,DV SSL证书220元/年起,助力中小企业轻松实现HTTPS加密,保障数据传输安全。
539 3
阿里云SSL证书双11精选,WoSign SSL国产证书优惠