how to resize a form without border at runtime

349 views Asked by At

I have this code to resize a form at runtime:

private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTBORDER = 18;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTCAPTION = 2;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;

[DllImport("user32.dll")]
public extern static bool ReleaseCapture();

[DllImport("user32.dll")]
public extern static bool SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private enum ResizeDirection
{
    None,
    Left,
    TopLeft,
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft
}

private ResizeDirection resizeDir;

private ResizeDirection ResizeDir
{
    get { return resizeDir; }
    set
    {
        resizeDir = value;

        switch (resizeDir)
        {
            case ResizeDirection.Left:
                Cursor = Cursors.SizeWE;
                break;

            case ResizeDirection.Right:
                Cursor = Cursors.SizeWE;
                break;

            case ResizeDirection.Top:
                Cursor = Cursors.SizeNS;
                break;

            case ResizeDirection.Bottom:
                Cursor = Cursors.SizeNS;
                break;

            case ResizeDirection.BottomLeft:
                Cursor = Cursors.SizeNESW;
                break;

            case ResizeDirection.TopRight:
                Cursor = Cursors.SizeNESW;
                break;

            case ResizeDirection.BottomRight:
                Cursor = Cursors.SizeNWSE;
                break;

            case ResizeDirection.TopLeft:
                Cursor = Cursors.SizeNWSE;
                break;

            default:
                Cursor = Cursors.Default;
                break;
        }
    }
}

private void ResizeForm(ResizeDirection direction)
{
    int dir = -1;

    switch (direction)
    {
        case ResizeDirection.Left:
            dir = HTLEFT;
            break;

        case ResizeDirection.TopLeft:
            dir = HTTOPLEFT;
            break;

        case ResizeDirection.Top:
            dir = HTTOP;
            break;

        case ResizeDirection.TopRight:
            dir = HTTOPRIGHT;
            break;

        case ResizeDirection.Right:
            dir = HTRIGHT;
            break;

        case ResizeDirection.BottomRight:
            dir = HTBOTTOMRIGHT;
            break;

        case ResizeDirection.Bottom:
            dir = HTBOTTOM;
            break;

        case ResizeDirection.BottomLeft:
            dir = HTBOTTOMLEFT;
            break;
    }

    if (dir != -1)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, dir, 0);
    }
}

protected override void OnMouseDown(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && WindowState != FormWindowState.Maximized)
        ResizeForm(ResizeDir);

    base.OnMouseDown(e);
}

private const int BorderWidth = 6;

protected override void OnMouseMove(MouseEventArgs e)
{
    if (e.Location.X < BorderWidth && e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.TopLeft;

    else if (e.Location.X < BorderWidth && e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.BottomLeft;

    else if (e.Location.X > Width - BorderWidth && e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.BottomRight;

    else if (e.Location.X > Width - BorderWidth && e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.TopRight;

    else if (e.Location.X < BorderWidth)
        ResizeDir = ResizeDirection.Left;

    else if (e.Location.X > Width - BorderWidth)
        ResizeDir = ResizeDirection.Right;

    else if (e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.Top;

    else if (e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.Bottom;

    else ResizeDir = ResizeDirection.None;


    base.OnMouseMove(e);
}

the Code works fine without problems, but I also want to show the system menu on the form also I use this code:

private const int WS_CLIPCHILDREN = 0x2000000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_SYSMENU = 0x80000;
private const int CS_DBLCLKS = 0x8;

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
        cp.ClassStyle = CS_DBLCLKS;

        return cp;
    }
}

When I lay the last code, the resizer code stops working and when I change this code:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; //| WS_SYSMENU;
        cp.ClassStyle = CS_DBLCLKS;

        return cp;
    }
}

the resizer code returns to work again, but the system menu does not longer appear.
What is the solution?

0

There are 0 answers