首页 > 解决方案 > How to lock a windows form in c# to be always be maximized?

问题描述

Im working on a windows form program and I want to make the main form always maximized ,I've tried setting the WindowState to Maximized and FormBorderStyle to FixedDialog/FixedSingle and it works but the maximize button is still there so I tried setting the MaximizeBox to false but then the form is full screen and it totally covers the taskbar which is the problem ,I don't want it to be over the taskbar. If anyone knows a solution or ever an alternative solution to the problem please feel free to help me out.

标签: c#winforms

解决方案


保持FormBorderStyle = Sizable。设置MaximizeBox = falseMinimizeBox = false。作为使用背后的代码

public partial class frmFixedMaximized : Form
{
    private bool _changing;

    public frmFixedMaximized()
    {
        InitializeComponent();
        WindowState = FormWindowState.Maximized;
    }

    private void frmFixedMaximized_Shown(object sender, EventArgs e)
    {
        // Make resizing impossible.
        MinimumSize = Size;
        MaximumSize = Size;
    }

    private void frmFixedMaximized_LocationChanged(object sender, EventArgs e)
    {
        if (!_changing) {
            _changing = true;
            try {
                // Restore maximized state.
                WindowState = FormWindowState.Minimized;
                WindowState = FormWindowState.Maximized;
            } finally {
                _changing = false;
            }
        }
    }
}

这段代码的原因是用户仍然可以通过按住标题栏来拖动窗口。该_changing变量防止LocationChanged事件处理程序在无限循环中触发自身。


推荐阅读