C# - 为什么全屏 winform 应用程序不总是覆盖任务栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/118130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Why won't a fullscreen winform app ALWAYS cover the taskbar?
提问by Joel
I'm using Windows Vista and C#.net 3.5, but I had my friend run the program on XP and has the same problem.
我使用的是 Windows Vista 和 C#.net 3.5,但是我让我的朋友在 XP 上运行该程序并且遇到了同样的问题。
So I have a C# program that I have running in the background with an icon in the SystemTray. I have a low level keyboard hook so when I press two keys (Ctr+windows in this case) it'll pull of the application's main form. The form is set to be full screen in the combo key press even handler:
所以我有一个 C# 程序,它在后台运行,在 SystemTray 中有一个图标。我有一个低级键盘钩子,所以当我按下两个键(在这种情况下是 Ctr+windows)时,它会拉出应用程序的主窗体。该表单在组合键按下偶数处理程序中设置为全屏:
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
So it basically works. When I hit CTR+Windows it brings up the form, no matter what program I have given focus to. But sometimes, the taskbar will still show up over the form, which I don't want. I want it to always be full screen when I hit that key combo.
所以它基本上有效。当我点击 CTR+Windows 时,它会显示表单,无论我关注的是哪个程序。但有时,任务栏仍会显示在表单上,这是我不想要的。当我按下那个组合键时,我希望它总是全屏显示。
I figure it has something to do with what application has focus originally. But even when I click on my main form, the taskbar sometimes stays there. So I wonder if focus really is the problem. It just seems like sometimes the taskbar is being stubborn and doesn't want to sit behind my program.
我认为这与最初关注的应用程序有关。但即使当我单击主窗体时,任务栏有时也会停留在那里。所以我想知道焦点是否真的是问题所在。似乎有时任务栏很顽固,不想坐在我的程序后面。
Anyone have any ideas how I can fix this?
任何人都知道我该如何解决这个问题?
EDIT: More details- I'm trying to achieve the same effect that a web browser has when you put it into fullscreen mode, or when you put powerpoint into presentation mode.
编辑:更多详细信息-当您将浏览器置于全屏模式或将 powerpoint 置于演示模式时,我试图实现与网络浏览器相同的效果。
In a windows form you do that by putting the border style to none and maximizing the window. But sometimes the window won't cover the taskbar for some reason. Half the time it will.
在 Windows 窗体中,您可以通过将边框样式设置为无并最大化窗口来实现。但有时由于某种原因窗口不会覆盖任务栏。一半时间会。
If I have the main window topmost, the others will fall behind it when I click on it, which I don't want if the taskbar is hidden.
如果我将主窗口放在最上面,那么当我单击它时,其他窗口将落在它的后面,如果任务栏被隐藏,我不希望这样做。
采纳答案by Ryan Lundy
Try this (where this
is your form):
试试这个(this
你的表格在哪里):
this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;
That'll set the form to fullscreen, and it'll cover the taskbar.
这会将表单设置为全屏,并覆盖任务栏。
回答by jdmichal
As far as I know, the taskbar is either above or below windows based on the "Keep the taskbar on top of other windows" setting. (At least, that's the wording in XP.) I suppose you could try to see if you can detect this setting and toggle it if needed?
据我所知,任务栏基于“将任务栏保持在其他窗口的顶部”设置在窗口上方或下方。(至少,这是 XP 中的措辞。)我想您可以尝试查看是否可以检测到此设置并在需要时切换它?
回答by Paul Beesley
Try resizing the form and bringing it to the front of the z-order like so:
尝试调整表单大小并将其置于 z 顺序的前面,如下所示:
Rectangle screenRect = Screen.GetBounds(this);
this.Location = screenRect.Location;
this.Size = screenRect.Size;
this.BringToFront();
回答by Ronen
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F11)
if (FormBorderStyle == FormBorderStyle.None)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
}
else
{
SuspendLayout();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
ResumeLayout();
}
}
回答by mammadalius
I've tried so many solutions, some of them works on Windows XP and all of them did NOT work on Windows 7. After all I write a simple method to do so.
我已经尝试了很多解决方案,其中一些在 Windows XP 上工作,而所有这些在 Windows 7 上都不起作用。毕竟我写了一个简单的方法来做到这一点。
private void GoFullscreen(bool fullscreen)
{
if (fullscreen)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
}
else
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
the order of code is important and will not work if you change the place of WindwosState and FormBorderStyle.
代码的顺序很重要,如果更改 WindwosState 和 FormBorderStyle 的位置将不起作用。
One of the advantages of this method is leaving the TOPMOST on false that allow other forms to come over the main form.
此方法的优点之一是将 TOPMOST 保留为 false,从而允许其他表单覆盖主表单。
It absolutely solved my problem.
它绝对解决了我的问题。