1

I want this:

Pretty much, my form is resizeable if BorderStyle is set to None and isMDIContainer = false;

But, how do I get my form resizeable if BordeStyle is set to None and isMDICOntainer = true?

https://gyazo.com/6fe87f127a3b2768c152e64d372593c1

This is an example. You can see the form is resizeable just fine. But as soon as the MDI comes in play, it doesn't work anymore.

Here is the current code:

private const int cCaption = 62;
private const int cGrip = 16;
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.Blue, 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);
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
Hehe Idk
  • 11
  • 1
  • See that gray-ish *thing* that covers the MDI Parent's Client area? That's a Control, its Type is MdiClient. Your Form doesn't receive any Paint events related to the Client area (and no `WM_NCHITTEST`) after the MdiClient is created, simply because it's the MdiClient container what you actually see there, not the MDI Parent *background*. The MdiClient has a Handle, though... – Jimi Sep 09 '20 at 11:21
  • BTW, if you decide to use a NativeWindow to handle the MdiClient events (`WM_NCHITTEST` and `WM_PAINT`, mainly), you have to switch the position of the `base.WndProc(ref m);` call: move it on top of the WndProc override and set `m.result = IntPtr.Zero` in the `WM_PAINT` handler. Don't need to use `return`. – Jimi Sep 09 '20 at 12:29

1 Answers1

1

The explanation from Jimi was right. The OnPaint event only draws your Rectangle on the MainForm, while what you see is the MdiClient control. This control covers the background of the MainForm (like you set a panel control and set Dock = fill), so you cannot see and click the rectangle at bottom right to resize.

One way for you to be able to see and click on the rectangle for resizing is set the padding for MainForm, like this:

        protected override void OnClientSizeChanged(EventArgs e)
        {
            if (this.WindowState != lastState || lastState == FormWindowState.Normal)
            {
                lastState = this.WindowState;
                OnWindowStateChange(e);
            }
            base.OnClientSizeChanged(e);
        }

        private void OnWindowStateChange(EventArgs e)
        {
            if (WindowState == FormWindowState.Maximized)
            {
                Padding = new Padding(0);
            }
            else
            {
                Padding = new Padding(7);
            }
        }

So at normal window state (not fullscreen), the MdiClient will not cover all surface of mainform.

enter image description here

I find the color of this is not good. So you may want to change the background of the main form that fit the background of MdiClient, or use this approach to draw rectangles around your form for resizing like normal.

How to resize borderless form from all edges

B.Green
  • 67
  • 7