WPF 实现按钮的任意拖拽

WPF 实现按钮的任意拖拽
在不影响主窗体布局的情况下,任意拖动一个Button。

主要是通过创建无边框透明子窗体实现,前端代码如下:

<Window x:Class="topmosttest.Topmost"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="topmost" Height="98" Width="76"  WindowStartupLocation="CenterOwner" Visibility="Visible" Topmost="True" WindowStyle="None"
        ShowInTaskbar="False" AllowsTransparency="True" >
    <Canvas x:Name="canvas1">
        <Button Content="TopMost" Height="98" Width="76">Button>
        <Rectangle MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown1" Height="98"
                   Width="76" Cursor="SizeAll" Fill="Transparent" >Rectangle>
    Canvas>
Window>

后端代码调用DragMove()方法:

public partial class Topmost : Window
    {
        public Topmost()
        {
            InitializeComponent();
        }

        private void UIElement_OnMouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }

你可能感兴趣的:(工作点滴)