当前位置:网站首页 / WPF / 正文

WPF4.5 拖拽文件解密- 窗体外文件到窗体内,窗体控件之间传递,以及动画

时间:2015年05月27日 | 作者 : aaronyang | 分类 : WPF | 浏览: 4385次 | 评论 0

效果代码;

331.gif

界面代码:

<Window x:Class="blend2.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Aaronyang的拖拽demo" Height="500" Width="700" AllowDrop="True" Drop="Window_Drop">
	<Window.Resources>
	
		<Storyboard x:Key="recDragEnter" RepeatBehavior="Forever" AutoReverse="True">
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recTarget">
				<EasingColorKeyFrame KeyTime="0" Value="#FFFFC709"/>
				<EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FFF3E5B3"/>
				<EasingColorKeyFrame KeyTime="0:0:1.2" Value="Red"/>
				<EasingColorKeyFrame KeyTime="0:0:2" Value="#FFBAF1EF"/>
				<EasingColorKeyFrame KeyTime="0:0:2.9" Value="#FF0291F3"/>
				<EasingColorKeyFrame KeyTime="0:0:3.7" Value="#FF472189"/>
				<EasingColorKeyFrame KeyTime="0:0:4.6" Value="#FFFF00AE"/>
				<EasingColorKeyFrame KeyTime="0:0:5.4" Value="#FFFF002E"/>
			</ColorAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="OnDragLeave1">
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recTarget">
				<EasingColorKeyFrame KeyTime="0" Value="Black"/>
			</ColorAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="OnDrop1" AutoReverse="False">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="recTarget">
				<EasingDoubleKeyFrame KeyTime="0" Value="3"/>
			</DoubleAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="recTarget">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.5">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseOut"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseIn"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
			</DoubleAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="recTarget">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.5">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseOut"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseIn"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="OnDrop2" AutoReverse="False">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="3"/>
			</DoubleAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.5">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseOut"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseIn"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
			</DoubleAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.5">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseOut"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1">
					<EasingDoubleKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseIn"/>
					</EasingDoubleKeyFrame.EasingFunction>
				</EasingDoubleKeyFrame>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="OnWindowDragEnter">
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
				<EasingColorKeyFrame KeyTime="0" Value="White"/>
				<EasingColorKeyFrame KeyTime="0:0:0.3" Value="#FF646363"/>
			</ColorAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="3"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="3"/>
			</DoubleAnimationUsingKeyFrames>
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recSource">
				<EasingColorKeyFrame KeyTime="0:0:0.1" Value="#FFF1DD59"/>
				<EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FFF1DD59"/>
			</ColorAnimationUsingKeyFrames>
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recTarget">
				<EasingColorKeyFrame KeyTime="0" Value="#FFFFC709"/>
				<EasingColorKeyFrame KeyTime="0:0:0.4" Value="#FFFFC709"/>
			</ColorAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="recTarget">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0.015"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="Storyboard1">
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.SnapsToDevicePixels)" Storyboard.TargetName="recSource">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
				<DiscreteBooleanKeyFrame KeyTime="0:0:1" Value="False"/>
			</BooleanAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="recSource">
				<SplineDoubleKeyFrame KeyTime="0" Value="0"/>
				<SplineDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
			</DoubleAnimationUsingKeyFrames>
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recSource">
				<SplineColorKeyFrame KeyTime="0" Value="#FFFFB900"/>
				<SplineColorKeyFrame KeyTime="0:0:1" Value="#FFA03A35"/>
			</ColorAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(BlurEffect.Radius)" Storyboard.TargetName="recSource">
				<SplineDoubleKeyFrame KeyTime="0" Value="0"/>
				<SplineDoubleKeyFrame KeyTime="0:0:1" Value="7"/>
			</DoubleAnimationUsingKeyFrames>
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.ClipToBounds)" Storyboard.TargetName="recSource">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
				<DiscreteBooleanKeyFrame KeyTime="0:0:1" Value="False"/>
			</BooleanAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="Storyboard2" RepeatBehavior="Forever" AutoReverse="True">
			<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="recSource">
				<EasingColorKeyFrame KeyTime="0" Value="Black"/>
				<EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FFFFC500"/>
			</ColorAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="4"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="OnWindowDragLeave">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(BlurEffect.Radius)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="Storyboard3">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(BlurEffect.Radius)" Storyboard.TargetName="recSource">
				<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
	</Window.Resources>
	<Window.Triggers>
		<EventTrigger RoutedEvent="UIElement.DragEnter" SourceName="recTarget">
            <BeginStoryboard x:Name="OnDragEnter1_BeginStoryboard" Storyboard="{StaticResource recDragEnter}"/>
		</EventTrigger>
		<EventTrigger RoutedEvent="UIElement.DragLeave" SourceName="recTarget">
			<BeginStoryboard x:Name="OnDragLeave1_BeginStoryboard" Storyboard="{StaticResource OnDragLeave1}"/>
		</EventTrigger>
		<EventTrigger RoutedEvent="UIElement.Drop" SourceName="recTarget">
			<BeginStoryboard x:Name="OnDrop1_BeginStoryboard" Storyboard="{StaticResource OnDrop1}"/>
			<StopStoryboard BeginStoryboardName="OnDragEnter1_BeginStoryboard"/>
		</EventTrigger>
	</Window.Triggers>
    <Grid x:Name="grid" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="108*"/>
            <ColumnDefinition Width="511*"/>
            <ColumnDefinition Width="73*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="69*"/>
            <RowDefinition Height="353*"/>
            <RowDefinition Height="47*"/>
        </Grid.RowDefinitions>
        <Rectangle x:Name="recSource"  MouseDown="recSource_MouseDown" Grid.Column="1" HorizontalAlignment="Left" Height="320" Margin="68,10,0,-250" Grid.Row="1" Stroke="Black" VerticalAlignment="Top" Width="160" Grid.RowSpan="2" >
        	<Rectangle.Effect>
        		<BlurEffect/>
        	</Rectangle.Effect>
            <Rectangle.Fill>
                <ImageBrush ImageSource="/blend2;component/imgs/dragpic.jpg" Stretch="UniformToFill"/>
            </Rectangle.Fill>
			<Rectangle.RenderTransform>
        		<TransformGroup>
        			<ScaleTransform/>
        			<SkewTransform/>
        			<RotateTransform/>
        			<TranslateTransform/>
        		</TransformGroup>
        	</Rectangle.RenderTransform>
        </Rectangle>
        <Rectangle x:Name="recTarget" Grid.Column="1" Drop="recTarget_Drop" DragEnter="recTarget_DragEnter"  AllowDrop="True" HorizontalAlignment="Left" Height="320" Margin="296,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="160" Stroke="#FFFFC709" StrokeThickness="3" Fill="#FFB4AFAF" RenderTransformOrigin="0.5,0.5" >
        	<Rectangle.RenderTransform>
        		<TransformGroup>
        			<ScaleTransform/>
        			<SkewTransform/>
        			<RotateTransform/>
        			<TranslateTransform/>
        		</TransformGroup>
        	</Rectangle.RenderTransform>
        </Rectangle>
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace blend2
{
    /// <summary>
    /// Window4.xaml 的交互逻辑
    /// </summary>
    public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
        }
      
        private void recSource_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rec=sender as Rectangle;
            if(rec!=null){
                DataObject data = new DataObject(typeof(Rectangle), rec);
                DragDrop.DoDragDrop(rec, data, DragDropEffects.Copy);
            }
        }


        private void recTarget_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Rectangle)))
            {
                Rectangle dataobj = e.Data.GetData(typeof(Rectangle)) as Rectangle;
                recTarget.Fill = dataobj.Fill;
                this.recSource.Fill = null;
            }
          
           
        }

        private void recTarget_DragEnter(object sender, DragEventArgs e)
        {
            //string[] sg = e.Data.GetFormats();
      
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] a=(string[])e.Data.GetData(DataFormats.FileDrop);
                ImageBrush brush = new ImageBrush();
                string a1=a[0];
                brush.ImageSource = new BitmapImage(new Uri(a1, UriKind.RelativeOrAbsolute));
                this.recSource.Fill = brush;
            }

        }

        //btn.SetResourceRefrence(Button.StyleProperty,"SimpleButton") ;
        //control.style=Resources["MyButton"] as style
    }

    #region 序列化
    public class SerializerDeserialize
    {

        public static MemoryStream Serializer(object obj)
        {

            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化object对象  

            MemoryStream ms = new MemoryStream();//创建内存流对象  

            formatter.Serialize(ms, obj);//把object对象序列化到内存流  

            //byte[] buffer = ms.ToArray();//把内存流对象写入字节数组  

            //ms.Close();//关闭内存流对象  

            //ms.Dispose();//释放资源  

            //MemoryStream msNew = new MemoryStream();

            //GZipStream gzipStream = new GZipStream(msNew, CompressionMode.Compress, true);//创建压缩对象  

            //gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件  

            //gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整              

            //gzipStream.Dispose();//释放对象  

            //msNew.Close();

            //msNew.Dispose();

            return ms;

        }

        /// <summary>  

        /// 反序列化压缩的object  

        /// </summary>  

        /// <param name="_filePath"></param>  

        /// <returns></returns>  

        public static object Deserialize(byte[] bytes)
        {

            MemoryStream msNew = new MemoryStream(bytes);

            msNew.Position = 0;

            GZipStream gzipStream = new GZipStream(msNew, CompressionMode.Decompress);//创建解压对象  

            byte[] buffer = new byte[4096];//定义数据缓冲  

            int offset = 0;//定义读取位置  

            MemoryStream ms = new MemoryStream();//定义内存流  

            while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
            {

                ms.Write(buffer, 0, offset);//解压后的数据写入内存流  

            }

            BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化object对象  

            ms.Position = 0;//设置内存流的位置  

            object obj;

            try
            {

                obj = (object)sfFormatter.Deserialize(ms);//反序列化  

            }

            catch
            {

                throw;

            }

            finally
            {

                ms.Close();//关闭内存流  

                ms.Dispose();//释放资源  

            }

            gzipStream.Close();//关闭解压缩流  

            gzipStream.Dispose();//释放资源  

            msNew.Close();

            msNew.Dispose();

            return obj;

        }

    } 
    #endregion


}


推荐您阅读更多有关于“WPF4.5,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表