2012-12-04 29 views
7

在WPF中,我可以刪除主窗口的邊框,但是允許調整窗口大小(沒有夾點)嗎?如何創建一個沒有可調整邊框的WPF窗口?

我意識到有一種方法可以通過將調整大小模式設置爲CanResizeWithGrip來執行此方案。不過,我希望能夠通過設置爲CanResize的調整大小模式來執行此操作。

我試着設置如下:

ResizeMode="CanResize" 
WindowStyle="None" 
AllowsTransparency="True" 

然而,通過設置AllowsTransparency,它刪除沒有抓地力,調整的能力。任何想法,我怎麼能把它關閉?

我也應該注意,我無法將AllowsTransparency設置爲true,因爲我在窗口中使用了winformshost控件,當AllowsTransparency爲真時,此窗口未顯示。

+0

我決定只改造我的UI,而是和剛剛成立ResizeMode = 「CanResize」 和機智遺留的邊界。 – jsirr13

回答

-5

最簡單的,但也許太簡單是

this.MouseLeftButtonDown += delegate { this.DragMove(); }; 
+0

我不一定要移動窗口,只是在邊框(我最終只想隱藏)被拖動時調整大小。實質上,我想模仿調整窗口大小的一般行爲,只是隱藏邊框。 – jsirr13

+0

然後,您需要捕捉您正在移動的邊緣,並根據邊緣調整X,Y,W,H。 – kenny

0

您可以使用WPF自定義窗口庫,可在這裏:http://wpfwindow.codeplex.com/

然後,使用該庫的窗口類型時,可以設置相同屬性如上所列

<CustomWindow:EssentialWindow x:Class="CustomWindowDemo.DemoEssentialWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:CustomWindow="clr-namespace:CustomWindow;assembly=CustomWindow" 
    AllowsTransparency="True" ResizeMode="CanResize" WindowStyle="None" Title="EssentialWindow" 
    Height="300" Width="250"> 
+0

在他們的網站上,它表示:只有當AllowsTransparency設置爲「True」時,才能完成WPF窗口的完全自定義。 再次,不幸的是我不能使用,因爲WinFormsHost錯誤,當AllowsTransparency設置爲true時不顯示控件。也許如果我能通過這個問題,你的迴應將是一個接近理想的解決方案。 – jsirr13

+0

@ jsirr13,我還沒有嘗試過,但這裏有一個關於如何通過Win API解決WinFormsHost錯誤http://mrtncls.blogspot.com/2011/07/windowsformshost-on-transparent-wpf.html解決方案的解決方案HTTP://www.neowin。net/forum/topic/646970-c%23-wpf-window-with-transparency-makes-windowsformshost-disappear/ –

+0

我嘗試了第二種解決方案,沒有運氣。我會在有空的時候嘗試第一個解決方案。感謝您的反饋。 – jsirr13

1

用戶需要一些東西來互動來調整窗口的大小。如果您不喜歡默認邊框或夾點,則需要添加一些其他控件,使其具有調整窗口大小的單擊和拖動行爲。也許窗口的每個邊緣上都有一條線?

0

如果ResizeBorderThickness設置爲0,這可能發生

可以將它設置爲正值,在XAML這樣的:

<WindowChrome> 
    <WindowChrome.ResizeBorderThickness> 
     <Thickness>1</Thickness> 
    </WindowChrome.ResizeBorderThickness> 
</WindowChrome> 
5

我知道這是一個老問題,但我只是想爲新人提供工作樣本。

的XAML窗口:

