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);
    }

结果:

时间: 2024-10-11 06:15:45

WPF Tips: 无边框渐变色窗体示例的相关文章

C# 无边框异型窗体制作

我是一个C#的初学者 只要涉及到windows窗体编程 都希望窗体的外观比较好看 不是系统默认的那样 对于C# 更改窗体外观感觉并不那么轻松 更改窗体外观涉及到使用GDI+ 我所知道的有两种方法: 有系统边框的窗体  处理窗体的Paint方法,在paint方法中 参数e.Graphics属性将返回一个对象 用来作画  但是这个画布的区域是窗体的客户区 所以无法修改到窗体的标题栏 边框等位置的.要更改标题栏只有获取整个窗口的句柄 这样创建的画布是整个窗体区域 但是对于我这样的初学者来说 对于句柄

使用WPF创建无边框窗体

一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency="True"使得窗口无边框.并对Window添加DropShadowEffect效果并设定相关参数,在这里我根据设计师的要求设置ShadowDepth="1" BlurRadius="6" Direction="270" Opacity=&q

c#无边框异形窗体制作

下面是最终效果:   这就是一个无边框窗体 可以注意到它的外观 四个角是圆的 有控制按钮 并且还可以拖拽,当鼠标移动到窗体的四个角和边缘的时候可以拖拽大小 这个窗体没有标题栏和边框的限制 控件可以放在窗体上任何地方 下面就是直奔主题了: 先说一说制作这个窗体的思路(窗体集成自System.Windows.Forms.Form): 1.设置FormBorderStyle属性为none 让它成为一个无边框窗体 2.设置窗体的Region属性 该属性设置窗体的有效区域 而我们把窗体的有效区域设置为圆角

wpf 自定义 无边框 窗体 resize 实现

参数定义 1 class NativeMethods 2 { 3 public const int WM_NCHITTEST = 0x84; 4 public const int HTCAPTION = 2; 5 public const int HTLEFT = 10; 6 public const int HTRIGHT = 11; 7 public const int HTTOP = 12; 8 public const int HTTOPLEFT = 13; 9 public const

C# WPF 建立无边框(标题栏)的登录窗口

前言:笔者最近用c#写WPF做了一个项目,此前未曾做过完整的WPF项目,算是一边学一边用,网上搜了不少资料,效率当然是不敢恭维的,有时会在一些很简单的问题上纠结很长时间,血与泪的教训可不少. 不过,正如电视剧某榜里的一句话:既然我活了下来,就不会白白活着!笔者怎么也算挣扎过了,有些经验与教训可以分享,趁着记忆深刻总结写下来.希望后来者少走弯路,提高工作效率.如果有写得不好的地方,希望读者能够指正,一起进步! --------------------------------- 今天先从登录窗口说起

WPF 创建无边框的圆角窗口

第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为True,即:Background="Transparent"  AllowsTransparency="True",将Window的WindowStyle属性设置为None,即WindowStyle="None" (当AllowsTransparency="True"时,WindowStyle.None 是 WindowStyle 的唯一有效值)

【Reproduce】 C#中实现拖动无边框Form窗体

首先建一个Windows应用程序 将Form1的 FormBorderStyle属性设置为Noe 主要是在Form1窗体触发三个事件:Form4_MouseDown,Form4_MouseMove,Form4_MouseUp 代码如下:       public partial class Form1 : Form { Point mouseOff; //鼠标移动位置变量 bool leftFlag; //标签是否为左键 public Form1() { InitializeComponent(

无边框窗体和后台创建控件

1.无边框窗体 最小化 最大化 关闭 按钮 不一定非要用按钮来做, 可以用图片 写事件,加上鼠标 移入移出 点击 来操作 MouseEnter-鼠标移入的时候发生的事件 private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\..\\..\\images\\btn_close

无边框窗体设置

#region 方法:无边框拖动窗体 Point mouseOff;//鼠标移动位置变量 bool RightFlag;//标签是否为左键 private void groupMenu_MouseUp(object sender, MouseEventArgs e) { if (RightFlag) { RightFlag = false;//释放鼠标后标注为false; } } private void groupMenu_MouseMove(object sender, MouseEvent