【问题标题】:how to make a winform application fullscreen and cover taskbar如何使winform应用程序全屏并覆盖任务栏
【发布时间】:2017-05-28 16:49:43
【问题描述】:

我尝试了How do I make a WinForms app go Full ScreenHow to display a Windows Form in full screen on top of the taskbar 下的所有答案。但它们不能工作(意味着任务栏可见)。
设置 TopMost = true 是一个不好的方法,因为它不能通过 Alt+Tab 切换窗口。

我认为 WindowState = FormWindowState.Maximized 与 AutoSize 冲突。 (我已将 AutoSize 设置为 True,将 AutoSizeMode 设置为 GrowAndShrink 以使其适应面板。)

代码如下(注意FormBorderStyle=和WindowState=的顺序):

public void fullScreenDisplay()
{
    this.currentPanelSize = new Size(this.mainPanel.ClientSize.Width, this.mainPanel.ClientSize.Height);

    this.FormBorderStyle = FormBorderStyle.None;
    //this.TopMost = true;
    //Rectangle ret = Screen.GetWorkingArea(this);
    Rectangle ret = Screen.PrimaryScreen.Bounds;

    this.mainPanel.ClientSize = new Size(ret.Width, ret.Height);
    //this.mainPanel.Dock = DockStyle.Fill;
    this.mainPanel.BringToFront();

    this.WindowState = FormWindowState.Maximized;
}

Designer.cs的代码:

private void InitializeComponent()
{ 
    this.mainPanel = new System.Windows.Forms.Panel();
    this.SuspendLayout();
    // 
    // mainPanel
    // 
    this.mainPanel.Location = new System.Drawing.Point(0, 0);
    this.mainPanel.Margin = new System.Windows.Forms.Padding(0);
    this.mainPanel.Name = "mainPanel";
    this.mainPanel.Size = new System.Drawing.Size(1600, 900);
    this.mainPanel.TabIndex = 0;
    this.mainPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.mainPanel_Paint);
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.AutoSize = true;
    this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
    this.ClientSize = new System.Drawing.Size(120, 0);
    this.Controls.Add(this.mainPanel);
    this.MaximizeBox = false;
    this.Name = "MainForm";
    this.Text = "MainForm";
    this.Load += new System.EventHandler(this.MainForm_Load);
    this.ResumeLayout(false);

}

我正在使用 Windows10。

【问题讨论】:

  • @RezaAghaei 我试过这个解决方案,任务栏也是可见的......
  • 您是否尝试过复制FullScreen 属性并将其设置为true 的确切代码?
  • @RezaAghaei 这就是问题所在。最初在 WindowState.Maximized 中形成。等待 OP 确认以重复关闭
  • @Steve 我将窗口状态设置为Normal,然后将边框样式设置为None,然后将其设置为Maximized。我没有看到解决方案有任何问题。

标签: c# .net winforms


【解决方案1】:

这就是我们正在使用的——我们是多屏的,它们都是全覆盖的任务栏

mainForm.WindowState = FormWindowState.Normal;
mainForm.FormBorderStyle = FormBorderStyle.Sizable;
mainForm.StartPosition = FormStartPosition.Manual;
mainForm.Location = this.SystemScreen.Bounds.Location;
mainForm.FormBorderStyle = FormBorderStyle.None;
mainForm.Size = this.SystemScreen.Bounds.Size;
mainForm.WindowState = FormWindowState.Maximized;

SystemScreenSystem.Windows.Forms.Screen

但这只是为了获取屏幕的尺寸 你可以通过Screen.AllScreens获取你的屏幕

【讨论】:

  • 我把this.SystemScreen换成了Screen.PrimaryScreen,但是任务栏也是可见的..
  • 如果你点击表格,任务栏会消失吗?
【解决方案2】:

这对我来说是全屏并越过任务栏
(还有AutoSize = trueAutoSizeMode = AutoSizeMode.GrowAndShrink

public void fullScreenDisplay()
{
    // This is required if the form reaches this code in maximized state
    // otherwise the TaskBar remains on top of the form
    this.WindowState = FormWindowState.Normal;

    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
    this.mainPanel.Dock = DockStyle.Fill;
    this.BringToFront();
}

换句话说,如果您希望获得默认行为,请不要尝试为表单和面板设置尺寸。而是强制面板停靠在表单的完整尺寸内。

【讨论】:

  • 我发现当我创建一个新项目并使用this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized;时任务栏仍然可见...我不知道这种情况:(
  • 尝试添加this.TopMost = true;
  • @CodeJoy this.TopMost = true 在使用 Tab+Alt 时会导致无法切换窗口。这并不完美。
  • 不知道,但您的任务栏设置可能有些奇怪?右键单击任务栏,然后单击设置。
  • @illyrix 该问题已根据 RhezaAghaei 发布的副本进行了复制。如果表单最初处于 WindowState Maximized 状态,则 TaskBar 保持在顶部。尝试将初始 WindowState 更改为 Normal
猜你喜欢
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多