<Window x:Class="CustomWindowDemo.DemoCustomWindow" 
     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" 
     xmlns:local="clr-namespace:CustomWindowDemo" 
     mc:Ignorable="d" 
     Title="DemoCustomWindow" Height="300" Width="300" Background="{x:Null}" AllowsTransparency="True" WindowStyle="None"> 
    <Window.Resources> 
     <Style x:Key="BorderThumb" TargetType="Thumb"> 
      <Setter Property="SnapsToDevicePixels" Value="true" /> 
      <Setter Property="OverridesDefaultStyle" Value="true" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Thumb"> 
         <Rectangle MinWidth="4" MinHeight="4" StrokeThickness="0"> 
          <Rectangle.Fill> 
           <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" Opacity="0.05"/> 
          </Rectangle.Fill> 
         </Rectangle> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <Border BorderBrush="#55FFFFFF" BorderThickness="1" CornerRadius="5"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto"/> 
        <ColumnDefinition/> 
        <ColumnDefinition Width="Auto"/> 
       </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
      <Border MouseMove="Header_MouseMove" DockPanel.Dock="Top" Height="32" Grid.Row="1" Grid.Column="1"> 
        <Border.Background> 
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,3"> 
          <GradientStop Color="#3BB2EA" Offset="0" /> 
          <GradientStop Color="#EFF7FA" Offset="0.3" /> 
         </LinearGradientBrush> 
        </Border.Background> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
         </Grid.ColumnDefinitions> 
         <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0" Text="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/> 
         <Button x:Name="btn_Minimize" Background="{x:Null}" BorderBrush="{x:Null}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" Click="btn_Minimize_Click" Grid.Column="1"> 
          <Image Source="Resources/Ahmadhania-Spherical-Minimize.ico"/> 
         </Button> 
         <Button x:Name="btn_Maximize" Background="{x:Null}" BorderBrush="{x:Null}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" Grid.Column="2" Click="btn_Maximize_Click"> 
          <Image Source="Resources/1412181205_61002.ico"/> 
         </Button> 
         <Button x:Name="btn_Close" Background="{x:Null}" BorderBrush="{x:Null}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" Grid.Column="3" Click="btn_Close_Click"> 
          <Image Source="Resources/Ahmadhania-Spherical-Close(1).ico"/> 
         </Button> 
        </Grid> 
       </Border> 
      <Thumb x:Name="ThumbBottom" DragDelta="ThumbBottom_DragDelta" HorizontalAlignment="Stretch" Cursor="SizeNS" Grid.Column="0" Background="{x:Null}" Margin="3,0" Grid.ColumnSpan="3" Grid.Row="3" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Thumb x:Name="ThumbTop" DragDelta="ThumbTop_DragDelta" HorizontalAlignment="Stretch" Cursor="SizeNS" Grid.Column="0" Background="{x:Null}" Margin="3,0" Grid.ColumnSpan="3" Height="4" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Thumb x:Name="ThumbBottomRightCorner" DragDelta="ThumbBottomRightCorner_DragDelta" HorizontalAlignment="Right" Cursor="SizeNWSE" Grid.Row="3" Grid.Column="3" Background="{x:Null}" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Thumb x:Name="ThumbTopRightCorner" DragDelta="ThumbTopRightCorner_DragDelta" HorizontalAlignment="Right" Cursor="SizeNESW" Grid.Row="0" Grid.Column="2" Background="{x:Null}" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Thumb x:Name="ThumbTopLeftCorner" DragDelta="ThumbTopLeftCorner_DragDelta" HorizontalAlignment="Left" Cursor="SizeNWSE" Grid.Row="0" Grid.Column="0" Background="{x:Null}" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}" /> 
      <Thumb x:Name="ThumbBottomLeftCorner" DragDelta="ThumbBottomLeftCorner_DragDelta" HorizontalAlignment="Left" Cursor="SizeNESW" Grid.Row="3" Grid.Column="0" Background="{x:Null}" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}" /> 
      <Thumb x:Name="ThumbRight" DragDelta="ThumbRight_DragDelta" Cursor="SizeWE" Grid.Column="2" Grid.RowSpan="4" Background="{x:Null}" Margin="0,3" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Thumb x:Name="ThumbLeft" DragDelta="ThumbLeft_DragDelta" Cursor="SizeWE" Grid.Column="0" Grid.RowSpan="4" HorizontalContentAlignment="Right" Background="{x:Null}" Margin="0,3" Style="{Binding Mode=OneWay, Source={StaticResource BorderThumb}}"/> 
      <Grid x:Name="Grid_Content" Background="#EFF7FA" Grid.Row="2" Grid.Column="1"> 

      </Grid> 
     </Grid> 
    </Border> 
</Window> 

C#代碼隱藏:

using System.Windows.Interop; 
using winforms = System.Windows.Forms; 

namespace CustomWindowDemo 
{ 
    /// <summary> 
    /// Interaction logic for DemoCustomWindow.xaml 
    /// </summary> 
    public partial class DemoCustomWindow : Window 
    { 
     bool Maximized = false; 
     int NormalWidth = 0; 
     int NormalHeight = 0; 
     int NormalX = 0; 
     int NormalY = 0; 

     public DemoCustomWindow() 
     { 
      InitializeComponent(); 
     } 

