Search code examples
c#winformsvisual-studiodocking

Having a child form switch between MdiChild and Normal?


I am currently trying the recreate the Docking Panel/forms of visual studio. I want to apply the same system to a different project to handle different window views, but first I want to recreate it.

So far I've been switching the MDiParent property between the MainForm and null, but haven't been able to recreate the smooth transition seen in Visual Studio. It's always janky and half the time I find out the MouseUP event I tie the switch to rarely works (I'm currently using the Location change event).

To move the form, I've been using this handy code block I found;

 #region Form Movement

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

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

    private void MoveForm()
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }

    #endregion

And to Handle the Dock/UnDock, I'm using this code;

    private void DockToMain()
    {
        Point f = Form1.Main.PointToClient(this.Location);
        Point newP = new Point(f.X - 25, f.Y - 51);
        try { this.MdiParent = Form1.Main; } catch { }
        this.Location = newP;
    }

    private void UnDockFromMain()
    {
        Point f = Form1.Main.PointToScreen(this.Location);
        Point newP = new Point(f.X + 25, f.Y + 51);
        try { this.MdiParent = null; } catch { }
        this.Location = newP;
    }

Is there a way to recreate the Dock Panel/Form in Winforms? Am I even on the right track here, or is there a better way I'm not seeing?


Solution

  • Just to Answer this, Will is Completely Right. I Somehow got it to kind of work, but it's far too glitchy to be of any use. Just Switch to WPF (it isn't too difficult to make the jump from WinForms) and use the Avalon dock control from xceedsoftware's " Extended WPF Toolkit"