我使用WPF工具和WPF工具擴展和注意到,當我通過顯示一個消息框:獲取工具包樣式窗口與WPF
MessageBox.Show("...");
我得到以下...
我想讓我的主應用程序窗口也使用這種樣式。有沒有辦法做到這一點?
編輯:我試過WindowStyle =「ToolWindow」,它不工作。
編輯:在回答下面的答案,請看下圖:
另外,我想它有一個最大化和最小化按鈕,但具有相同的風格爲關閉按鈕。
謝謝!
我使用WPF工具和WPF工具擴展和注意到,當我通過顯示一個消息框:獲取工具包樣式窗口與WPF
MessageBox.Show("...");
我得到以下...
我想讓我的主應用程序窗口也使用這種樣式。有沒有辦法做到這一點?
編輯:我試過WindowStyle =「ToolWindow」,它不工作。
編輯:在回答下面的答案,請看下圖:
另外,我想它有一個最大化和最小化按鈕,但具有相同的風格爲關閉按鈕。
謝謝!
我想在這裏補充一個聲明,我很快扔了一起出於好奇,比什麼都重要。我沒有測試過這麼多,除了在那裏放下幾個控件並運行它。它有效,但不應被視爲完全完整。我也認爲可能有更好的方法來做到這一點,但我認爲這至少會給你一個很好的起點。
添加新ResourceDictionary
到您的項目,並添加以下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。
接下來,將新類添加到您添加資源的同一目錄中,並將文件命名爲BaseResource.xaml.cs,替換XAML文件的實際名稱。
回到BaseResource.xaml並將x:Class="CustomWindow.BaseResource"
屬性更改爲ResourceDictionary的完整類型名稱。如果正確更改,Visual Studio應該抱怨BaseResource.xaml.cs中存在重複的定義。通過將partial
關鍵字修飾符添加到.cs文件中的類來修復此問題。
最後,下面的代碼添加到代碼文件:
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替換文件的名稱(如果不同)。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BaseResource.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
將樣式添加到目標窗口。
<Window
...
Style="{StaticResource WindowStyle}">
一些東西......
設計師混亂的風格時,有沒有內容。這可能是因爲我在哪裏放置了TemplateBinding for Height和Width,但在運行時看起來很好,所以我沒有進一步弄亂它。我也沒有實現轉角的大小調整,但很容易添加到模板中。
最後,我沒有畫最小化和最大化按鈕的圖標。我吮吸在Blend中創建路徑。
希望它有幫助。
這在XAML編輯
WindowStyle = 「工具窗口」
我剛剛嘗試過,它只是使它正方形和透明(Windows 7),並使關閉按鈕更小。它不像上面的圖片那樣。 –
你的問題是相當模糊的添加到您的窗口標籤。你不會在任何地方說出你想要的MessageBox風格的哪一部分,但我想我知道你在問什麼(相同的顏色和窗口鑲邊)。
由於您使用的是MessageBox的Extended Toolkit版本,因此您有兩個選項。首先,您可以切換出您在Xaml中的每個Window
實例,並將其替換爲extToolkit:ChildWindow
(extToolkit是在xmlns下的名稱空間:extToolkit =「http://schemas.microsoft.com/winfx/2006/」)。 XAML /演示/工具/擴展「)。
這是快速的選擇,但讓你陷入困境與幾件事情。 Title
其中一個缺少來自ChildWindow,而使用Caption
。如果你不用自己的Windows做太多事情,那對你來說可能就足夠了。
您的第二個選擇是通過使用MessageBox或ChildWindow模板中相同的樣式信息來模板Window
本身。擴展工具包ChildWindow的模板位於此答案的底部,但需要進行一些修改才能符合Window的屬性。由於chrome是在模板中實現的,因此您也必須隱藏Window chrome,並使其具有無邊界形式。
這不是最好的情況,擴展工具包應該有自己的Window實現。
如果我是你,我會嘗試切換Windows的ChildWindows開始。
HTH。
下面是一個模板 -
<ControlTemplate x:Key="ChildWindowControlTemplate1" TargetType="{x:Type extToolkit:ChildWindow}">
<Grid x:Name="Root">
<Grid.Resources>
<Style x:Key="FocusVisualStyle" TargetType="{x:Type Control}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="-1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Fill="{TemplateBinding Background}" Margin="{TemplateBinding Margin}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="0.5" StrokeDashArray="4 3">
<Rectangle.RenderTransform>
<TranslateTransform X="{Binding Left}" Y="{Binding Top}"/>
</Rectangle.RenderTransform>
</Rectangle>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Grid.Resources>
<Grid x:Name="PART_WindowRoot" HorizontalAlignment="Left" Height="{TemplateBinding Height}" MinWidth="{TemplateBinding MinWidth}" MinHeight="{TemplateBinding MinHeight}" VerticalAlignment="Top" Width="{TemplateBinding Width}">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Grid x:Name="WindowGrid">
<Border BorderBrush="{TemplateBinding WindowBorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5,5,0,0" Opacity="{TemplateBinding WindowOpacity}"/>
<Grid Background="{x:Null}" Margin="0">
<Border x:Name="WindowBorder" Background="{TemplateBinding WindowBackground}" CornerRadius="4,4,0,0" Margin="1" Opacity="{TemplateBinding WindowOpacity}"/>
<Border BorderBrush="White" BorderThickness="1" CornerRadius="4,4,0,0" Margin="1" Opacity="0.7"/>
</Grid>
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="26"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid x:Name="ContentGrid" Margin="6,0,6,6" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderBrush="White" BorderThickness="1" CornerRadius="1"/>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0.1" Margin="1">
<ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
</Border>
</Grid>
<Border x:Name="PART_DragWidget" Background="Transparent" Grid.Column="1" CornerRadius="5,5,0,0" Margin="1,1,1,0">
<Grid>
<Grid x:Name="CaptionHeader" Margin="1,1,105,0" VerticalAlignment="Center">
<ContentControl x:Name="Caption" Content="{TemplateBinding Caption}" Foreground="{TemplateBinding CaptionForeground}" HorizontalAlignment="Stretch" IsTabStop="False" Margin="5,0,0,0"/>
</Grid>
</Grid>
</Border>
</Grid>
<Border BorderBrush="#A5FFFFFF" BorderThickness="1,0,1,1" CornerRadius="0,0,3,3" HorizontalAlignment="Right" Margin="0,1,7,0" VerticalAlignment="Top">
<Button x:Name="PART_CloseButton" Height="17" IsTabStop="False" Style="{TemplateBinding CloseButtonStyle}" Visibility="{TemplateBinding CloseButtonVisibility}" Width="43">
<Path 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" HorizontalAlignment="Center" Height="10" Stretch="Fill" Stroke="#FF535666" VerticalAlignment="Center" Width="12"/>
</Button>
</Border>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Closed">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
感謝您的回覆,但是,我不能使用子窗口,因爲我需要能夠使用隱藏和其他功能。我只是想知道如何將這種風格應用於Window類。 –
你必須修改窗口的模板本身。你可以使用上面的代碼示例作爲一個起點,但就像我說的,你需要使窗口無邊界,並自己連接所有正常的窗口chrome東西(關閉,最小化等等)。 – Stimul8d
您可以使用WPF Shell Integration Library來完全控制窗口的鑲邊。下載包括一個樣本。
PS。一個常見的誤解是,您可以通過將您的WindowStyle
設置爲None
來提供自定義鑲邊。雖然這會刪除默認鑲邊,但它有幾個缺點,包括您渲染的任何鑲邊需要處理大小/移動,並且您的窗口將在用戶的任務欄上最大化,這是不可接受的行爲。
看起來不錯!謝謝! –