我想让我的应用程序能够最大化到全屏,这意味着它还可以隐藏Windows任务栏和标题栏。它应该由按钮触发。
我正在尝试像这样开发我的应用程序窗口。
在下方添加我的代码段
<controls:MetroWindow x:Class="EDUI.MainWindow"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:EDiscoveryCore;assembly=EDiscoveryCore"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="eDi" BorderBrush="SkyBlue" BorderThickness="2" Height="999" Width="1071" WindowState="Maximized" x:Name="MainWindows">
答案 0 :(得分:18)
您需要将WindowStyle设置为none,将WindowState设置为Maximized
<Window ...
WindowStyle="None"
WindowState="Maximized">
答案 1 :(得分:12)
您需要将ResizeMode设置为NoResize,将WindowState设置为Maximized
<Window ...
ResizeMode="NoResize" WindowState="Maximized">
答案 2 :(得分:9)
试试这个:
<Window ShowTitleBar="False" IgnoreTaskbarOnMaximize="True">
答案 3 :(得分:6)
如果任务栏没有消失,可能有助于在更改窗口样式之前和之后更改窗口可见性,如下所示:
private void MainWindow_StateChanged(object sender, EventArgs e) {
if (this.WindowState == WindowState.Maximized) {
// hide the window before changing window style
this.Visibility = Visibility.Collapsed;
this.Topmost = true;
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
// re-show the window after changing style
this.Visibility = Visibility.Visible;
}
else {
this.Topmost = false;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.ResizeMode = ResizeMode.CanResize;
}
}
答案 4 :(得分:3)
您只需要将WindowStyle设置为none:
<Window ...
WindowStyle="None">
答案 5 :(得分:1)
我遇到了这个问题,任务栏停留在窗口顶部。我使用的当前解决方案是将窗口设置为Topmost一小段时间,然后将其设置为false(我希望我的窗口能够与Alt + Tab一起使用)
private Timer t;
public void OnLoad()
{
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
StartTopmostTimer(window);
}
private void StartTopmostTimer(Window window)
{
t = new Timer(o => SetTopMostFalse(window), null, 1000, Timeout.Infinite);
}
private void SetTopMostFalse(Window window)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
window.Topmost = false;
}));
t.Dispose();
}
我也使用此代码在全屏和窗口模式之间切换:
public void SwitchFullScreen()
{
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
if (window != null)
{
if (window.WindowStyle == WindowStyle.None)
{
window.WindowStyle = WindowStyle.SingleBorderWindow;
window.WindowState = state;
}
else
{
state = window.WindowState;
window.WindowStyle = WindowStyle.None;
window.WindowState = WindowState.Maximized;
window.Topmost = true;
StartTopmostTimer(window);
}
}
}
答案 6 :(得分:1)
步骤1:使用静态方法编写类使用任务栏的Hide()和Show()
public class Taskbar
{
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
private Taskbar()
{
// hide ctor
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
}
}
步骤2:连接到窗口关闭事件以在窗口将以Alt + F4关闭时返回任务栏
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Taskbar.Show();
}
隐藏任务栏并显示全屏:
Taskbar.Hide();
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
ResizeMode = ResizeMode.NoResize;
Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Height = System.Windows.SystemParameters.PrimaryScreenHeight;
Topmost = true;
Left = 0;
Top = 0;
显示任务栏并在窗口中运行
Taskbar.Show();
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Normal;
ResizeMode = ResizeMode.CanResize;
Width = System.Windows.SystemParameters.WorkArea.Width-100;
Height = System.Windows.SystemParameters.WorkArea.Height-100;
Topmost = false;
Left = 0;
Top = 0;
在Windows 10 1703(创作者更新)上测试
答案 7 :(得分:0)
只需将此事件处理程序挂接到表单的Loaded事件,即可正常工作。
不要在表单的构造函数中应用这些东西(这对我不起作用)。
private void aWindow_Loaded(object sender, RoutedEventArgs e)
{
MaxHeight = SystemParameters.FullPrimaryScreenHeight;
MaxWidth = SystemParameters.FullPrimaryScreenWidth;
Width = SystemParameters.FullPrimaryScreenWidth;
Height = SystemParameters.FullPrimaryScreenHeight;
WindowState = WindowState.Maximized;
ResizeMode = ResizeMode.NoResize;
Left = 0;
Top = 0;
Topmost = true;
ShowInTaskbar = false;
//throw new NotImplementedException();
}
答案 8 :(得分:0)
以上都不对我有用。即使使用Windows API,任务栏也不会隐藏,但是在最大化窗口之后仍然有空间。 有效的方法是不设置Maximized属性,获取桌面的大小并设置以下内容:
Top = 0;
Left = 0;
Width = width_of_your_desktop;
Height = height_of_your_desktop;
甚至不需要设置最高! 要获取屏幕大小,可以使用SystemParameters.PrimaryScreenHeight和PrimaryScreenWidth中的值,或者如果要获取当前窗口所在的屏幕,请使用下面的GetMonitorFromWindow:
[StructLayout(LayoutKind.Sequential)]
private struct MonitorInfo
{
public uint cbSize;
public Rect2 rcMonitor;
public Rect2 rcWork;
public uint dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
private struct Rect2
{
public int left;
public int top;
public int right;
public int bottom;
}
private const int MONITOR_DEFAULTTONULL = 0;
private const int MONITOR_DEFAULTTOPRIMARY = 1;
private const int MONITOR_DEFAULTTONEAREST = 2;
[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags);
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hwnd, ref MonitorInfo mInfo);
public static Rect GetMonitorFromWindow(Window win) {
var mi = new MonitorInfo();
mi.cbSize = (uint)Marshal.SizeOf(mi);
var hwmon = MonitorFromWindow(new System.Windows.Interop.WindowInteropHelper(win).EnsureHandle(), MONITOR_DEFAULTTONULL);
if (hwmon != null && GetMonitorInfo(hwmon, ref mi)) {
//convert to device-independent vaues
var mon = mi.rcMonitor;
Point realp1;
Point realp2;
var trans = PresentationSource.FromVisual(win).CompositionTarget.TransformFromDevice;
realp1 = trans.Transform(new Point(mon.left, mon.top));
realp2 = trans.Transform(new Point(mon.right, mon.bottom));
return new Rect(realp1, realp2);
}
else
throw new Exception("Failed to get monitor info.");
}
答案 9 :(得分:0)
完整可行的解决方案
private void BtnFullScreen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.Topmost = false;
if (WindowState != WindowState.Maximized)
{
WindowState = WindowState.Maximized;
WindowStyle = WindowStyle.None;
btnFullScreen.Source = this.FindResource("FullScreenIn") as System.Windows.Media.ImageSource;
this.ResizeMode = ResizeMode.NoResize;
this.Visibility = Visibility.Collapsed;
this.Topmost = true;
//// re-show the window after changing style
this.Visibility = Visibility.Visible;
MaxHeight = SystemParameters.VirtualScreenHeight; MaxWidth = SystemParameters.VirtualScreenWidth;
}
else
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.SingleBorderWindow;
ResizeMode = ResizeMode.CanResize;
//WindowStartupLocation = WindowStartupLocation.CenterScreen;
Top = 0.0;
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
btnFullScreen.Source = this.FindResource("FullScreenOut") as System.Windows.Media.ImageSource;
}
}