My goal is to create window (using WindowsForms) that is resizable like normal Win10 windows but has no titlebar, so I can draw it myself (like UWP apps).
Asked
Active
Viewed 487 times
0
-
Maybe something [like this](https://stackoverflow.com/a/32319579/3110834). – Reza Aghaei Feb 15 '21 at 14:26
-
Or as an option, set the `Padding = 8` for the form and then use a panel with Dock=Fill. And put rest of controls in the panel. – Reza Aghaei Feb 15 '21 at 14:28
-
Thank you for your ideas. Both seem to work, but it would of cause be (much) nicer to be able to resize the window on all sides and not having a big(ger) border around due to adding a padding (Maybe I can fix this with layered window?) ... – ShortDevelopment Feb 15 '21 at 15:38
-
Use the idea of second comment with smaller padding, like 1 – Reza Aghaei Feb 15 '21 at 15:41
-
No, that's not really working cause it is simply to tricky for the user, to hit the resizing area... – ShortDevelopment Feb 15 '21 at 15:52
-
Well you get the idea, use whatever number which looks better :) – Reza Aghaei Feb 15 '21 at 15:54
-
But anyhow, moving the form will be an issue if you dock something to fill. – Reza Aghaei Feb 15 '21 at 15:55
-
I've already found a good solution for moving the window. I'm putting a layered window above my custom title bar, fire the necessary event using `SendMessage` – ShortDevelopment Feb 15 '21 at 16:18
-
Well, if you have a custom title bar, then that's easy, I though you don't have any titlebar. – Reza Aghaei Feb 15 '21 at 16:19
1 Answers
0
I finally found a really good working anwser by modifing this code: https://stackoverflow.com/a/29788300/15213858
What I have now:
public class MyForm : Form
{
//Window Messages
public const uint WM_NCPAINT = 0x85;
public const uint WM_NCCALCSIZE = 0x83;
public const uint WM_NCHITTEST = 0x84;
//RECT Structure
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left, top, right, bottom;
}
//WINDOWPOS Structure
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndinsertafter;
public int x, y, cx, cy;
public int flags;
}
//NCCALCSIZE_PARAMS Structure
[StructLayout(LayoutKind.Sequential)]
public struct NCCALCSIZE_PARAMS
{
public RECT rgrc0, rgrc1, rgrc2;
public WINDOWPOS lppos;
}
//Window Procedure Hook
protected override void WndProc(ref Message m)
{
//Don't style window in designer...
if (DesignMode)
base.WndProc(ref m);
//Handle Message
switch ((uint)m.Msg)
{
case WM_NCCALCSIZE: WmNCCalcSize(ref m); break;
default: base.WndProc(ref m); break;
}
}
//WM_NCCALCSIZE
private void WmNCCalcSize(ref Message m)
{
//Check WPARAM
if (m.WParam != IntPtr.Zero) //TRUE
{
//When TRUE, LPARAM Points to a NCCALCSIZE_PARAMS structure
var nccsp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS));
//We're adjusting the size of the client area here. Right now, the client area is the whole form.
//Adding to the Top, Bottom, Left, and Right will size the client area.
nccsp.rgrc0.top += 0; //30-pixel top border
nccsp.rgrc0.bottom -= 8; //4-pixel bottom (resize) border
nccsp.rgrc0.left += 8; //4-pixel left (resize) border
nccsp.rgrc0.right -= 8; //4-pixel right (resize) border
//Set the structure back into memory
Marshal.StructureToPtr(nccsp, m.LParam, true);
}
else //FALSE
{
//When FALSE, LPARAM Points to a RECT structure
var clnRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
//Like before, we're adjusting the rectangle...
//Adding to the Top, Bottom, Left, and Right will size the client area.
clnRect.top += 0; //30-pixel top border
clnRect.bottom -= 8; //4-pixel bottom (resize) border
clnRect.left += 8; //4-pixel left (resize) border
clnRect.right -= 8; //4-pixel right (resize) border
//Set the structure back into memory
Marshal.StructureToPtr(clnRect, m.LParam, true);
}
//Return Zero
m.Result = IntPtr.Zero;
}
}

ShortDevelopment
- 91
- 1
- 7