using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Demo19_3 {
/// <summary>
/// 可浮动窗体
/// </summary>
public abstract class FloatableWindow : Canvas {
/// <summary>
/// 3D整型坐标点
/// </summary>
public struct Point3D {
public int X,
Y,
Z;
public Point3D(int x, int y, int z) {
X = x;
Y = y;
Z = z;
}
}
/// <summary>
/// 获取或设置中心
/// </summary>
public virtual Point Center { get; set; }
/// <summary>
/// 获取或设置X、Y坐标
/// </summary>
public virtual Point Coordinate {
get { return new Point(Canvas.GetLeft(this) + Center.X, Canvas.GetTop(this) + Center.Y); }
set { Canvas.SetLeft(this, value.X - Center.X); Canvas.SetTop(this, value.Y - Center.Y); }
}
/// <summary>
/// 获取或设置Z层次深度
/// </summary>
public int Z {
get { return Canvas.GetZIndex(this); }
set { Canvas.SetZIndex(this, value); }
}
ContentControl HeadContent = new ContentControl(),
BodyContent = new ContentControl(),
FootContent = new ContentControl(),
CloserContent = new ContentControl();
/// <summary>
/// 设置头部对象
/// </summary>
public object Head {
set { HeadContent.Content = value; }
}
/// <summary>
/// 设置身体对象
/// </summary>
public object Body {
set { BodyContent.Content = value; }
}
/// <summary>
/// 设置脚部对象
/// </summary>
public object Foot {
set { FootContent.Content = value; }
}
/// <summary>
/// 设置关闭按钮对象
/// </summary>
public object Closer {
set { CloserContent.Content = value; }
}
public double HeadHeight {
get { return HeadContent.Height; }
set { HeadContent.Height = value; }
}
public double BodyHeight {
get { return BodyContent.Height; }
set { BodyContent.Height = value; }
}
public double FootHeight {
get { return FootContent.Height; }
set { FootContent.Height = value; }
}
/// <summary>
/// 设置头部空间位置
/// </summary>
public Point3D HeadPosition {
set {
Canvas.SetLeft(HeadContent, value.X);
Canvas.SetTop(HeadContent, value.Y);
Canvas.SetZIndex(HeadContent, value.Z);
}
}
/// <summary>
/// 设置身体空间位置
/// </summary>
public Point3D BodyPosition {
set {
Canvas.SetLeft(BodyContent, value.X);
Canvas.SetTop(BodyContent, value.Y);
Canvas.SetZIndex(BodyContent, value.Z);
}
}
/// <summary>
/// 设置脚部空间位置
/// </summary>
public Point3D FootPosition {
set {
Canvas.SetLeft(FootContent, value.X);
Canvas.SetTop(FootContent, value.Y);
Canvas.SetZIndex(FootContent, value.Z);
}
}
/// <summary>
/// 设置关闭按钮空间位置
/// </summary>
public Point3D CloserPosition {
set {
Canvas.SetLeft(CloserContent, value.X);
Canvas.SetTop(CloserContent, value.Y);
Canvas.SetZIndex(CloserContent, value.Z);
}
}
/// <summary>
/// 动画飞向
/// </summary>
/// <param name="fromX">起点X</param>
/// <param name="toX">目标X</param>
/// <param name="fromY">起点Y</param>
/// <param name="toY">目标Y</param>
/// <param name="speed">速度倍数</param>
public void AnimationFlyTo(double fromX, double toX, double fromY, double toY, double speed) {
Storyboard storyboard = new Storyboard();
storyboard.Completed += (s, e) => {
Storyboard sb = s as Storyboard;
sb = null;
};
int duration = Convert.ToInt32(Math.Sqrt(Math.Pow((toX - fromX), 2) + Math.Pow((toY - fromY), 2)) * speed);
DoubleAnimation xAnimation = new DoubleAnimation() {
From = fromX,
To = toX,
Duration = new Duration(TimeSpan.FromMilliseconds(duration))
};
Storyboard.SetTarget(xAnimation, this);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(xAnimation);
DoubleAnimation yAnimation = new DoubleAnimation() {
From = fromY,
To = toY,
Duration = new Duration(TimeSpan.FromMilliseconds(duration))
};
Storyboard.SetTarget(yAnimation, this);
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(yAnimation);
storyboard.Begin();
}
/// <summary>
/// 关闭事件
/// </summary>
public event MouseButtonEventHandler Closed;
static int z; //层次
public FloatableWindow() {
this.Children.Add(HeadContent);
this.Children.Add(BodyContent);
this.Children.Add(FootContent);
this.Children.Add(CloserContent);
//设置可拖动方法
bool isMouseCaptured = false; //是否捕获了鼠标
Point clickPoint = new Point(); //点击点
HeadContent.MouseLeftButtonDown += (s, e) => {
z++;
Canvas.SetZIndex(this, z);
HeadContent.CaptureMouse();
isMouseCaptured = true;
clickPoint = e.GetPosition(s as UIElement);
e.Handled = true;
};
HeadContent.MouseLeftButtonUp += (s, e) => {
HeadContent.ReleaseMouseCapture();
isMouseCaptured = false;
e.Handled = true;
};
HeadContent.MouseMove += (s, e) => {
if (isMouseCaptured) {
TransformGroup transformGroup = this.RenderTransform as TransformGroup;
if (transformGroup == null) {
transformGroup = new TransformGroup();
}
if (transformGroup != null) {
transformGroup.Children.Add(new TranslateTransform() {
X = e.GetPosition(this).X - clickPoint.X,
Y = e.GetPosition(this).Y - clickPoint.Y
});
this.RenderTransform = transformGroup;
}
}
};
CloserContent.MouseLeftButtonDown += (s, e) => {
if (Closed != null) {
Closed(this, e);
};
e.Handled = true;
};
}
}
}