2011-07-22 14 views
26

閉じるボタンと移動可能なサイズ変更可能なウィンドウを含まない、WPFウィンドウの基本カスタムウィンドウクロムを作成するにはどうすればよいですか?wpfでカスタムウィンドウクロムを作成するには?

+1

http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf .aspx – BoltClock

+0

私はそれをチェックしましたが、チュートリアルの多くを探していました... –

+0

古くても、まだ関連性の高い質問はここにあります:[カスタムビジュアルで無骨なウィンドウを作成する](http://stackoverflow.com/questions/ 2969521 /作成-bordless-windows-with-custom-visual/2969652#2969652)。 –

答えて

43

ウィンドウの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"; 
    } 

} 
+0

+1 - タイトルバーのマウス操作のための良い解決策 –

+2

実際には、この-vs-メインウィンドウ-vs-アプリケーションの使用は、(エラーが発生しやすいように)ちょっと忙しいです。アレックスはもっと頑丈な[実装](http://web.archive.org/web/20081006142447/http://blog.opennetcf.com/ayakhnin/PermaLink,guid,23a7be17-d833-4e45-b498-34ef04d98cb2.aspx) 、それはまた少数の機能的な断片を欠いている。 –

+2

おそらく、 'Titlebar'ドックパネル上に' DockPanel.Dock = "Top"を付けた方がいいでしょう。 – Aaron

35

のためのいくつかのサンプルコードビハインドの.NET 4.5が大幅にこれを簡素化する新しいクラスを追加しました。

WindowChrome classは、通常、オペレーティングシステムのウィンドウマネージャ用に予約されているウィンドウの非クライアント領域にWindowsのプレゼンテーション財団(WPF)コンテンツを拡張することができます。

tutorial hereがあります。

ここにはshort example usageがあります。

+0

このチュートリアルでは、リンクが不足していて、廃止された情報が満載です。この短い例には、もはや動作しないアセンブリ参照が含まれています。 –

+0

私にお知らせいただきありがとうございます。私が何か良いものを見つけたら、投稿します。 – dss539

1

私はちょうど.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!"); 
     } 
    } 
} 
関連する問題