Reputation: 301
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;
}
}
}
}
Upvotes: 15
Views: 12039
Reputation: 11
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);
}
Upvotes: 0
Reputation: 8817
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:
Upvotes: 22
Reputation: 5965
You should set the border style after loading the form:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.FormBorderStyle = FormBorderStyle.None;
}
Upvotes: 0