WPF Tips: 无边框渐变色窗体示例
Posted Jane&Coding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Tips: 无边框渐变色窗体示例相关的知识,希望对你有一定的参考价值。
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="窗体名称" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True" Background="{x:Null}" WindowStartupLocation="CenterScreen" > <Grid> <Border Height="Auto"> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFD9E1EE" Offset="1"/> <GradientStop Color="#FF3281C3" Offset="0"/> </LinearGradientBrush> </Border.Background> </Border> <DockPanel LastChildFill="True"> <DockPanel x:Name="Header" LastChildFill="True" Height="30" VerticalAlignment="Top" DockPanel.Dock="Top"> <Button x:Name="ButtonClose" Style="{StaticResource btnCloseStyle}" Click="ButtonClose_Click" DockPanel.Dock="Right" Margin="0,0,8,0"/> <Button x:Name="ButtonMini" Style="{StaticResource btnMiniStyle}" Click="ButtonMini_Click" DockPanel.Dock="Right" Margin="0,0,10,0"></Button> <Image Source="logo.ico" DockPanel.Dock="Left" Margin="5,2,5,0" Width="25" Height="25"/> <Label Foreground="White" FontSize="14" FontFamily="Microsoft YaHei UI">WPF 窗体外观示例 V1.0</Label> </DockPanel> </DockPanel> </Grid> </Window>
App.xaml
<Style x:Key="btnCloseStyle" TargetType="Button"> <Setter Property="Width" Value="30"/> <Setter Property="Height" Value="30"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="PART_Border" CornerRadius="0" BorderBrush="Black" BorderThickness="0" Padding="2"> <Grid Width="12" Height="12"> <Path x:Name="PART_Path" Data="M6,6 L6,6 10,10 M10,6 L10,6 6,10" Fill="Gray" Stretch="Fill" Stroke="White" StrokeThickness="1" VerticalAlignment="Center"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" TargetName="PART_Border" Value="Red"> </Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" TargetName="PART_Border" Value="#FF2727BF"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="btnMiniStyle" TargetType="Button"> <Setter Property="Width" Value="30"/> <Setter Property="Height" Value="30"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="PART_Border" CornerRadius="0" BorderBrush="Black" BorderThickness="0" Padding="2"> <Path x:Name="PART_Path" Data="m 0,15 h12 " Fill="Gray" Stretch="None" Stroke="White" StrokeThickness="1" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" TargetName="PART_Border" Value="#FF88B7E6"> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
MainWindow.xaml.cs(为了能够实现窗口拖动,需要实现MouseLeftButtonDown事件)
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown; } void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.OriginalSource is Grid || e.OriginalSource is Border || e.OriginalSource is Window) { WindowInteropHelper win = new WindowInteropHelper(this); Win32.SendMessage(win.Handle, Win32.WM_NCLBUTTONDOWN, (int)Win32.HitTest.HTCAPTION, 0); } } private void ButtonClose_Click(object sender, RoutedEventArgs e) { this.Close(); } private void ButtonMini_Click(object sender, RoutedEventArgs e) { this.WindowState = System.Windows.WindowState.Minimized; } } public class Win32 { public const int WM_GETMINMAXINFO = 0x0024; public const int MONITOR_DEFAULTTONEAREST = 2; [DllImport("user32.dll")] public static extern IntPtr MonitorFromWindow(IntPtr hwnd, int dwFlags); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] public class MONITORINFOEX { public int cbSize; public RECT rcMonitor; public RECT rcWork; public int dwFlags; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] public char[] szDevice; } [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; public POINT(int x, int y) { this.x = x; this.y = y; } } [StructLayout(LayoutKind.Sequential)] public struct MINMAXINFO { public POINT ptReserved; public POINT ptMaxSize; public POINT ptMaxPosition; public POINT ptMinTrackSize; public POINT ptMaxTrackSize; } [DllImport("user32.dll")] public static extern bool GetMonitorInfo(HandleRef hmonitor, [In, Out]MONITORINFOEX monitorInfo); public const int WM_NCHITTEST = 0x0084; public enum HitTest : int { HTERROR = -2, HTTRANSPARENT = -1, HTNOWHERE = 0, HTCLIENT = 1, HTCAPTION = 2, HTSYSMENU = 3, HTGROWBOX = 4, HTSIZE = 4, HTMENU = 5, HTHSCROLL = 6, HTVSCROLL = 7, HTMINBUTTON = 8, HTREDUCE = 8, HTMAXBUTTON = 9, HTZOOM = 9, HTLEFT = 10, HTRIGHT = 11, HTTOP = 12, HTTOPLEFT = 13, HTTOPRIGHT = 14, HTBOTTOM = 15, HTBOTTOMLEFT = 16, HTBOTTOMRIGHT = 17, HTBORDER = 18, HTCLOSE = 20, HTHELP = 21, } public const int WM_NCLBUTTONDOWN = 0x00A1; [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); }
结果:
以上是关于WPF Tips: 无边框渐变色窗体示例的主要内容,如果未能解决你的问题,请参考以下文章