拖动一个无边框窗口

代码

DragBorderlessWindowSample.xaml

  1. <Window
  2. x:Class="Drag_a_Borderless_Window_Sample.DragBorderlessWindowSample"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7. Title="DragBorderlessWindowSample"
  8. Width="800"
  9. Height="450"
  10. AllowsTransparency="True"
  11. Background="Gray"
  12. MouseLeftButtonDown="Window_MouseLeftButtonDown"
  13. WindowStyle="None"
  14. mc:Ignorable="d">
  15. <Grid>
  16. <Button
  17. Name="btnClose"
  18. Width="75"
  19. HorizontalAlignment="Center"
  20. VerticalAlignment="Center"
  21. Click="BtnClose_OnClick"
  22. Content="Close" />
  23. </Grid>
  24. </Window>

DragBorderlessWindowSample.xaml

  1. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  2. {
  3. this.DragMove();
  4. }
  5. private void BtnClose_OnClick(object sender, RoutedEventArgs e)
  6. {
  7. Close();
  8. }

效果

borderless sample.gif