使用 WPF 获取工具包样式窗口 [英] Get Toolkit Style Window with WPF

查看:36
本文介绍了使用 WPF 获取工具包样式窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WPF 工具包和 WPF 工具包扩展,并注意到当我通过以下方式显示消息框时:

I am using the WPF toolkit and the WPF toolkit extended and notice that when I display a message box via:

MessageBox.Show("...");

我得到以下...

我想让我的主应用程序窗口也使用这种样式.有办法吗?

I would like to make my main application window use this styling as well. Is there a way to do it?

我试过 WindowStyle="ToolWindow" 但它不起作用.

I've tried WindowStyle="ToolWindow" and it doesn't work.

针对下面的回答,请看下图:

In response to the answer below, please see picture below:

另外,我希望它有一个最大化和最小化按钮,但与关闭按钮的样式相同.

Also, I would like it to have a maximize and minimize button but with the same style as the button for close.

谢谢!

推荐答案

我想在这里添加一个免责声明,我是出于好奇而很快将这些放在一起,而不是其他任何事情.除了在那里放置一些控件并运行它之外,我还没有进行过太多测试.它有效,但不应被视为完全完整.我也认为可能有更好的方法来做到这一点,但我认为这至少会给你一个好的起点.

I want to add a disclaimer here that I threw this together pretty quickly out of curiosity, more than anything. I haven't tested this much, other than dropping a few controls on there and running it. It works, but is shouldn't be considered fully complete. I also think there are probably better ways to do this, but I figured this would at least give you a good starting place.

向您的项目添加一个新的 ResourceDictionary,并添加以下 XAML:

Add a new ResourceDictionary to your project, and add the following XAML:

<ResourceDictionary
  x:Class="CustomWindow.BaseResource"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="WindowStyle" Value="None" />
    <Setter Property="ResizeMode" Value="NoResize" />
    <Setter Property="Background" Value="White" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Window}">
          <Border
            x:Name="WindowBorder"
            BorderBrush="Black"
            BorderThickness="1">
            <Border.Background>
              <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0" Color="White" />
                <GradientStop Offset="1" Color="#FFDADADA" />
              </LinearGradientBrush>
            </Border.Background>
            <Grid
              Width="{TemplateBinding Width}"
              Height="{TemplateBinding Height}"
              MinWidth="{TemplateBinding MinWidth}"
              MinHeight="{TemplateBinding MinHeight}"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              Cursor="Arrow">
              <Grid.RowDefinitions>
                <RowDefinition Height="25" />
                <RowDefinition Height="*" />
              </Grid.RowDefinitions>
              <TextBlock
                Width="500"
                Height="23"
                Margin="11,2,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="{TemplateBinding Title}" />
              <Rectangle
                x:Name="TitleBar"
                Fill="Transparent"
                MouseDown="TitleBar_MouseDown" />
              <Rectangle
                Grid.RowSpan="2"
                Width="10"
                HorizontalAlignment="Left"
                Cursor="SizeWE"
                Fill="Transparent"
                MouseDown="Border_MouseDown"
                Tag="Left" />
              <Rectangle
                Grid.RowSpan="2"
                Width="10"
                HorizontalAlignment="Right"
                Cursor="SizeWE"
                Fill="Transparent"
                MouseDown="Border_MouseDown"
                Tag="Right" />
              <Rectangle
                Height="5"
                VerticalAlignment="Top"
                Cursor="SizeNS"
                Fill="Transparent"
                MouseDown="Border_MouseDown"
                Tag="Top" />
              <Rectangle
                Grid.Row="1"
                Height="10"
                VerticalAlignment="Bottom"
                Cursor="SizeNS"
                Fill="Transparent"
                MouseDown="Border_MouseDown"
                Tag="Bottom" />
              <StackPanel
                Margin="10,0"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Orientation="Horizontal">
                <Button
                  x:Name="MinimizeButton"
                  Width="43"
                  Height="17"
                  Click="MinimizeButton_Click"
                  IsTabStop="False">
                  min
                </Button>
                <Button
                  x:Name="MaximizeButton"
                  Width="43"
                  Height="17"
                  Margin="5,0"
                  Click="MaximizeButton_Click"
                  IsTabStop="False">
                  max
                </Button>
                <Button
                  x:Name="CloseButton"
                  Width="43"
                  Height="17"
                  Click="CloseButton_Click"
                  IsTabStop="False">
                  <Path
                    Width="12"
                    Height="10"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Data="M0.5,0.5L4.5178828,0.5 6.0620003,3.125 7.4936447,
                      0.5 11.5,0.5 11.5,1.5476431 8.7425003,6.1201854 11.5,
                      10.359666 11.5,11.5 7.4941902,11.5 6.0620003,8.8740005 
                      4.5172949,11.5 0.5,11.5 0.5,10.43379 3.3059995,
                      6.1201582 0.5,1.4676378 0.5,0.5z"
                    Fill="#E4FFFFFF"
                    Stretch="Fill"
                    Stroke="#FF535666" />
                </Button>
              </StackPanel>
              <Border
                Grid.Row="1"
                Margin="10,0,10,10"
                BorderBrush="Black"
                BorderThickness="1">
                <Grid>
                  <Rectangle Fill="White" />
                  <ContentPresenter Content="{TemplateBinding Content}" />
                </Grid>
              </Border>
            </Grid>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

