using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading;
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.animation;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;
namespace suncreate.common.controls
{
/// <summary>
/// 悬浮按钮
/// </summary>
public partial class floatbutton : button
{
public event eventhandler clickevent;
private bool _move = false;
double _distance = 200;
double _distancenew = 5;
private point _lastpos;
private point _newpos;
private point _oldpos;
public floatbutton()
{
initializecomponent();
}
private void btn_loaded(object sender, routedeventargs e)
{
if (this.parent != null && this.parent is frameworkelement)
{
frameworkelement parent = this.parent as frameworkelement;
double left = parent.actualwidth - this.actualwidth - this._distancenew;
double top = parent.actualheight - this.actualheight - this._distancenew;
this.margin = new thickness(left, top, 0, 0);
}
}
private void border_mouseleftbuttondown(object sender, mousebuttoneventargs e)
{
if (this.parent != null && this.parent is frameworkelement)
{
frameworkelement parent = this.parent as frameworkelement;
_move = true;
_lastpos = e.getposition(parent);
_oldpos = _lastpos;
parent.previewmousemove += (s, ee) =>
{
if (_move)
{
point pos = ee.getposition(parent);
double left = this.margin.left + pos.x - this._lastpos.x;
double top = this.margin.top + pos.y - this._lastpos.y;
this.margin = new thickness(left, top, 0, 0);
_lastpos = e.getposition(parent);
}
};
parent.previewmouseup += (s, ee) =>
{
if (_move)
{
_move = false;
point pos = ee.getposition(parent);
_newpos = pos;
double left = this.margin.left + pos.x - this._lastpos.x;
double top = this.margin.top + pos.y - this._lastpos.y;
double right = parent.actualwidth - left - this.actualwidth;
double bottom = parent.actualheight - top - this.actualheight;
if (left < _distance && top < _distance) //左上
{
left = this._distancenew;
top = this._distancenew;
}
else if (left < _distance && bottom < _distance) //左下
{
left = this._distancenew;
top = parent.actualheight - this.actualheight - this._distancenew;
}
else if (right < _distance && top < _distance) //右上
{
left = parent.actualwidth - this.actualwidth - this._distancenew;
top = this._distancenew;
}
else if (right < _distance && bottom < _distance) //右下
{
left = parent.actualwidth - this.actualwidth - this._distancenew;
top = parent.actualheight - this.actualheight - this._distancenew;
}
else if (left < _distance && top > _distance && bottom > _distance) //左
{
left = this._distancenew;
top = this.margin.top;
}
else if (right < _distance && top > _distance && bottom > _distance) //右
{
left = parent.actualwidth - this.actualwidth - this._distancenew;
top = this.margin.top;
}
else if (top < _distance && left > _distance && right > _distance) //上
{
left = this.margin.left;
top = this._distancenew;
}
else if (bottom < _distance && left > _distance && right > _distance) //下
{
left = this.margin.left;
top = parent.actualheight - this.actualheight - this._distancenew;
}
thicknessanimation marginanimation = new thicknessanimation();
marginanimation.from = this.margin;
marginanimation.to = new thickness(left, top, 0, 0);
marginanimation.duration = timespan.frommilliseconds(200);
storyboard story = new storyboard();
story.fillbehavior = fillbehavior.stop;
story.children.add(marginanimation);
storyboard.settargetname(marginanimation, "btn");
storyboard.settargetproperty(marginanimation, new propertypath("(0)", border.marginproperty));
story.begin(this);
this.margin = new thickness(left, top, 0, 0);
}
};
}
}
private void btn_click(object sender, routedeventargs e)
{
if (_newpos.equals(_oldpos))
{
if (clickevent != null)
{
clickevent(sender, e);
}
}
}
}
}