C# 如何在任务栏顶部全屏显示 Windows 窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2272019/
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
How to display a Windows Form in full screen on top of the taskbar?
提问by myquestionoftheday
I have a .net windows application that needs to run in full screen. When the application starts however the taskbar is shown on top of the main form and it only disappears when activating the form by clicking on it or using ALT-TAB. The form's current properties are as follow:
我有一个需要全屏运行的 .net windows 应用程序。然而,当应用程序启动时,任务栏会显示在主窗体的顶部,只有在通过单击它或使用 ALT-TAB 激活窗体时它才会消失。表单的当前属性如下:
- WindowState=FormWindowState.Normal
- TopMost=Normal
- Size=1024,768 (this is the screen resolution of the machines it's going to be running on)
- FormBorderStyle = None
- WindowState=FormWindowState.Normal
- TopMost=正常
- 大小=1024,768(这是将要运行的机器的屏幕分辨率)
- FormBorderStyle = 无
I've tried adding the followings on form load but none worked for me:
我试过在表单加载时添加以下内容,但没有一个对我有用:
- this.Focus(); (after giving the focus this.Focus property is always false)
- this.BringToFront();
- this.TopMost = true; (this however would not be ideal in my scenario)
- this.Bounds = Screen.PrimaryScreen.Bounds;
- this.Bounds = Screen.PrimaryScreen.Bounds;
- this.Focus(); (在赋予焦点后 this.Focus 属性始终为 false)
- this.BringToFront();
- this.TopMost = true; (然而,这在我的场景中并不理想)
- this.Bounds = Screen.PrimaryScreen.Bounds;
- this.Bounds = Screen.PrimaryScreen.Bounds;
Is there a way to do it within .NET or would I have to invoke native windows methods and if so a code snippet would very much be appreciated.
有没有办法在 .NET 中做到这一点,或者我是否必须调用本机 Windows 方法,如果是这样,非常感谢代码片段。
many thanks
非常感谢
采纳答案by myquestionoftheday
My simple fix it turned out to be calling the form's Activate()
method, so there's no need to use TopMost
(which is what I was aiming at).
我的简单修复原来是调用表单的Activate()
方法,所以没有必要使用TopMost
(这就是我的目标)。
回答by TcKs
Use:
用:
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
And then your form is placed over the taskbar.
然后您的表单被放置在任务栏上。
回答by Christopher B. Adkins
I believe that it can be done by simply setting your FormBorderStyle Property to None and the WindowState to Maximized. If you are using Visual Studio both of those can be found in the IDE so there is no need to do so programmatically. Make sure to include some way of closing/exiting the program before doing this cause this will remove that oh so helpful X in the upper right corner.
我相信它可以通过简单地将 FormBorderStyle 属性设置为 None 并将 WindowState 设置为 Maximized 来完成。如果您使用的是 Visual Studio,则可以在 IDE 中找到这两者,因此无需以编程方式执行此操作。在执行此操作之前,请确保包含关闭/退出程序的某种方式,因为这将删除右上角的那个非常有用的 X。
EDIT:
编辑:
Try this instead. It is a snippet that I have kept for a long time. I can't even remember who to credit for it, but it works.
试试这个。这是我保存了很长时间的片段。我什至不记得该归功于谁,但它确实有效。
/*
* A function to put a System.Windows.Forms.Form in fullscreen mode
* Author: Danny Battison
* Contact: [email protected]
*/
// a struct containing important information about the state to restore to
struct clientRect
{
public Point location;
public int width;
public int height;
};
// this should be in the scope your class
clientRect restore;
bool fullscreen = false;
/// <summary>
/// Makes the form either fullscreen, or restores it to it's original size/location
/// </summary>
void Fullscreen()
{
if (fullscreen == false)
{
this.restore.location = this.Location;
this.restore.width = this.Width;
this.restore.height = this.Height;
this.TopMost = true;
this.Location = new Point(0,0);
this.FormBorderStyle = FormBorderStyle.None;
this.Width = Screen.PrimaryScreen.Bounds.Width;
this.Height = Screen.PrimaryScreen.Bounds.Height;
}
else
{
this.TopMost = false;
this.Location = this.restore.location;
this.Width = this.restore.width;
this.Height = this.restore.height;
// these are the two variables you may wish to change, depending
// on the design of your form (WindowState and FormBorderStyle)
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
回答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.
它绝对解决了我的问题。
回答by Zignd
A tested and simple solution
经过测试的简单解决方案
I've been looking for an answer for this question in SO and some other sites, but one gave an answer was very complex to me and some others answers simply doesn't work correctly, so after a lot code testing I solved this puzzle.
我一直在 SO 和其他一些网站上寻找这个问题的答案,但有人给出的答案对我来说非常复杂,而其他一些答案根本无法正常工作,因此经过大量代码测试后,我解决了这个难题。
Note: I'm using Windows 8and my taskbar isn't on auto-hide mode.
注意:我使用的是Windows 8,我的任务栏未处于自动隐藏模式。
I discovered that setting the WindowState to Normal before performing any modifications will stop the error with the not covered taskbar.
我发现在执行任何修改之前将 WindowState 设置为 Normal 将停止未覆盖任务栏的错误。
The code
编码
I created this class that have two methods, the first enters in the "full screen mode" and the second leaves the "full screen mode". So you just need to create an object of this class and pass the Form you want to set full screen as an argument to the EnterFullScreenMode method or to the LeaveFullScreenMode method:
我创建了这个有两种方法的类,第一个进入“全屏模式”,第二个离开“全屏模式”。所以你只需要创建这个类的一个对象,并将你想要设置全屏的 Form 作为参数传递给 EnterFullScreenMode 方法或 LeaveFullScreenMode 方法:
class FullScreen
{
public void EnterFullScreenMode(Form targetForm)
{
targetForm.WindowState = FormWindowState.Normal;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.WindowState = FormWindowState.Maximized;
}
public void LeaveFullScreenMode(Form targetForm)
{
targetForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
targetForm.WindowState = FormWindowState.Normal;
}
}
Usage example
使用示例
private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
FullScreen fullScreen = new FullScreen();
if (fullScreenMode == FullScreenMode.No) // FullScreenMode is an enum
{
fullScreen.EnterFullScreenMode(this);
fullScreenMode = FullScreenMode.Yes;
}
else
{
fullScreen.LeaveFullScreenMode(this);
fullScreenMode = FullScreenMode.No;
}
}
I have placed this same answer on another question that I'm not sure if is a duplicate or not of this one. (Link to the other question: How do I make a WinForms app go Full Screen)
我已将相同的答案放在另一个问题上,我不确定是否与此问题重复。(链接到另一个问题:如何使 WinForms 应用程序全屏显示)
回答by Wayne LukeSkywalka Deatherage
This is how I make forms full screen.
这就是我使表格全屏的方式。
private void button1_Click(object sender, EventArgs e)
{
int minx, miny, maxx, maxy;
inx = miny = int.MaxValue;
maxx = maxy = int.MinValue;
foreach (Screen screen in Screen.AllScreens)
{
var bounds = screen.Bounds;
minx = Math.Min(minx, bounds.X);
miny = Math.Min(miny, bounds.Y);
maxx = Math.Max(maxx, bounds.Right);
maxy = Math.Max(maxy, bounds.Bottom);
}
Form3 fs = new Form3();
fs.Activate();
Rectangle tempRect = new Rectangle(1, 0, maxx, maxy);
this.DesktopBounds = tempRect;
}
回答by MEO
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
回答by user3647677
FormBorderStyle = FormBorderStyle.Sizable;
TopMost = false;
WindowState = FormWindowState.Normal;
THIS CODE MAKE YOUR WINDOWS FULL SCREEN THIS WILL ALSO COVER WHOLE SCREEN
此代码使您的 WINDOWS 全屏 这也将覆盖整个屏幕
回答by Antonio Lopez
I'm not have an explain on how it works, but works, and being cowboy coder is that all I need.
我没有解释它是如何工作的,但它是有效的,并且成为牛仔编码员就是我所需要的。
System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
this.MaximizedBounds = Screen.GetWorkingArea(this);
this.WindowState = FormWindowState.Maximized;