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

Asked

Viewed 303 times

1

Hello, I’m a small problem, because I need to change the design of my Form related to your border, and the way I found in doing this was changing the property FormBorderStyle for None and making the appropriate design changes, but in doing so I take the key functions of when to be as Sizable, and the functions Minimize, Restore e Close I can still do it with the use of images or buttons, but the function of resizing the form with the mouse the "stretch" and "shrink" I don’t have it anymore. What should I do to get the form back?
What I need is something similar to the new design of the visual studio for example, where it keeps the same functions, but with the custom layout, as can be seen in the image below:

inserir a descrição da imagem aqui

1 answer

1


You can resize the form using the Wndproc method. Run a test using 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 the resize may occur, in this case, a square area of 50 pixels in the lower right corner.

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

Metroframework - Modern UI for Winforms (http://thielj.github.io/MetroFramework/)

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

  • It worked perfectly as you described, around here already gives me an idea of where to go to get what I want, vlw even!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.