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;
    }
}

 

 

 

 

目录
相关文章
|
10天前
|
弹性计算 人工智能 安全
对话 | ECS如何构筑企业上云的第一道安全防线
随着中小企业加速上云,数据泄露、网络攻击等安全威胁日益严重。阿里云推出深度访谈栏目,汇聚产品技术专家,探讨云上安全问题及应对策略。首期节目聚焦ECS安全性,提出三道防线:数据安全、网络安全和身份认证与权限管理,确保用户在云端的数据主权和业务稳定。此外,阿里云还推出了“ECS 99套餐”,以高性价比提供全面的安全保障,帮助中小企业安全上云。
201862 13
对话 | ECS如何构筑企业上云的第一道安全防线
|
18天前
|
调度 云计算 芯片
云超算技术跃进,阿里云牵头制定我国首个云超算国家标准
近日,由阿里云联合中国电子技术标准化研究院主导制定的首个云超算国家标准已完成报批,不久后将正式批准发布。标准规定了云超算服务涉及的云计算基础资源、资源管理、运行和调度等方面的技术要求,为云超算服务产品的设计、实现、应用和选型提供指导,为云超算在HPC应用和用户的大范围采用奠定了基础。
179634 22
|
5天前
|
弹性计算 人工智能 安全
|
5天前
|
安全 数据安全/隐私保护
阿里云 SASE 2.0 能力迭代|构建一体化办公数据安全解决方案
阿里云SASE能力全新升级,快速构建数据安全治理与运营体系。
1084 5
|
27天前
|
人工智能 自然语言处理 前端开发
从0开始打造一款APP:前端+搭建本机服务,定制暖冬卫衣先到先得
通义灵码携手科技博主@玺哥超carry 打造全网第一个完整的、面向普通人的自然语言编程教程。完全使用 AI,再配合简单易懂的方法,只要你会打字,就能真正做出一个完整的应用。
9829 29
|
5天前
|
搜索推荐 物联网 PyTorch
Qwen2.5-7B-Instruct Lora 微调
本教程介绍如何基于Transformers和PEFT框架对Qwen2.5-7B-Instruct模型进行LoRA微调。
377 34
Qwen2.5-7B-Instruct Lora 微调
|
7天前
|
机器学习/深度学习 人工智能 安全
阿里云先知安全沙龙(武汉站) ——AI赋能软件漏洞检测,机遇, 挑战与展望
本文介绍了漏洞检测的发展历程、现状及未来展望。2023年全球披露的漏洞数量达26447个,同比增长5.2%,其中超过7000个具有利用代码,115个已被广泛利用,涉及多个知名软件和系统。文章探讨了从人工审计到AI技术的应用,强调了数据集质量对模型性能的重要性,并展示了不同检测模型的工作原理与实现方法。此外,还讨论了对抗攻击对模型的影响及提高模型可解释性的多种方法,展望了未来通过任务大模型实现自动化漏洞检测与修复的趋势。
|
12天前
|
机器学习/深度学习 分布式计算 供应链
阿里云先知安全沙龙(上海站) ——大模型基础设施安全攻防
大模型基础设施的安全攻防体系涵盖恶意输入防御和基础设施安全,包括框架、三方库、插件、平台、模型和系统安全。关键漏洞如CVE-2023-6019(Ray框架命令注入)、CVE-2024-5480(PyTorch分布式RPC)及llama.cpp中的多个漏洞,强调了代码安全性的重要性。模型文件安全方面,需防范pickle反序列化等风险,建议使用Safetensors格式。相关实践包括构建供应链漏洞库、智能化漏洞分析和深度检测,确保全方位防护。
|
11天前
|
机器学习/深度学习 人工智能 安全
通义视觉推理大模型QVQ-72B-preview重磅上线
Qwen团队推出了新成员QVQ-72B-preview,这是一个专注于提升视觉推理能力的实验性研究模型。提升了视觉表示的效率和准确性。它在多模态评测集如MMMU、MathVista和MathVision上表现出色,尤其在数学推理任务中取得了显著进步。尽管如此,该模型仍存在一些局限性,仍在学习和完善中。
|
12天前
|
Java Maven
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
在执行Maven项目中的`install`命令时,遇到编译插件版本不匹配的错误。具体报错为:`maven-compiler-plugin:3.13.0`要求Maven版本至少为3.6.3。解决方案是将Maven版本升级到3.6.3或降低插件版本。本文详细介绍了如何下载、解压并配置Maven 3.6.3,包括环境变量设置和IDEA中的Maven配置,确保项目顺利编译。
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案