简体   繁体   中英

Windows Forms Move Borderless Window

i have a Windows Forms application which should be borderless (even without the titlebar) and be resizeable and moveable.

so far, i've set the BorderStyle to 'none' which removes all the borders and let my program look pretty.

Now i've added invisble borders with the following:

    private const int cGrip = 16;      // Grip size
    private const int cCaption = 50;   // Caption bar height;

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
        rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
        //e.Graphics.FillRectangle(Brushes.DarkBlue, rc);

    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84)
        {  // Trap WM_NCHITTEST
            Point pos = new Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.Y < cCaption)
            {
                m.Result = (IntPtr)2;  // HTCAPTION
                return;
            }
            if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
            {
                m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                return;
            }
        }

        base.WndProc(ref m);
    }

Program Layout

The blue rectangle is rendered via the OnPaint() method and shows the field where the user is able to move the window while holding the left mouse button.

My problem is, that this rectangle is below my label. Does anyone know how to get the rectangle in front of the label?

Another way would be disabling the label which than turns dark grey. You would solve my problem if i could change the color of my disabled label.

Leave the Label enabled and make it so that dragging the label causes the form to be dragged as well:

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

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

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

private void label1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM