当前位置:网站首页>C# Winform 最大化遮挡任务栏和全屏显示问题

C# Winform 最大化遮挡任务栏和全屏显示问题

2022-06-24 19:49:00 熊思宇

在打开最大化窗体时,如果不进行配置,那么默认情况下窗体是被任务栏档住,导致有部分界面看不见,看看下面代码的效果,也许对你有帮助

新建一个Winform项目,添加三个按钮,给三个按钮添加点击事件

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Size WindowMaximumSize;

        private void Form1_Load(object sender, EventArgs e)
        {
            WindowMaximumSize = this.MaximumSize;
            this.TopMost = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //任务栏不会被遮挡
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //任务栏会被遮挡
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.MaximumSize = WindowMaximumSize;
                this.WindowState = FormWindowState.Maximized;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //任务栏不会被遮挡
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
        }

    }
}

效果:

点击按钮1,会最大化,但窗体不会遮挡任务栏,

点击按钮2,会全屏显示,

点击按钮3,会最大化,但窗体不会遮挡任务栏,

end

原网站

版权声明
本文为[熊思宇]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38693757/article/details/125445723