WPF 窗口

简介: 在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置。 窗口样式 1、无边框窗口 无边框透明窗体 设置 WindowStyle="None"--无边框,如果需要其它按钮,如缩小、放大、收缩、关闭按钮,可以自定义 AllowsTransparency="True"--只有设置了Allo...

在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置。

窗口样式

1、无边框窗口

无边框透明窗体 设置

WindowStyle="None"--无边框,如果需要其它按钮,如缩小、放大、收缩、关闭按钮,可以自定义

AllowsTransparency="True"--只有设置了AllowsTransparency,才有1、窗体真正的隐藏边框2、背景透明有效

Background="Transparent"

窗口阴影设置

可以用Border的CornerRadius设置窗口四周圆角

设置窗口阴影--值得一提的是,如果直接用Window.Effect,效果会影响CornerRadius设置的圆角效果。

<Window.Effect>
<DropShadowEffect BlurRadius="30" Color="Gray" ShadowDepth="0" RenderingBias="Quality" ></DropShadowEffect>
</Window.Effect>

举例:

<Window x:Class="WpfApplication4.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" 
        AllowsTransparency="True" WindowStyle="None" Background="Transparent">
    <Grid>
        <Border CornerRadius="5" Background="Red"></Border>
    </Grid>
</Window>
View Code

2、窗口按钮

当我们设置无边窗口时,也即是需要重新设置一套窗口的操作按钮。

按钮,可以用修改Button的Background或者修改Template,往其中设置图标或者直接用Path、Border、Rectangle等基础元素绘制。

然后添加简单的动画Animation

1) 方法1.下面是自定义的按钮Style,往其中传入图标路径即可

<Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Rectangle x:Name="T_Rectangle" Height="15" Width="15">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="{Binding ImagesSource}"></ImageBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"></ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="T_Rectangle" Property="Height" Value="18"></Setter>
                                <Setter TargetName="T_Rectangle" Property="Width" Value="18"></Setter>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="T_Rectangle" Property="Height" Value="20"></Setter>
                                <Setter TargetName="T_Rectangle" Property="Width" Value="20"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
View Code

2)方法2.直接用基础元素绘制

Path,通过Path来画线。当然一般水平、竖直也是可以用Rectangle/Border之类的替代

一些简单的线条图标用Path来做,还是很方便的。

<StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button x:Name="BtnMinimize" ToolTip="Minimize" Style="{StaticResource TitleBtnStyle}">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border x:Name="T_Grid" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                                    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
                                        <Path x:Name="T_Path" Data="M0,0 L8,0" Stroke="Black" StrokeThickness="1"></Path>
                                    </Grid>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger SourceName="T_Grid" Property="IsMouseOver" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="LightGray"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="#FFC1C1C1"></Setter>
                                        <Setter TargetName="T_Path" Property="Margin" Value="0,4,0,0"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                         </Button.Template>
                    </Button>
                    <Button x:Name="BtnMaximize" ToolTip="Maximize" Style="{StaticResource TitleBtnStyle}">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border x:Name="T_Grid" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                                    <Rectangle x:Name="T_Rect" Stroke="Black" StrokeThickness="0.5" Height="8" Margin="5,0" Width="8"></Rectangle>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="LightGray"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="#FFC1C1C1"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <Button x:Name="BtnRestore" ToolTip="Restore" Style="{StaticResource TitleBtnStyle}">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Border x:Name="T_Grid" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                                    <Grid >
                                        <Rectangle Stroke="Black" StrokeThickness="0.5" Height="7" Width="7" Margin="2,0,0,2"></Rectangle>
                                        <Rectangle Stroke="Black" StrokeThickness="0.5" Height="7" Width="7" Fill="White" Margin="0,2,2,0"></Rectangle>
                                    </Grid>
                                 </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="LightGray"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="#FFC1C1C1"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <Button x:Name="BtnClose" ToolTip="Close" Style="{StaticResource TitleBtnStyle}" Click="BtnClose_OnClick">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                    <Border x:Name="T_Grid" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                                        <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
                                            <Path Data="M0,0 L8,8" Stroke="Black" StrokeThickness="1"></Path>
                                            <Path Data="M8,0 L0,8" Stroke="Black" StrokeThickness="1"></Path>
                                        </Grid>
                                    </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="LightGray"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="True">
                                        <Setter TargetName="T_Grid" Property="Background" Value="#FFC1C1C1"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </StackPanel>
View Code

 

3、窗口 resize 鼠标自由改变窗口大小 (转)

