简体   繁体   English

C# - 为什么全屏winform应用程序永远不会覆盖任务栏?

[英]C# - Why won't a fullscreen winform app ALWAYS cover the taskbar?

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. 所以我有一个C#程序,我在后台运行,在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. 我有一个低级键盘钩,所以当我按下两个键(在这种情况下为Ctr +窗口)时,它将拉动应用程序的主窗体。 The form is set to be full screen in the combo key press even handler: 在组合键按下甚至处理程序中将表单设置为全屏:

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. 当我点击CTR + Windows时,它会调出表单,无论我关注哪个程序。 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. 当我按下那个键组合时,我希望它始终是全屏的。

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置于演示模式时,我试图获得与Web浏览器相同的效果。

In a windows form you do that by putting the border style to none and maximizing the window. 在Windows窗体中,您可以将边框样式设置为none并最大化窗口。 But sometimes the window won't cover the taskbar for some reason. 但有时窗口不会出于某种原因覆盖任务栏。 Half the time it will. 一半的时间。

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. 如果我将主窗口放在最顶层,那么当我点击它时其他窗口会落在它后面,如果任务栏被隐藏,我不想要它。

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. 这会将表单设置为全屏,它将覆盖任务栏。

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处于假状态,允许其他形式通过主窗体。

It absolutely solved my problem. 它绝对解决了我的问题。

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();
        }
}

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中的措辞。)我想你可以尝试看看你是否可以检测到这个设置并在需要时进行切换?

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM