当前位置: 移动技术网 > IT编程>开发语言>.net > WPF实现窗体中的悬浮按钮

WPF实现窗体中的悬浮按钮

2018年11月23日  | 移动技术网IT编程  | 我要评论

吴宇都博客,注射隆鼻大概要多少钱,武汉花园道

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>
view code

控件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);
                }
            }
        }
    }
}
view code

如何使用:

<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>
view code

效果图:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网