您的位置:首页 > 其它

WPF 实现按钮的任意拖拽

2015-07-29 10:42 211 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: