WPF AllowsTransparency DragMove

 
//xaml
<Window x:Class="WpfApp191.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp191"
        mc:Ignorable="d" AllowsTransparency="True"
        WindowStyle="None" 
        Title="MainWindow" Height="450" Width="800">
    <Window.Background>
        <ImageBrush ImageSource="324.jpg"/>
    </Window.Background>
    <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <Rectangle RadiusX="30" RadiusY="30">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="DarkBlue" Offset="0"/>
                    <GradientStop Color="#80000080" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock TextAlignment="Center" VerticalAlignment="Top"
                       Margin="4" Text="My Window Title"
                       FontSize="16" Foreground="White" FontWeight="Bold"/>
        <Button Content="X" HorizontalAlignment="Right" Click="CloseClick"
                    FontWeight="Bold" VerticalAlignment="Top"
                    Margin="20,4" FontSize="16"/>
        <TextBlock Text="Welcome o the new Window"
                       Foreground="Yellow" FontSize="25"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"/>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp191
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CloseClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

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

 

 

 

 

 

 

 

 

 

 

 

 //
 // Summary:
 //     Allows a window to be dragged by a mouse with its left button down over an exposed
 //     area of the window's client area.
 //
 // Exceptions:
 //   T:System.InvalidOperationException:
 //     The left mouse button is not down.
 [SecurityCritical]
 public void DragMove()
 {
     VerifyApiSupported();
     SecurityHelper.DemandUIWindowPermission();
     VerifyContextAndObjectState();
     VerifyHwndCreateShowState();
     if (!IsSourceWindowNull && !IsCompositionTargetInvalid)
     {
         if (Mouse.LeftButton != MouseButtonState.Pressed)
         {
             throw new InvalidOperationException(SR.Get("DragMoveFail"));
         }

         if (WindowState == WindowState.Normal)
         {
             UnsafeNativeMethods.SendMessage(CriticalHandle, WindowMessage.WM_SYSCOMMAND, (IntPtr)61458, IntPtr.Zero);
             UnsafeNativeMethods.SendMessage(CriticalHandle, WindowMessage.WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
         }
     }
 }

 

posted @ 2024-07-03 21:17  FredGrit  阅读(4)  评论(0编辑  收藏  举报