覆写一个OnSourceInitialized方法,在其中添加一个委托

        //重写
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource != null)
            {
                hwndSource.AddHook(new HwndSourceHook(WndProc));
            }
        }

        private const int WM_NCHITTEST = 0x0084;
        private Point mousePoint = new Point(); //鼠标坐标
        private const int ResizeBorderAGWidth = 12;//转角宽度 
        private const int ResizeBorderThickness = 4;//边框宽度
        /// <summary>
        /// 委托方法
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <param name="handled"></param>
        /// <returns></returns>
        protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case WM_NCHITTEST:
                    mousePoint.X = (lParam.ToInt32() & 0xFFFF);
                    mousePoint.Y = (lParam.ToInt32() >> 16);
                    // 窗口左上角
                    if (mousePoint.Y - Top <= ResizeBorderAGWidth && mousePoint.X - Left <= ResizeBorderAGWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPLEFT);
                    }
                    // 窗口左下角  
                    else if (ActualHeight + Top - mousePoint.Y <= ResizeBorderAGWidth && mousePoint.X - Left <= ResizeBorderAGWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                    }
                    // 窗口右上角
                    else if (mousePoint.Y - Top <= ResizeBorderAGWidth && ActualWidth + Left - mousePoint.X <= ResizeBorderAGWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPRIGHT);
                    }
                    // 窗口右下角
                    else if (ActualWidth + Left - mousePoint.X <= ResizeBorderAGWidth && ActualHeight + Top - mousePoint.Y <= ResizeBorderAGWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                    }
                    // 窗口左侧
                    else if (mousePoint.X - Left <= ResizeBorderThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTLEFT);
                    }
                    // 窗口右侧
                    else if (ActualWidth + Left - mousePoint.X <= ResizeBorderThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTRIGHT);
                    }
                    // 窗口上方
                    else if (mousePoint.Y - Top <= ResizeBorderThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOP);
                    }
                    // 窗口下方
                    else if (ActualHeight + Top - mousePoint.Y <= ResizeBorderThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOM);
                    }
                    else // 窗口移动
                    {
                        //handled = true;
                        //return new IntPtr((int)HitTest.HTCAPTION);
                        return IntPtr.Zero;
                    }
            }

            return IntPtr.Zero;
        }
View Code

用到的枚举列表类:

public enum HitTest : int
    {
        HTERROR = -2,
        HTTRANSPARENT = -1,
        HTNOWHERE = 0,
        HTCLIENT = 1,
        HTCAPTION = 2,
        HTSYSMENU = 3,
        HTGROWBOX = 4,
        HTSIZE = HTGROWBOX,
        HTMENU = 5,
        HTHSCROLL = 6,
        HTVSCROLL = 7,
        HTMINBUTTON = 8,
        HTMAXBUTTON = 9,
        HTLEFT = 10,
        HTRIGHT = 11,
        HTTOP = 12,
        HTTOPLEFT = 13,
        HTTOPRIGHT = 14,
        HTBOTTOM = 15,
        HTBOTTOMLEFT = 16,
        HTBOTTOMRIGHT = 17,
        HTBORDER = 18,
        HTREDUCE = HTMINBUTTON,
        HTZOOM = HTMAXBUTTON,
        HTSIZEFIRST = HTLEFT,
        HTSIZELAST = HTBOTTOMRIGHT,
        HTOBJECT = 19,
        HTCLOSE = 20,
        HTHELP = 21,
    }
View Code

  

窗口拖动

1、在界面上对布局元素如Grid,添加委托事件: MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown"

2、委托方法中处理一下就行了:

        private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState==MouseButtonState.Pressed) { this.DragMove(); } }

 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
目录
相关文章
|
C# 数据可视化 开发工具
WPF实现选项卡效果(1)——使用AvalonDock
原文:WPF实现选项卡效果(1)——使用AvalonDock 简介   公司最近一个项目,软件采用WPF开发,需要实现类似于VS的选项卡(或者是浏览器的选项卡)效果。
2258 0
|
人工智能 C#
WPF自定义控件库之Window窗口
本文以自定义窗口为例,简述WPF开发中如何通过自定义控件来扩展功能和样式,仅供学习分享使用,如有不足之处,还请指正。
242 5
|
API C# UED
wpf的PixelLab.Wpf切换效果
wpf的PixelLab.Wpf切换效果
|
C#
WPF实现窗体中的悬浮按钮
原文:WPF实现窗体中的悬浮按钮 WPF实现窗体中的悬浮按钮,按钮可拖动,吸附停靠在窗体边缘。 控件XAML代码: ...
2338 0
|
C#
C#或者WPF中让某个窗体置顶
原文:C#或者WPF中让某个窗体置顶 前记:在工作中有个需求,要求不管到那个界面,我必须让一个浮动条(其实是个窗体)置顶。
2087 0
|
C# 前端开发
使用Adorner显示WPF控件的边界点
原文:使用Adorner显示WPF控件的边界点 当我们拖动WPF控件时,我们为了更清楚地需要显示控件,一般我们会在WPF控件所围成的矩形区域的四个边界点上作一个特殊的记号(比如圆点)。
1275 0
WPF-WPF BitmapEffect (按钮凹凸效果)
原文:WPF-WPF BitmapEffect (按钮凹凸效果) BitmapEffect位图效果是简单的像素处理操作。它可以呈现下面几种特殊效果。              BevelBitmapEffect        凹凸效果             BlurBitmapEffect...
1779 0
|
C#
WPF:拖动父窗口行为
原文 WPF:拖动父窗口行为 这次只是一个快速的帖子:当我点击并拖动特定的UIElement时,我需要能够重新定位WPF窗口。目的是重新创建在标准Windows标题栏上单击和拖动的行为(在我的情况下,我正在实现我自己的标题栏)。
926 0
|
C#
WPF 窗口
原文:WPF 窗口 在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置。 窗口样式 1、无边框窗口 无边框透明窗体 设置 WindowStyle="None"--无边框,如果需要其它按钮,如缩小、放大、收缩、关闭按钮,可以自定义 AllowsTransparency="True"-...
746 0
|
前端开发 C#
WPF 实现拖动工具箱效果
原文:WPF 实现拖动工具箱效果     1.效果   点击左边的矩形拖动到右边canvas面板,右边面板添加矩形  2.布局  左边是个StockPanel,上面有个矩形,右边是个Canvas面板。
2031 0