C# - 为什么全屏 winform 应用程序总是覆盖任务栏?

发布于 2024-07-06 02:01:28 字数 769 浏览 5 评论 0原文

我使用的是 Windows Vista 和 C#.net 3.5,但我让我的朋友在 XP 上运行该程序并遇到了同样的问题。

因此,我有一个在后台运行的 C# 程序,并在系统托盘中显示一个图标。 我有一个低级键盘挂钩,因此当我按两个键(在本例中为 Ctr+windows)时,它将拉出应用程序的主窗体。 该表单在组合按键事件处理程序中设置为全屏:

this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;

所以它基本上可以工作。 当我点击 CTR+Windows 时,无论我将焦点放在哪个程序上,它都会显示该表单。 但有时,任务栏仍会显示在表单上,​​这是我不希望的。 我希望当我按下该组合键时它始终为全屏。

我认为这与应用程序最初关注的内容有关。 但即使当我单击主窗体时,任务栏有时也会停留在那里。 所以我想知道注意力是否真的是问题所在。 有时任务栏似乎很顽固,不想坐在我的程序后面。

有人知道我该如何解决这个问题吗?

编辑:更多细节- 我试图实现与将网络浏览器置于全屏模式或将 powerpoint 置于演示模式时相同的效果。

在窗口窗体中,您可以通过将边框样式设置为无并最大化窗口来实现此目的。 但有时由于某种原因窗口不会覆盖任务栏。 有一半的时间会这样。

如果我将主窗口放在最上面,那么当我单击它时,其他窗口将落后于它,如果任务栏被隐藏,我不希望出现这种情况。

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.

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:

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.

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南冥有猫 2024-07-13 02:01:28
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();
        }
}
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();
        }
}
野心澎湃 2024-07-13 02:01:28

我尝试了很多解决方案,其中一些适用于 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;
        }
    }

代码的顺序很重要,如果更改 WindwosState 和 FormBorderStyle 的位置,代码的顺序将不起作用。

此方法的优点之一是将 TOPMOST 设置为 false,以允许其他表单优先于主表单。

它绝对解决了我的问题。

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.

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.

One of the advantages of this method is leaving the TOPMOST on false that allow other forms to come over the main form.

It absolutely solved my problem.

晨与橙与城 2024-07-13 02:01:28

试试这个(其中 this 是您的表单):

this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;

这会将表单设置为全屏,并且它将覆盖任务栏。

Try this (where this is your form):

this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;

That'll set the form to fullscreen, and it'll cover the taskbar.

唐婉 2024-07-13 02:01:28

尝试调整表单大小并将其置于 z 顺序的前面,如下所示:

        Rectangle screenRect = Screen.GetBounds(this);
        this.Location = screenRect.Location;
        this.Size = screenRect.Size;
        this.BringToFront();

Try resizing the form and bringing it to the front of the z-order like so:

        Rectangle screenRect = Screen.GetBounds(this);
        this.Location = screenRect.Location;
        this.Size = screenRect.Size;
        this.BringToFront();
就像说晚安 2024-07-13 02:01:28

据我所知,任务栏位于窗口上方或下方,具体取决于“将任务栏保持在其他窗口之上”设置。 (至少,这是 XP 中的措辞。)我想您可以尝试看看是否可以检测到此设置并在需要时切换它?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文