Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borderless and Resizable Form (C#)

Tags:

c#

border

resize

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

like image 320
Code Man Avatar asked Jul 03 '15 05:07

Code Man


People also ask

How do you make a form resizable in C#?

Open Visual Studio and select "Windows Forms Application" from the list of available templates and name it "DynamicallyPositioningControls". Rename your form to "frmDynamicResizing" by setting its Name property and sets its Text property to "Dynamic Resizing Form".

How do I stop winform from resizing?

Resizing of the form can be disabled by setting the FormBorderStyle property of the form to `FixedDialog`, `FixedSingle`, or `Fixed3D`.

How do I resize Winforms?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.


1 Answers

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:

  • https://stackoverflow.com/a/19670447/2352507
  • Custom Resize Handle in Border-less Form C#
  • http://www.csharp411.com/add-drop-shadow-to-borderless-form/
like image 72
SunsetQuest Avatar answered Oct 18 '22 23:10

SunsetQuest