我正在尝试设置WPF窗口。
我尝试了以下XAML代码
<Window x:Class="BasicImagingStandAlone"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myUserControl="clr-namespace:WpfUserControlLibrary;assembly=WpfUserControlLibrary"
Title="BasicImagingStandAlone" Icon="desktopicon.png" MinWidth="600" MinHeight="350" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Height="600" Width="1200" WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize">
</Window>
xaml的输出是一个处于最大化状态的窗口,无法移动或调整大小但没有按钮。 我怎样才能立刻达到所有要求?
答案 0 :(得分:3)
这个问题差不多有两年了,但万一有人有相同的要求。
在WPF中,您可以使用帮助程序类HwndSource
挂钩到窗口过程,然后可以使用它来处理窗口消息。
因此,在XAML中设置WindowState="Maximized"
,ResizeMode="CanMinimize"
,并覆盖SourceInitialized
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowState="Maximized" ResizeMode="CanMinimize" SourceInitialized="MainWindow_SourceInitialized">
代码隐藏(来自an answer in this SO question的修改):
private void MainWindow_SourceInitialized(object sender, EventArgs e)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
HwndSource source = HwndSource.FromHwnd(helper.Handle);
source.AddHook(WndProc);
}
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int SC_RESTORE = 0xF120;
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case WM_SYSCOMMAND:
int command = wParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
{
// prevent user from moving the window
handled = true;
}
else if (command == SC_RESTORE && WindowState == WindowState.Maximized)
{
// prevent user from restoring the window while it is maximized
// (but allow restoring when it is minimized)
handled = true;
}
break;
default:
break;
}
return IntPtr.Zero;
}
不要忘记包括:using System.Windows.Interop;
这应符合OP的3项要求:
答案 1 :(得分:2)
我找不到XAML唯一的方法。但是这段代码可以帮到你
显示关闭和最小化按钮
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="CanMinimize" WindowState="Maximized"
Title="MainWindow" Height="350" Width="425">
<Grid>
</Grid>
</Window>
如果您双击窗口标题栏,它将调整为正常,以避免使用以下代码。
public MainWindow()
{
InitializeComponent();
this.SizeChanged += MainWindow_SizeChanged;
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (WindowState == System.Windows.WindowState.Normal)
{
WindowState = System.Windows.WindowState.Maximized;
}
}
希望有所帮助:)
答案 2 :(得分:0)
要触摸窗口的标题栏,您必须按照以下答案中的说明导入user32 API:
Disable Minimize Button, but Keep Cross and Maximize Buttons - WPF, C#
您也可以将ResizeMode设置为CanMinimize。这将禁用最大化按钮。但是为了隐藏它,你将不得不使用如上所述的user32 apis