无边界和可调整大小的表单 (C#)

Borderless and Resizable Form (C#)

我在网上找到了一些代码并复制了它,到目前为止我已经能够把所有事情都做对了,除了一件事我想让表格 (window) 完全无边界。

我使用的是 Visual Studio 2013,这个问题只是关于使表单 (window) 无边界所需的代码。问题是,当你让它无边框时,它不再可以调整大小,但当它有边框时,它可以调整大小。

我知道使用一些代码可以覆盖并实现两者。这是我目前所拥有的(从另一个网站复制的)。这摆脱了带有程序名称的顶部栏,通过单击和拖动表单使表单可移动,并且可以调整大小。

唯一的问题是边界仍然存在。我可以添加什么代码让边框消失?我想保留当前代码,因为它提供了一些我已经需要的功能。

顺便说一下,我查看了一些具有类似主题的旧问题,但没有找到我需要的正确代码。

mod 将我引导至另一个线程: 我尝试了那里的代码,虽然它是一个类似的问题,但这并不是我想要实现的.当我尝试该代码时,无法单击表单 (window) 上的任何位置来移动它。另外,它在右下角有一个可调整大小的角,这不是我要找的。我需要像普通 window.

一样在所有角落和侧面调整大小的功能
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 BoxHider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Next line doesn't seem to be working
            this.FormBorderStyle = FormBorderStyle.None;
        }
        const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 1;
        const int HTCAPTION = 2;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    if (m.Result == (IntPtr)HTCLIENT)
                    {
                        m.Result = (IntPtr)HTCAPTION;
                    }
                    break;
            }
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= 0x40000;
                return cp;
            }
        }               
    }
}

您应该在加载表单后设置边框样式:

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  this.FormBorderStyle = FormBorderStyle.None;
}

试试这个:

public Form1()
{
    InitializeComponent();
    this.FormBorderStyle = FormBorderStyle.None;
}

protected override void WndProc(ref Message m)
{
    const int RESIZE_HANDLE_SIZE = 10;

    switch (m.Msg)
    {
        case 0x0084/*NCHITTEST*/ :
            base.WndProc(ref m);

            if ((int)m.Result == 0x01/*HTCLIENT*/)
            {
                Point screenPoint = new Point(m.LParam.ToInt32());
                Point clientPoint = this.PointToClient(screenPoint);                        
                if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 13/*HTTOPLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 12/*HTTOP*/ ;
                    else
                        m.Result = (IntPtr) 14/*HTTOPRIGHT*/ ;
                }
                else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 10/*HTLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 2/*HTCAPTION*/ ;
                    else
                        m.Result = (IntPtr) 11/*HTRIGHT*/ ;
                }
                else
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 16/*HTBOTTOMLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 15/*HTBOTTOM*/ ;
                    else
                        m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                }
            }
            return;
    }
    base.WndProc(ref m);
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style |= 0x20000; // <--- use 0x20000
        return cp;
    }
}

信息来源:

最近我需要一个与您遇到的相同问题类似的答案。我覆盖了 WndProc 函数并且代码有效。 注意:Titlebar的顶部不能有任何控件,否则将无法使用!

protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x84:
                base.WndProc(ref m);
                if ((int)m.Result == 0x1)
                {
                    if (Cursor.Position.Y <= (this.Location.Y + HeightOfTitleBar) && Cursor.Position.Y >= this.Location.Y + 4)
                        m.Result = (IntPtr)0x2;


                    if (Cursor.Position.Y <= this.Location.Y + this.Height && Cursor.Position.Y >= this.Location.Y + this.Height - 4)
                        m.Result = (IntPtr)15;
                    if (Cursor.Position.Y <= this.Location.Y + 4 && Cursor.Position.Y >= this.Location.Y)
                        m.Result = (IntPtr)12;
                    if (Cursor.Position.X <= this.Location.X + this.Width && Cursor.Position.X >= this.Location.X + this.Width - 4)
                        m.Result = (IntPtr)11;
                    if (Cursor.Position.X <= this.Location.X + 4 && Cursor.Position.X >= this.Location.X)
                        m.Result = (IntPtr)10;


                    if (Cursor.Position.Y <= this.Location.Y + 8 && Cursor.Position.Y >= this.Location.Y && Cursor.Position.X <= this.Location.X + 8 && Cursor.Position.X >= this.Location.X)
                        m.Result = (IntPtr)13;
                    if (Cursor.Position.Y <= this.Location.Y + this.Height && Cursor.Position.Y >= this.Location.Y + this.Height - 8 && Cursor.Position.X <= this.Location.X + this.Width && Cursor.Position.X >= this.Location.X + this.Width - 8)
                        m.Result = (IntPtr)17;
                    if (Cursor.Position.Y <= this.Location.Y + this.Height && Cursor.Position.Y >= this.Location.Y + this.Height - 8 && Cursor.Position.X <= this.Location.X + 8 && Cursor.Position.X >= this.Location.X)
                        m.Result = (IntPtr)16;
                    if (Cursor.Position.Y <= this.Location.Y + 8 && Cursor.Position.Y >= this.Location.Y && Cursor.Position.X <= this.Location.X + this.Width && Cursor.Position.X >= this.Location.X + this.Width - 8)
                        m.Result = (IntPtr)14;
                }
                return;
        }

        base.WndProc(ref m);
    }