首页 > 解决方案 > C# 设计模式内带有边距设置的无边框表单

问题描述

我已将边框样式定义为 none 以制作无边框表单,并使用覆盖方法来自定义标题栏。

但是当我在设计模式下放置一个面板控件时,它会覆盖我自定义的标题栏,或者在表单下放置一个statusStrip控件,也会覆盖表单自定义的外部边框线。

在此处输入图像描述

我将面板的顶部/左侧/宽度值设置为限制防止超出窗口,但这不是一个好的解决方案,因为我还想使用 Control.Dock 属性让它“停靠到其父控件”。

我在这里找到了另一种方法: WPF borderless window resize with inner-margins,但它不能。

我想拖动一个新控件然后固定绿色区域 在此处输入图像描述

我可以像 CSS BOX 一样定义“边距”,让所有新控件在设计模式下添加到 Form1,然后自动限制范围吗?

这是我构建无边界表单的代码:

----
Test.cs
----

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace TestForm
{
    public partial class Test : Form
    {
        protected override void WndProc(ref Message m)
        {   
        ....
            base.WndProc(ref m);
        }
        private const int cGrip = 20;      // Grip size
        private const int cCaption = 35;   // Caption bar height;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x84)
            {
                Point pos = new Point(m.LParam.ToInt32());
                pos = this.PointToClient(pos);
                if (pos.Y < cCaption)
                {
                    m.Result = (IntPtr)2;
                    return;
                }
                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)17;
                    return;
                }
            }
            base.WndProc(ref m);
        }
        protected override void OnLoad(EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;
            base.OnLoad(e);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle borderRectangle = this.ClientRectangle;
            borderRectangle.Inflate(0, 0);
            ControlPaint.DrawBorder3D(e.Graphics, borderRectangle, Border3DStyle.Raised);

            base.OnPaint(e);
        }

    }
}

和 Form1.cs:

namespace TestForm
{
    public partial class Form1 : Test
    {
        public Form1()
        {
        //
        }
   }
}

中文注释:

在视听范围内被视听小组控制,造成其“停职,会造成这个安排的父中的标题”,然后将每个人的状态,的原本特制化视窗外框线被盖住。</p>

是否可以让这个从可以直接在设计阶段时,就可以让所有被拖拉进来的 Controls 都限制在一个特定范围内?</p>

标签: c#winforms.net-4.0

解决方案


推荐阅读