吴宇都博客,注射隆鼻大概要多少钱,武汉花园道
wpf实现窗体中的悬浮按钮,按钮可拖动,吸附停靠在窗体边缘。
控件xaml代码:
<button x:class="suncreate.common.controls.floatbutton" 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" mc:ignorable="d" d:designheight="300" d:designwidth="300" width="50" height="50" margin="0" horizontalalignment="left" verticalalignment="top" x:name="btn" loaded="btn_loaded" click="btn_click" > <button.template> <controltemplate> <grid mouseleftbuttondown="border_mouseleftbuttondown"> <border cornerradius="25" background="#022938" opacity="0.2" > </border> <border cornerradius="20" width="40" height="40" background="#022938" opacity="0.3" > </border> <border cornerradius="14" width="28" height="28" background="#b06919" opacity="0.8" > </border> </grid> </controltemplate> </button.template> </button>
控件cs代码:
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); } } } } }
如何使用:
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="clr-namespace:suncreate.common.controls;assembly=suncreate.common.controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" x:class="suncreate.common.controls.demo.mainwindow" title="mainwindow" height="700" width="1200" background="#ff10498c" windowstartuplocation="centerscreen"> <grid> <ui:floatbutton x:name="floatbtn" ></ui:floatbutton> </grid> </window>
效果图:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论