WPF的最大化全屏 [英] WPF full screen on maximize

查看:1465
本文介绍了WPF的最大化全屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想有我的WPF窗口在全屏模式下,当按下F11键或在窗口被按下右上角的最大化按钮去了。

I basically want to have my WPF window to go in full screen mode, when F11 is pressed or the maximize button in the right top corner of the window is pressed.

虽然下面的作品就像一个魅力为按F11:

While the following works like a charm for pressing F11:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F11)
    {
        WindowStyle = WindowStyle.None;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
    }
}

这将仍显示在Windows任务栏上(与Windows测试7):

This will still displays the Windows taskbar (tested with Windows 7):

protected override void OnStateChanged(EventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        WindowStyle = WindowStyle.None;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
    }
    base.OnStateChanged(e);
}



我缺少的是在这里吗?或者,我可以做到这一点更显优雅?

What am I missing here? Or can I do it even more elegant?

推荐答案

WPF似乎是作出有关是否要全屏或尊重的决定基于所述WindowStyle在最大化的时间的任务栏。因此缺憾而有效的解决方案是将切换窗口回非最大化,设置WindowStyle,然后再次设置窗口回最大化

WPF seems to be making the decision about whether to go full-screen or respect the taskbar based on the WindowStyle at the time of maximisation. So a kludgy but effective solution is to switch the window back to non-maximised, set the WindowStyle, and then set the window back to maximised again:

private bool _inStateChange;

protected override void OnStateChanged(EventArgs e)
{
  if (WindowState == WindowState.Maximized && !_inStateChange)
  {
    _inStateChange = true;
    WindowState = WindowState.Normal;
    WindowStyle = WindowStyle.None;
    WindowState = WindowState.Maximized;
    ResizeMode = ResizeMode.NoResize;
    _inStateChange = false;
  }
  base.OnStateChanged(e);
}



虽然代码显然是丑陋的,过渡到正常,然后返回到最大化似乎并没有让用户体验更差。在我的显示,我注意到无论是F11代码和杂牌最大化,而不是对杂牌最大化明显恶化闪烁。但是你的里程可能会有所不同!

Although the code is obviously ugly, the transition to Normal and then back to Maximized doesn't seem to make the user experience any worse. On my display, I noticed flicker with both the F11 code and the kludge maximise, but not noticeably worse on the kludge maximise. But your mileage may vary!

这篇关于WPF的最大化全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