如何使winform应用程序全屏并覆盖任务栏

时间:2017-01-13 10:32:52

标签: c# .net winforms

我在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。

2 个答案:

答案 0 :(得分: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

获取屏幕

答案 1 :(得分:0)

这会全屏显示在我的任务栏上 (还有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();
}

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