Borderless Form, Drag to top to maximize like a normal form or drag to side to split screen

Viewed 488

not sure i explained it well in the title;

if youre on windows, click and drag on the title bar of FireFox/Chrome/other application and drag it to the top or side of the screen. it creates a weird effect and when u release the capture, depending on where you dragged it, it will either maximize or split the screen.

when i use App.FormBorderStyle = FormBorderStyle.None; it doesnt give me the split/max effect when i drag it to top. i have enabled drag by:

public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    
    // this is the panel i use as a title bar for dragging
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

now, how can i enable this feature of dragging to the top of screen and having windows display the effect of max/split.

thank you.

1 Answers

Maybe you can try to achieve them by determining the location of the form based on Location.X and Location.Y .

Here is a demo you can refer to.

public Form1()
{
    InitializeComponent();
    this.FormBorderStyle = FormBorderStyle.None;
}

// Save original size
Size original;

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

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

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

        // Drag to top to maximize
        if (this.Location.Y <= 0)
        {
            this.WindowState = FormWindowState.Maximized;
        }
        else
        {
            this.Size = original;
        }

        // Drag to side to split screen
        if (this.Location.X <= 0)
        {
            this.Location = new Point(0, 0);
            this.Size = new Size(SystemInformation.WorkingArea.Width / 2, SystemInformation.WorkingArea.Height);
        }
        else if (this.Location.X >= SystemInformation.WorkingArea.Width / 2)
        {
            this.Location = new Point(SystemInformation.WorkingArea.Width / 2, 0);
            this.Size = new Size(SystemInformation.WorkingArea.Width / 2, SystemInformation.WorkingArea.Height);
        }
        else
        {
            this.Size = original;
        }
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    original = this.Size;
}
Related