閉じるボタンと移動可能なサイズ変更可能なウィンドウを含まない、WPFウィンドウの基本カスタムウィンドウクロムを作成するにはどうすればよいですか?wpfでカスタムウィンドウクロムを作成するには?
答えて
ウィンドウのWindowStyle="None"
を設定して、独自のウィンドウインターフェイスを構築します。独自のMin/Max/Close/Dragイベントハンドラを構築する必要がありますが、サイズ変更は依然として維持されています。例えば
:
<Window
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
>
<DockPanel x:Name="RootWindow">
<DockPanel x:Name="TitleBar">
<Button x:Name="CloseButton" DockPanel.Dock="Right" />
<Button x:Name="MaxButton" DockPanel.Dock="Right" />
<Button x:Name="MinButton" DockPanel.Dock="Right" />
<TextBlock HorizontalAlignment="Center">Application Name</TextBlock>
</DockPanel>
<ContentControl Content="{Binding CurrentPage}" />
</DockPanel>
</Window>
そして、ここでは一般的なウィンドウ機能
/// <summary>
/// TitleBar_MouseDown - Drag if single-click, resize if double-click
/// </summary>
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
if (e.ClickCount == 2)
{
AdjustWindowSize();
}
else
{
Application.Current.MainWindow.DragMove();
}
}
/// <summary>
/// CloseButton_Clicked
/// </summary>
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
/// <summary>
/// MaximizedButton_Clicked
/// </summary>
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
AdjustWindowSize();
}
/// <summary>
/// Minimized Button_Clicked
/// </summary>
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
/// <summary>
/// Adjusts the WindowSize to correct parameters when Maximize button is clicked
/// </summary>
private void AdjustWindowSize()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
MaximizeButton.Content = "1";
}
else
{
this.WindowState = WindowState.Maximized;
MaximizeButton.Content = "2";
}
}
+1 - タイトルバーのマウス操作のための良い解決策 –
実際には、この-vs-メインウィンドウ-vs-アプリケーションの使用は、(エラーが発生しやすいように)ちょっと忙しいです。アレックスはもっと頑丈な[実装](http://web.archive.org/web/20081006142447/http://blog.opennetcf.com/ayakhnin/PermaLink,guid,23a7be17-d833-4e45-b498-34ef04d98cb2.aspx) 、それはまた少数の機能的な断片を欠いている。 –
おそらく、 'Titlebar'ドックパネル上に' DockPanel.Dock = "Top"を付けた方がいいでしょう。 – Aaron
のためのいくつかのサンプルコードビハインドの.NET 4.5が大幅にこれを簡素化する新しいクラスを追加しました。
WindowChrome classは、通常、オペレーティングシステムのウィンドウマネージャ用に予約されているウィンドウの非クライアント領域にWindowsのプレゼンテーション財団(WPF)コンテンツを拡張することができます。
tutorial hereがあります。
ここにはshort example usageがあります。
このチュートリアルでは、リンクが不足していて、廃止された情報が満載です。この短い例には、もはや動作しないアセンブリ参照が含まれています。 –
私にお知らせいただきありがとうございます。私が何か良いものを見つけたら、投稿します。 – dss539
私はちょうど.net 4.5のための以下の例を使用しました、そして、それはとてもうまく動作します。興味深いことに、Clickイベントのリソース辞書のコードを使用しています。あなたがしなければならないのは、app.xaml
ファイルのリソース辞書を参照してから、ウィンドウにスタイルCustomWindowStyle
を割り当てるだけです。これは恥知らずに盗まれたhttp://www.eidias.com/blog/2014/1/27/restyle-your-windowからだった。
<ResourceDictionary x:Class="WpfApp7.WindowStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
CornerRadius="4"
GlassFrameThickness="0"
NonClientFrameEdges="None"
ResizeBorderThickness="5"
UseAeroCaptionButtons="False" />
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="5,30,5,5">
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Border>
<Grid Height="30"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="5,0">
<Button Content="A" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/>
<Button Content="B" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/>
<Button Content="C" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/>
<Button Content="D" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/>
</StackPanel>
<TextBlock Margin="5,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="16"
Foreground="White"
Text="{TemplateBinding Title}"
Grid.Column="1"/>
<StackPanel Orientation="Horizontal"
Grid.Column="2">
<Button x:Name="btnClose"
Width="15"
Margin="5"
Click="CloseClick"
Content="X"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnRestore"
Width="15"
Margin="5"
Click="MaximizeRestoreClick"
Content="#"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnMinimize"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="MinimizeClick"
Content="_"
WindowChrome.IsHitTestVisibleInChrome="True" />
</StackPanel>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
そして、背後にあるコードのために:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp7
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
private void CloseClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
window.Close();
}
private void MaximizeRestoreClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
if (window.WindowState == System.Windows.WindowState.Normal)
{
window.WindowState = System.Windows.WindowState.Maximized;
}
else
{
window.WindowState = System.Windows.WindowState.Normal;
}
}
private void MinimizeClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
window.WindowState = System.Windows.WindowState.Minimized;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello!");
}
}
}
- 1. WPFでレンダリングループを作成するには?
- 2. Direct2Dを使用してカスタムウィンドウクロムを作成するにはどうすればよいですか?
- 3. カスタムWPFコレクションを作成するには?
- 4. wpfカウントダウンタイマーを作成するには?
- 5. C#、WPFでWindowsで「プッシュ通知」サービスを作成するには?
- 6. WPFのビットマップであるUserControlを作成するには?
- 7. WPFで16ビット+グレースケールイメージを作成する
- 8. WPFでDataGridRowのControlTemplateを作成する
- 9. WPFでカスタムデザイン時コレクションエディタを作成する
- 10. バックグラウンドスレッドでUIを作成するWPF?
- 11. SciChartでショートカットを作成するWPF
- 12. Wpfで子を作成するMenuItemを強制的に作成する
- 13. Wpfでverticalメニューを作成
- 14. SilverlightでWPFのようなデータトリガを作成するには?
- 15. WPFで複数ページのUserControlを作成するには?
- 16. WPFでページフリップ効果を作成するには?
- 17. スケーラブルなMoebius-stripをWPFで作成するには?
- 18. WPFでSystem.Drawing.ImageからImageBrushを作成するには?
- 19. WPFでボタンの右クリックコンテキストメニューを作成するには
- 20. WPFでカスタムシェイプのパネルを作成するには?
- 21. wpfアプリでwin7電話スライドエフェクトを再作成するには
- 22. wpfで次のスタイルを作成するには?
- 23. WPFでのカスタムコンボボックスの作成
- 24. WPFでカスタムフォームを作成しますか?
- 25. WPFでメニューバーを作成しますか?
- 26. wpfアプリケーションのエラーログを作成する
- 27. XMLインスタンスを作成するWPFアプリケーション
- 28. WPFプロジェクトのバッチファイルを作成する
- 29. カスタムウィンドウクロムを使用しているときに、WPFキャプションバーにボタンを追加するにはどうすればよいですか?
- 30. WPF/C#でタブ可能なコンテンツを作成する方法は?
http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf .aspx – BoltClock
私はそれをチェックしましたが、チュートリアルの多くを探していました... –
古くても、まだ関連性の高い質問はここにあります:[カスタムビジュアルで無骨なウィンドウを作成する](http://stackoverflow.com/questions/ 2969521 /作成-bordless-windows-with-custom-visual/2969652#2969652)。 –