如何禁止窗口自动最大化?

发布于 2024-12-08 07:03:17 字数 2489 浏览 0 评论 0原文

我有一个窗口,我设置了 ResizeMode=NoResize 并删除了所有标题栏和按钮,但问题是当窗口被拖动到屏幕顶部时它会最大化,我无法阻止它。以前有人遇到过这个问题吗?我有代码将窗口最大化和最小化到一定的宽度和高度。

这是示例代码

<Window xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    x:Class="Custom_title_bar.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" WindowStyle="None" AllowsTransparency="True" 
    Background="Transparent" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    ResizeMode="NoResize"  
    MinHeight="180" MinWidth="180"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" 
    VerticalContentAlignment="Center" VerticalAlignment="Top" WindowState="Normal">
<Grid Name="Main" >
   <Border Name="MainWindow" CornerRadius="1" Background="White"   
    BorderBrush="AliceBlue" MouseDown="move_window"  >
      <Grid>
        <DockPanel>
           <DockPanel DockPanel.Dock="Top" Height="26">
              <Border CornerRadius="1">
                 <Border.Background>
                     <LinearGradientBrush>
                        <GradientStop Color="White" Offset="0.0"/>
                        <GradientStop Color="BurlyWood" Offset="0.25"/>
                        <GradientStop Color="Bisque" Offset="0.5"/>
                      </LinearGradientBrush>
                  </Border.Background>
                 <Grid>
                   <DockPanel>  
                    <Image  MouseDown="MINIMIZE" 
                     Source="/Custom%20title%20bar;component/Images/minimize.png" 
                     Grid.ColumnSpan="4" />
                     <Image  MouseDown="MAX_RESTORE" 
                      Source="/Custom%20title%20bar;component/Images/Restore.png"                   
                      Grid.ColumnSpan="4" />
                      <Image  MouseDown="EXIT" 
                      Source="/Custom%20title%20bar;component/Images/close.png"  />
                       <TextBlock/>
                    </DockPanel>
                 </Grid>
            </Border>
         </DockPanel>
       </DockPanel>
      </Grid>
     </Border>
  </Grid>

I have a window that i have set ResizeMode=NoResize and got rid of all the title bar and buttons but the problem is when window is dragged to the top of the screen it maximizes and i am not able to stop that. Does any one faced this problem before where i have code for maximize and minimize the window to a certain width and height.

this is a sample code

<Window xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    x:Class="Custom_title_bar.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" WindowStyle="None" AllowsTransparency="True" 
    Background="Transparent" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    ResizeMode="NoResize"  
    MinHeight="180" MinWidth="180"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" 
    VerticalContentAlignment="Center" VerticalAlignment="Top" WindowState="Normal">
<Grid Name="Main" >
   <Border Name="MainWindow" CornerRadius="1" Background="White"   
    BorderBrush="AliceBlue" MouseDown="move_window"  >
      <Grid>
        <DockPanel>
           <DockPanel DockPanel.Dock="Top" Height="26">
              <Border CornerRadius="1">
                 <Border.Background>
                     <LinearGradientBrush>
                        <GradientStop Color="White" Offset="0.0"/>
                        <GradientStop Color="BurlyWood" Offset="0.25"/>
                        <GradientStop Color="Bisque" Offset="0.5"/>
                      </LinearGradientBrush>
                  </Border.Background>
                 <Grid>
                   <DockPanel>  
                    <Image  MouseDown="MINIMIZE" 
                     Source="/Custom%20title%20bar;component/Images/minimize.png" 
                     Grid.ColumnSpan="4" />
                     <Image  MouseDown="MAX_RESTORE" 
                      Source="/Custom%20title%20bar;component/Images/Restore.png"                   
                      Grid.ColumnSpan="4" />
                      <Image  MouseDown="EXIT" 
                      Source="/Custom%20title%20bar;component/Images/close.png"  />
                       <TextBlock/>
                    </DockPanel>
                 </Grid>
            </Border>
         </DockPanel>
       </DockPanel>
      </Grid>
     </Border>
  </Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2024-12-15 07:03:17

为什么不设置 MaxHeight/MaxWidth 属性?

由于您的默认大小似乎是 SizeToContent="WidthAndHeight",因此最好在 Loaded 中设置 MaxHeight/MaxWidth Window 事件

this.MaxHeight = this.ActualHeight;
this.MaxWidth = this.ActualWidth;

Why don't you set your MaxHeight/MaxWidth properties?

Since your default size seems to be SizeToContent="WidthAndHeight", it might be best to set the MaxHeight/MaxWidth in the Loaded event of your Window

this.MaxHeight = this.ActualHeight;
this.MaxWidth = this.ActualWidth;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文