     #region Header & Resize 
     void Header_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) 
     { 
      if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed) 
       this.DragMove(); 
     } 

     void ThumbBottomRightCorner_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Width + e.HorizontalChange > 10) 
       this.Width += e.HorizontalChange; 
      if (this.Height + e.VerticalChange > 10) 
       this.Height += e.VerticalChange; 
     } 
     void ThumbTopRightCorner_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Width + e.HorizontalChange > 10) 
       this.Width += e.HorizontalChange; 
      if (this.Top + e.VerticalChange > 10) 
      { 
       this.Top += e.VerticalChange; 
       this.Height -= e.VerticalChange; 
      } 
     } 
     void ThumbTopLeftCorner_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Left + e.HorizontalChange > 10) 
      { 
       this.Left += e.HorizontalChange; 
       this.Width -= e.HorizontalChange; 
      } 
      if (this.Top + e.VerticalChange > 10) 
      { 
       this.Top += e.VerticalChange; 
       this.Height -= e.VerticalChange; 
      } 
     } 
     void ThumbBottomLeftCorner_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Left + e.HorizontalChange > 10) 
      { 
       this.Left += e.HorizontalChange; 
       this.Width -= e.HorizontalChange; 
      } 
      if (this.Height + e.VerticalChange > 10) 
       this.Height += e.VerticalChange; 
     } 
     void ThumbRight_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Width + e.HorizontalChange > 10) 
       this.Width += e.HorizontalChange; 
     } 
     void ThumbLeft_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Left + e.HorizontalChange > 10) 
      { 
       this.Left += e.HorizontalChange; 
       this.Width -= e.HorizontalChange; 
      } 
     } 
     void ThumbBottom_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Height + e.VerticalChange > 10) 
       this.Height += e.VerticalChange; 
     } 
     void ThumbTop_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
     { 
      if (this.Top + e.VerticalChange > 10) 
      { 
       this.Top += e.VerticalChange; 
       this.Height -= e.VerticalChange; 
      } 
     } 

     void btn_Minimize_Click(object sender, RoutedEventArgs e) 
     { 
      WindowState = WindowState.Minimized; 
     } 
     void btn_Maximize_Click(object sender, RoutedEventArgs e) 
     { 
      if (Maximized == true) 
      { 
       this.Width = NormalWidth; 
       this.Height = NormalHeight; 
       this.Left = NormalX; 
       this.Top = NormalY; 
       Maximized = false; 
       Thumbs(); 
      } 
      else 
      { 
       NormalX = (int)this.Left; 
       NormalY = (int)this.Top; 
       NormalHeight = (int)this.Height; 
       NormalWidth = (int)this.Width; 
       this.Left = winforms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Left; 
       this.Top = winforms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Top; 
       this.Width = winforms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Width; 
       this.Height = winforms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Height; 
       Maximized = true; 
       Thumbs(); 
      } 
     } 
     void btn_Close_Click(object sender, RoutedEventArgs e) 
     { 
      Close(); 
     } 
     void Thumbs() 
     { 
      if (Maximized == true) 
      { 
       ThumbBottom.Visibility = Visibility.Collapsed; 
       ThumbLeft.Visibility = Visibility.Collapsed; 
       ThumbTop.Visibility = Visibility.Collapsed; 
       ThumbRight.Visibility = Visibility.Collapsed; 
       ThumbTopLeftCorner.Visibility = Visibility.Collapsed; 
       ThumbTopRightCorner.Visibility = Visibility.Collapsed; 
       ThumbBottomLeftCorner.Visibility = Visibility.Collapsed; 
       ThumbBottomRightCorner.Visibility = Visibility.Collapsed; 
      } 
      else 
      { 
       ThumbBottom.Visibility = Visibility.Visible; 
       ThumbLeft.Visibility = Visibility.Visible; 
       ThumbTop.Visibility = Visibility.Visible; 
       ThumbRight.Visibility = Visibility.Visible; 
       ThumbTopLeftCorner.Visibility = Visibility.Visible; 
       ThumbTopRightCorner.Visibility = Visibility.Visible; 
       ThumbBottomLeftCorner.Visibility = Visibility.Visible; 
       ThumbBottomRightCorner.Visibility = Visibility.Visible; 
      } 
     } 
     #endregion 
    } 
} 
+0

據我所知,有一個屬性Window.RestoreBounds,它正好代表窗口在轉換到正常狀態後返回的大小 – Demarsch

相關問題