为了便于说明,假设我们将 ResourceDictionary 命名为 BaseResource.xaml.

For illustrative purposes, let's say we named the ResourceDictionary, BaseResource.xaml.

接下来将一个新类添加到您添加资源的同一目录中,并将文件命名为 BaseResource.xaml.cs,替换您的 XAML 文件的实际名称.

Next add a new Class to the same directory where you added the resource, and name the file BaseResource.xaml.cs, substituting the actual name of your XAML file.

返回 BaseResource.xaml 并将 x:Class="CustomWindow.BaseResource" 属性更改为 ResourceDictionary 的完整类型名称.如果更改正确,Visual Studio 应该会抱怨 BaseResource.xaml.cs 中有重复的定义.通过向 .cs 文件中的类添加 partial 关键字修饰符来修复该问题.

Go back to BaseResource.xaml and change the x:Class="CustomWindow.BaseResource" property to the full type name of the ResourceDictionary. If changed correctly, Visual Studio should complain that there is a duplicate definition in BaseResource.xaml.cs. Fix that by adding the partial keyword modifier to the class in the .cs file.

最后在代码文件中加入如下代码:

Finally, add the following code to the code file:

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

namespace CustomWindow
{
    public partial class BaseResource
    {
        private const uint WM_SYSCOMMAND = 0x112;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr 
            SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            Window.GetWindow((DependencyObject) sender).Close();
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            Window.GetWindow((DependencyObject) sender).WindowState = WindowState.Minimized;
        }

        private void MaximizeButton_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            var window = Window.GetWindow((DependencyObject) sender);
            window.WindowState = 
              (window.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
        }

        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            Window.GetWindow((DependencyObject) sender).DragMove();
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var direction = (Direction)Enum.Parse(typeof(Direction), ((FrameworkElement)sender).Tag.ToString());
            ResizeWindow(PresentationSource.FromVisual((Visual)sender) as HwndSource, direction);
        }

        private void ResizeWindow(HwndSource hwndSource, Direction direction)
        {
            SendMessage(hwndSource.Handle, WM_SYSCOMMAND, (int)(61440 + direction), 0);
        }

        private enum Direction
        {
            Left = 1,
            Right = 2,
            Top = 3,
            TopLeft = 4,
            TopRight = 5,
            Bottom = 6,
            BottomLeft = 7,
            BottomRight = 8,
        }
    }
}

接下来将 ResourceDictionary 添加到全局应用程序资源.同样,如果您的文件名不同,请将 BaseResource.xaml 替换为您的文件名.

Next add the ResourceDictionary to global Application resources. Again, substituting BaseResource.xaml with the name of your file, if it is different.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/BaseResource.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

将样式添加到目标窗口.

Add the style to the target Window.

<Window
  ...
  Style="{StaticResource WindowStyle}">

一些事情...

设计师在没有内容的情况下就搞乱了风格.这可能是因为我将 TemplateBinding 的 Height 和 Width 放在了哪里,但它在运行时看起来不错,所以我没有进一步弄乱它.我也没有为角落实现调整大小的手柄,但它很容易添加到模板中.

The designer messes with the style when there is no content. It is probably because of where I placed the TemplateBinding for Height and Width, but it looks fine at runtime, so I didn't mess with it further. I also didn't implement the resize grip for the corners, but it is easy enough to add to the template.

最后,我没有为最小化和最大化按钮绘制图标.我不擅长在 Blend 中创建路径.

Finally, I didn't draw icons for the Minimize and Maximize buttons. I suck at creating Paths in Blend.

希望有帮助.

这篇关于使用 WPF 获取工具包样式窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