WPF-009:WPF窗体的拖动

在使用了WindowStyle="None"时,你可能想自定定义窗体的一些操作,拖动就是常用的。实现起来也很简单:
第一种:
<Window x:Class="TestWPFDrag.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         WindowStyle="None"         Title="MainWindow" Height="451" Width="775">     <Grid>         <Grid.RowDefinitions>             <RowDefinition Height="auto"/>             <RowDefinition Height="*"/>         </Grid.RowDefinitions>         <Grid Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="40" MouseMove="Drag_MouseMove" Background="Transparent">             <Button Content="关 闭" Width="50" Name="btnClose" HorizontalAlignment="Right" Click="btnClose_Click" />         </Grid>     </Grid> </Window>
第二种:
<!--<Window x:Class="TestWPFDrag.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         WindowStyle="None"         Title="MainWindow" Height="451" Width="775"  MouseMove="Drag_MouseMove" >     <Grid>         <Grid.RowDefinitions>             <RowDefinition Height="auto"/>             <RowDefinition Height="*"/>         </Grid.RowDefinitions>         <Grid Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="40" >             <Button Content="关 闭" Width="50" Name="btnClose" HorizontalAlignment="Right" Click="btnClose_Click" />         </Grid>     </Grid> </Window>-->

Drag_MouseMove事件:
   
private void Drag_MouseMove(object sender, MouseEventArgs e)         {             try             {                 if (e.LeftButton == MouseButtonState.Pressed)                 {                     this.DragMove();                 }             }             catch { }         }

就OK了。Drag_MouseMove如果放在了窗体上就意味着鼠标在整个窗体的任何部分都可拖动整个窗体;如果是在某个控件上,只有拖动这个控件才可以拖动窗体,当然如果再某个控件上拖动的话,最好设置该控件的Background="Transparent"。

代码下载:

http://download.csdn.net/detail/yysyangyangyangshan/5168904

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值