14

I found some code online and copied it, so far I have been able to get everything right except for one thing which is I want to make the form (window) completely borderless.

I'm using Visual Studio 2013 and this question is simply about the code that is needed to make the form (window) borderless. The problem is that when you make it borderless, it's no longer resizable but when it has a border, it can be resized.

I know using some code it's possible to override and achieve both. This is what I have so far (copied from another website). This gets rid of the top bar which has the program name, makes the form movable by clicking and dragging the form, and it's resizable.

Only problem is that the border is still there. What code can I add to this so the border will be gone? I want to keep this current code because it's serving a couple of features I need already.

By the way, I looked at some older questions with similar topics but didn't find the right code I need.

For the mod who directed me to another thread: I tried the code there and it's not exactly what I'm trying to achieve although it's a similar problem. When I tried that code, I couldn't click anywhere on the form (window) to move it. Plus, it had one resizable corner at the bottom right which is not what I'm looking for. I need the resize function at all corners and sides just like a normal 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;
            }
        }               
    }
}

What I need

Code Man
  • 181
  • 1
  • 4
  • 10
  • 1
    How will you expect to resize the form with no border to drag?? Just by dragging the edge of the form? – dub stylee Jul 03 '15 at 16:05
  • 1
    The visible border is gone but when you move the mouse to the edge of the form, your cursor changes shape and you are able to resize it by click and drag. – Code Man Jul 03 '15 at 16:38

3 Answers3

21

Try this:

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

informational sources:

Community
  • 1
  • 1
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
  • 4
    Got rid of the border but makes it not resizable. – Code Man Jul 03 '15 at 07:56
  • Try it now. The locations are now manually determined and the correct action is applied. – SunsetQuest Jul 03 '15 at 16:06
  • 1
    This is professional code. It's working exactly how I needed it to. – Code Man Jul 03 '15 at 16:40
  • 1
    Upvote from me too. Best code for resizing I've seen. – LuckyLuke82 Aug 04 '17 at 08:28
  • Thanks @LuckyLuke82, I am glad you and CodeMan enjoyed it. Thanks for taking the time to comment. =) – SunsetQuest Aug 05 '17 at 19:24
  • 2
    Very nice, thanks. Don't forget to add padding to the form – Mutley Sep 13 '19 at 00:52
  • 2
    The only problem is the form must be on the top, not covered by any other control, if you place a control like a dataGridView up to the edge the form is not sizeable –  Jun 16 '20 at 10:26
  • I'm having problem with the Mouse-Events not firing. MouseClick(), MouseDblClick(), MouseEnter(), MouseLeaver() for the whole form are not firing. I'd like to catch e.g. the MouseClick()-event in order to display a context menu with commands like close() and other stuff. Currently I catch the KeyPress()-event but this is not very intuitiv. any Idea how to get the MouseClick() working again? – SUSiS Jan 04 '23 at 20:00
  • @StehtimSchilf - The form itself will not respond to click() anymore but any components like a button still would. You can add a panel to the form and have that be the background. You would need to be careful that the panel does not go all the way to the edges or the form will not be resizable anymore. (so docking a panel will not work) You would then need to add the click() to the panel event. Hopefully, that helps. – SunsetQuest Jan 04 '23 at 23:26
  • @SunsetQuest - I did add a panel to the window and added "padding" to the window. In doing so I successfully can catch the Mouse-events of the panel - BUT: I'm loosing the doubleclick-and-hold feature to drag the window around and as well the double-click feature to maximize-minimize. the doubleclick-and-hold feature is essential for our purpose. There is no code required for this doubleclick-and-hold feature, but can I add this somehow to the panel as well? – SUSiS Jan 05 '23 at 01:40
  • @StehtimSchilf - If you comment out the "HTCAPTION" line it would work I think, but then you would lose the ability to drag it around as the space in the middle will no longer be registered as caption area. You could add more ranges in there for areas so some parts activate set HTCAPTION and other parts do nothing. As soon as m.Result = (IntPtr)2/*HTCAPTION*/ in the middle, it stops the click/double-click from working but also disables the drag-around functionality. – SunsetQuest Jan 05 '23 at 16:35
0

You should set the border style after loading the form:

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  this.FormBorderStyle = FormBorderStyle.None;
}
jmc
  • 1,649
  • 6
  • 26
  • 47
Jaime
  • 5,770
  • 4
  • 23
  • 50
0

Recently I needed a similar answer to the same question you had. I overridden WndProc function and the code works. Note: On the top of the Titlebar there shouldn't be any controls otherwise it will not work!

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);
    }
IlcIliaDev
  • 11
  • 5