最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實現(xiàn)窗體中的懸浮按鈕

 更新時間:2018年11月23日 08:42:11   作者:秋荷雨翔  
這篇文章主要為大家詳細介紹了WPF實現(xiàn)窗體中的懸浮按鈕,具有一定的參考價值,感興趣的小伙伴們可以參考一下

WPF實現(xiàn)窗體中的懸浮按鈕,按鈕可拖動,吸附??吭诖绑w邊緣。

控件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>

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#用Lambda和委托實現(xiàn)模板方法

    C#用Lambda和委托實現(xiàn)模板方法

    C#用Lambda和委托實現(xiàn)模板方法,需要的朋友可以參考一下
    2013-03-03
  • WPF自定義實現(xiàn)雷達圖控件的示例詳解

    WPF自定義實現(xiàn)雷達圖控件的示例詳解

    雷達圖用于表示不同內(nèi)容的占比關(guān)系,在項目中有廣泛的應(yīng)用,但是目前未曾有封裝良好的雷達圖控件,所以本文分享了如何封裝一個通用的雷達圖控件,希望對大家有所幫助
    2023-08-08
  • 詳解C#通過反射獲取對象的幾種方式比較

    詳解C#通過反射獲取對象的幾種方式比較

    本文主要介紹了C#通過反射獲取對象的幾種方式比較,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C#中圖片的Base64編碼與解碼轉(zhuǎn)換詳解

    C#中圖片的Base64編碼與解碼轉(zhuǎn)換詳解

    在C#中,我們可以使用Base64編碼將圖片轉(zhuǎn)換為字符串,也可以將Base64編碼的字符串轉(zhuǎn)換回圖片,這通常用于在需要文本表示圖像數(shù)據(jù)的場合(例如在Web開發(fā)中傳輸圖像數(shù)據(jù)),本文介紹了C#中圖片的Base64編碼與解碼轉(zhuǎn)換,需要的朋友可以參考下
    2024-12-12
  • C#二叉搜索樹插入算法實例分析

    C#二叉搜索樹插入算法實例分析

    這篇文章主要介紹了C#二叉搜索樹插入算法,實例分析了C#二叉樹操作的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C# 大小寫轉(zhuǎn)換(金額)實例代碼

    C# 大小寫轉(zhuǎn)換(金額)實例代碼

    C# 大小寫轉(zhuǎn)換(金額)實例代碼,需要的朋友可以參考一下
    2013-03-03
  • C#基礎(chǔ)語法:可空類型詳解

    C#基礎(chǔ)語法:可空類型詳解

    這篇文章主要介紹了C#基礎(chǔ)語法:可空類型詳解,本文分析了可空類型的源碼、研究了可空類型強制轉(zhuǎn)換為常規(guī)類型、可空類型的運算等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • C#郵件定時群發(fā)工具Atilia用法實例

    C#郵件定時群發(fā)工具Atilia用法實例

    這篇文章主要介紹了C#郵件定時群發(fā)工具Atilia用法,較為詳細的分析了Atilia實現(xiàn)郵件定時群發(fā)功能的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn)

    C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn)

    Bartender是最優(yōu)秀的條碼打印軟件,在企業(yè)里使用非常普遍,本文主要介紹了C#調(diào)用barTender打印標(biāo)簽示例的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 詳解C#實現(xiàn)MD5加密的示例代碼

    詳解C#實現(xiàn)MD5加密的示例代碼

    本篇文章主要介紹了C#實現(xiàn)MD5加密的示例代碼,詳細的介紹了幾種方法,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12

最新評論

如皋市| 巴南区| 民权县| 淳化县| 五原县| 长治市| 房产| 普陀区| 永定县| 清水河县| 台州市| 辉南县| 太仆寺旗| 斗六市| 罗平县| 天门市| 河曲县| 凉山| 深圳市| 织金县| 礼泉县| 景德镇市| 论坛| 灵石县| 仁寿县| 庆城县| 牙克石市| 连山| 靖远县| 墨玉县| 延长县| 卓资县| 亳州市| 洛南县| 中牟县| 大姚县| 广丰县| 金堂县| 永修县| 西丰县| 宁德市|