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

时间:2015-07-03 05:53:15

标签: c# resize border

我在网上发现了一些代码并将其复制了,到目前为止,我已经能够把所有事情都搞定了,除了一件事我想让窗体(窗口)完全没有边框。

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

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

唯一的问题是边界仍在那里。我可以添加什么代码,这样边框就会消失?我想保留当前的代码,因为它提供了我需要的几个功能。

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

对于指导我到另一个线程的mod:我在那里尝试了代码,虽然这是一个类似的问题,但这并不是我想要实现的。当我尝试该代码时,我无法单击窗体(窗口)上的任何位置来移动它。另外,它右下角有一个可调整大小的角落,这不是我想要的。我需要在所有角落和侧面调整大小,就像普通窗口一样。

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;
            }
        }               
    }
}

What I need

3 个答案:

答案 0 :(得分:10)

试试这个:

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;
    }
}

信息来源:

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

最近我需要一个类似的答案来回答你提出的同一个问题。我覆盖了 WndProc 函数并且代码有效。 注意:标题栏顶部不应有任何控件,否则将无法使用!

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);
    }