删除标题栏但需要C#Windows窗体中的任务栏

我需要从我的窗体中删除标题栏.但是,当我设置

FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

我丢失了标题栏,但同时我看不到任务栏.

所以我需要通过删除标题栏来恢复任务栏.我需要像在附加图片中那样

有人可以帮我吗?

最佳答案 一种方法是告诉窗口最大边界是什么.您可以通过覆盖WM_GETMINMAXINFO窗口消息的默认行为来实现.所以基本上你必须覆盖窗体的WndProc方法.

您还可以更改MaximizedBounds(受保护属性)的值,而不是覆盖WndProc,但如果这样做,则每次将表单移动到另一个屏幕时都必须设置此属性.

    [StructLayout(LayoutKind.Sequential)]
    public class POINT
    {
        public int x;
        public int y;
        public POINT()
        {
        }
        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public class MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        const int WM_GETMINMAXINFO = 0x0024;
        if (m.Msg == WM_GETMINMAXINFO)
        {
            MINMAXINFO minmaxinfo = (MINMAXINFO)m.GetLParam(typeof(MINMAXINFO));
            var screen = Screen.FromControl(this);
            minmaxinfo.ptMaxPosition = new POINT(screen.WorkingArea.X, screen.WorkingArea.Y);
            minmaxinfo.ptMaxSize = new POINT(screen.WorkingArea.Width, screen.WorkingArea.Height);
            Marshal.StructureToPtr(minmaxinfo, m.LParam, false);
        }
    }
点赞