Change the FormBorderStyle property to None without losing the Sizable functions in C #

1
Hello, I'm a little problem, because I need to change the design of my Form referring to its border, and the way I found it in doing this was to change the property FormBorderStyle to None and make the necessary changes in the design, but when doing this I get the main functions of when to be Sizable , being Minimize, Restore e Close I can still do it with the use of images or buttons, however the function of resizing the form with the mouse "stretch" and "shrink" I do not have it anymore. What should I do to get the form back? Home What I need is something similar to the new design of visual studio for example, where it maintains the same functions, but with the custom layout, as can be seen in the image below:

    
asked by anonymous 15.03.2016 / 14:12

1 answer

1

You can resize the form using the WndProc method. Take a test with the code below:

public partial class Form1 :Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private int borderWidth = 5; //Exemplo apenas para teste
    private new Padding Padding = new Padding(50); //Exemplo apenas para teste (Pode ser especificado direto nas propriedades do Form)

    private WinApi.HitTest HitTestNCA(IntPtr lparam)
    {
        Point vPoint = new Point((Int16)lparam, (Int16)((int)lparam >> 16));
        int vPadding = Math.Max(Padding.Right, Padding.Bottom);

        if (RectangleToScreen(new Rectangle(ClientRectangle.Width - vPadding, ClientRectangle.Height - vPadding, vPadding, vPadding)).Contains(vPoint))
            return WinApi.HitTest.HTBOTTOMRIGHT;

        if (RectangleToScreen(new Rectangle(borderWidth, borderWidth, ClientRectangle.Width - 2 * borderWidth, 50)).Contains(vPoint))
            return WinApi.HitTest.HTCAPTION;

        return WinApi.HitTest.HTCLIENT;
    }

    protected override void WndProc(ref Message m)
    {
        if (DesignMode)
        {
            base.WndProc(ref m);
            return;
        }

        switch (m.Msg)
        {
            case (int) WinApi.Messages.WM_NCHITTEST:
                WinApi.HitTest ht = HitTestNCA(m.LParam);
                if (ht != WinApi.HitTest.HTCLIENT)
                {
                    m.Result = (IntPtr) ht;
                    return;
                }
                break;
        }

        base.WndProc(ref m);
    }

    [SuppressUnmanagedCodeSecurity]
    internal static class WinApi
    {
        public enum Messages : uint
        {
            WM_NCHITTEST = 0x84,
        }

        public enum HitTest
        {
            HTCLIENT = 1,
            HTBOTTOMRIGHT = 17,
            HTCAPTION = 2
        }

    }
}

* Note that the Padding property will determine the area in which resizing may occur, in this case a square area of 50 pixels in the lower right corner.

This code snippet was taken from a framework developed to create Metro-style applications using Windows Forms:

MetroFramework - Modern UI for WinForms ( link )

If the forms and controls they provide do not meet your requirements, you can get the source code and make any necessary changes.

    
27.03.2016 / 23:49