程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF 自定義窗口,wpf自定義窗口

WPF 自定義窗口,wpf自定義窗口

編輯:關於.NET

WPF 自定義窗口,wpf自定義窗口


在WPF中,經常需要對窗口進行設置,下面講講常用的幾個設置。

1、無邊框窗口

Window 窗口樣式無

AllowsTransparency="True" 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" Window Background="Transparent">
    <Grid>
        <Border CornerRadius="5" Background="Red"></Border>
    </Grid>
</Window>
View Code

2、窗口按鈕

當我們設置無邊窗口時,也即是需要重新設置一套窗口的操作按鈕。

按鈕,可以用修改Button的Background或者修改Template,往其中設置圖標或者直接用Path、Border、Rectangle等基礎元素繪制。

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.直接用基礎元素繪制

<StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button x:Name="BtnMinimize" ToolTip="Minimize" >
                        <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" >
                        <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" >
                        <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"  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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved