C#: Add move and resize edges to a form with no borders

Posted on Sat 28th Nov 2020 07:22

Modified on Sat 27th Feb 2021 19:32


Confession time, I still write WinForms applications.

I know I should have moved to at least WPF years ago but I didn’t and I haven’t. The problem is compounded by the fact that I’ve gotten pretty good with heavily customising the look of WinForms. Once you disable the form borders and title bar, you have a decent “canvas” to work with. Take our very own Fizzy Launcher. You might hate the design but you must admit that it looks very different from the default windows form.

A consequence of a border-less form is losing the title bar and control box (minimise, maximum and close buttons). Without the title bar you can’t drag and move the window. And without borders you can’t drag the windows edge to resize it.

Moving the form is straight-forward and relatively simple but edge-drag resizing is a little trickier. I never considered it a technical challenge more like an 100-piece puzzle that’s best left for a rainy-day indoors. Enter a global pandemic.

So, I sat down one day (after a nap) and worked out the resizing logic for all eight sides and corner.


 

The Code
The code is somewhat lengthy and a bit ugly. I’d hide it with inheritance.


protected bool isDragging = false;
protected Rectangle lastRectangle = new Rectangle();


public BorderlessForm() : base()
{
    this.FormBorderStyle = FormBorderStyle.None;

    initialiseFormEdge();
}


protected void initialiseFormEdge()
{
    int resizeWidth = 5;

    this.MouseDown += new MouseEventHandler(form_MouseDown);
    this.MouseMove += new MouseEventHandler(form_MouseMove);
    this.MouseUp += delegate (object sender, MouseEventArgs e)
    {
        isDragging = false;
    };

    // bottom
    UserControl uc1 = new UserControl()
    {
        Anchor = (AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right),
        Height = resizeWidth,
        Width = this.DisplayRectangle.Width - (resizeWidth * 2),
        Left = resizeWidth,
        Top = this.DisplayRectangle.Height - resizeWidth,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNS
    };
    uc1.MouseDown += form_MouseDown;
    uc1.MouseUp += form_MouseUp;
    uc1.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            this.Size = new Size(lastRectangle.Width, e.Y - lastRectangle.Y + this.Height);
        }
    };
    uc1.BringToFront();

    this.Controls.Add(uc1);

    // right
    UserControl uc2 = new UserControl()
    {
        Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom),
        Height = this.DisplayRectangle.Height - (resizeWidth * 2),
        Width = resizeWidth,
        Left = this.DisplayRectangle.Width - resizeWidth,
        Top = resizeWidth,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeWE
    };
    uc2.MouseDown += form_MouseDown;
    uc2.MouseUp += form_MouseUp;
    uc2.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            this.Size = new Size(e.X - lastRectangle.X + this.Width, lastRectangle.Height);
        }
    };
    uc2.BringToFront();

    this.Controls.Add(uc2);

    // bottom-right
    UserControl uc3 = new UserControl()
    {
        Anchor = (AnchorStyles.Bottom | AnchorStyles.Right),
        Height = resizeWidth,
        Width = resizeWidth,
        Left = this.DisplayRectangle.Width - resizeWidth,
        Top = this.DisplayRectangle.Height - resizeWidth,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNWSE
    };
    uc3.MouseDown += form_MouseDown;
    uc3.MouseUp += form_MouseUp;
    uc3.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            this.Size = new Size((e.X - lastRectangle.X + this.Width), (e.Y - lastRectangle.Y + this.Height));
        }
    };
    uc3.BringToFront();

    this.Controls.Add(uc3);

    // top-right
    UserControl uc4 = new UserControl()
    {
        Anchor = (AnchorStyles.Top | AnchorStyles.Right),
        Height = resizeWidth,
        Width = resizeWidth,
        Left = this.DisplayRectangle.Width - resizeWidth,
        Top = 0,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNESW
    };
    uc4.MouseDown += form_MouseDown;
    uc4.MouseUp += form_MouseUp;
    uc4.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            int diff = (e.Location.Y - lastRectangle.Y);
            int y = (this.Location.Y + diff);

            this.Location = new Point(this.Location.X, y);
            this.Size = new Size(e.X - lastRectangle.X + this.Width, (this.Height + (diff * -1)));
        }
    };
    uc4.BringToFront();
    //uc4.BackColor = Color.Firebrick;

    this.Controls.Add(uc4);

    // top
    UserControl uc5 = new UserControl()
    {
        Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right),
        Height = resizeWidth,
        Width = this.DisplayRectangle.Width - (resizeWidth * 2),
        Left = resizeWidth,
        Top = 0,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNS
    };
    uc5.MouseDown += form_MouseDown;
    uc5.MouseUp += form_MouseUp;
    uc5.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            int diff = (e.Location.Y - lastRectangle.Y);
            int y = (this.Location.Y + diff);

            this.Location = new Point(this.Location.X, y);
            this.Size = new Size(lastRectangle.Width, (this.Height + (diff * -1)));
        }
    };
    uc5.BringToFront();

    this.Controls.Add(uc5);

    // left
    UserControl uc6 = new UserControl()
    {
        Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom),
        Height = this.DisplayRectangle.Height - (resizeWidth * 2),
        Width = resizeWidth,
        Left = 0,
        Top = resizeWidth,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeWE
    };
    uc6.MouseDown += form_MouseDown;
    uc6.MouseUp += form_MouseUp;
    uc6.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            int diff = (e.Location.X - lastRectangle.X);
            int x = (this.Location.X + diff);

            this.Location = new Point(x, this.Location.Y);
            this.Size = new Size((this.Width + (diff * -1)), this.Height);
        }
    };
    uc6.BringToFront();

    this.Controls.Add(uc6);

    // bottom-left
    UserControl uc7 = new UserControl()
    {
        Anchor = (AnchorStyles.Bottom | AnchorStyles.Left),
        Height = resizeWidth,
        Width = resizeWidth,
        Left = 0,
        Top = this.DisplayRectangle.Height - resizeWidth,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNESW
    };
    uc7.MouseDown += form_MouseDown;
    uc7.MouseUp += form_MouseUp;
    uc7.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            int diff = (e.Location.X - lastRectangle.X);
            int x = (this.Location.X + diff);

            this.Location = new Point(x, this.Location.Y);
            this.Size = new Size((this.Width + (diff * -1)), (e.Y - lastRectangle.Y + this.Height));
        }
    };
    uc7.BringToFront();

    this.Controls.Add(uc7);

    // bottom-left
    UserControl uc8 = new UserControl()
    {
        Anchor = (AnchorStyles.Top | AnchorStyles.Left),
        Height = resizeWidth,
        Width = resizeWidth,
        Left = 0,
        Top = 0,
        BackColor = Color.Transparent,
        Cursor = Cursors.SizeNWSE
    };
    uc8.MouseDown += form_MouseDown;
    uc8.MouseUp += form_MouseUp;
    uc8.MouseMove += delegate (object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            int dX = (e.Location.X - lastRectangle.X);
            int dY = (e.Location.Y - lastRectangle.Y);
            int x = (this.Location.X + dX);
            int y = (this.Location.Y + dY);

            this.Location = new Point(x, y);
            this.Size = new Size((this.Width + (dX * -1)), (this.Height + (dY * -1)));
        }
    };
    uc8.BringToFront();

    this.Controls.Add(uc8);
}


private void form_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        isDragging = true;
        lastRectangle = new Rectangle(e.Location.X, e.Location.Y, this.Width, this.Height);
    }
}

private void form_MouseMove(object sender, MouseEventArgs e)
{
    if (isDragging)
    {
        int x = (this.Location.X + (e.Location.X - lastRectangle.X));
        int y = (this.Location.Y + (e.Location.Y - lastRectangle.Y));

        this.Location = new Point(x, y);
    }
}

private void form_MouseUp(object sender, MouseEventArgs e)
{
    isDragging = false;
}

 

Well, that’s it.

I hope someone finds this interesting or useful.
 

Code Repository

Copyright © 2014-2021 Ray Lam. All Rights Reserved.