MDI and Dock : MDI « GUI Windows Form « C# / C Sharp






MDI and Dock

MDI and Dock
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace DockingWindows
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class MDIMain : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Panel pnlDock;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public MDIMain()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pnlDock = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // pnlDock
            // 
            this.pnlDock.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left);
            this.pnlDock.BackColor = System.Drawing.SystemColors.AppWorkspace;
            this.pnlDock.Name = "pnlDock";
            this.pnlDock.Size = new System.Drawing.Size(148, 302);
            this.pnlDock.TabIndex = 2;
            this.pnlDock.Visible = false;
            this.pnlDock.Paint += new System.Windows.Forms.PaintEventHandler(this.pnlDock_Paint);
            // 
            // MDIMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(534, 304);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.pnlDock});
            this.IsMdiContainer = true;
            this.Name = "MDIMain";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new MDIMain());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            Floater frmFloat = new Floater();
            frmFloat.Owner = this;
            frmFloat.Show();

        }


        public bool DrawDockRectangle
        {
            get
            {
                return pnlDock.Visible;
            }
            set
            {
                pnlDock.Visible = value;
            }
        }

        public void AddToDock(Form frm)
        {
            // Allow the form to be contained in a container control.
            frm.TopLevel = false;
            pnlDock.Controls.Add(frm);

            // Don't let the form be dragged off.
            frm.WindowState = FormWindowState.Maximized;
        }

        private void pnlDock_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            HatchBrush dockCueBrush = new HatchBrush(HatchStyle.LightDownwardDiagonal, 
                Color.White, Color.Gray);
            Pen dockCuePen = new Pen(dockCueBrush, 10);
            e.Graphics.DrawRectangle(dockCuePen, 
                new Rectangle(0, 0, pnlDock.Width, pnlDock.Height));

        }

    }
    /// <summary>
    /// Summary description for Floater.
    /// </summary>
    public class Floater : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.PictureBox PictureBox2;
        internal System.Windows.Forms.Button Button3;
        internal System.Windows.Forms.Button Button2;
        internal System.Windows.Forms.Button Button1;
        internal System.Windows.Forms.Timer tmrDock;
        private System.ComponentModel.IContainer components;

        public Floater()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.PictureBox2 = new System.Windows.Forms.PictureBox();
            this.Button3 = new System.Windows.Forms.Button();
            this.Button2 = new System.Windows.Forms.Button();
            this.Button1 = new System.Windows.Forms.Button();
            this.tmrDock = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // PictureBox2
            // 
            this.PictureBox2.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.PictureBox2.Location = new System.Drawing.Point(1, -3);
            this.PictureBox2.Name = "PictureBox2";
            this.PictureBox2.Size = new System.Drawing.Size(134, 172);
            this.PictureBox2.TabIndex = 2;
            this.PictureBox2.TabStop = false;
            
            // 
            // Button3
            // 
            this.Button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button3.Location = new System.Drawing.Point(14, 56);
            this.Button3.Name = "Button3";
            this.Button3.Size = new System.Drawing.Size(108, 20);
            this.Button3.TabIndex = 7;
            this.Button3.Text = "Controls";
            // 
            // Button2
            // 
            this.Button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button2.Location = new System.Drawing.Point(14, 36);
            this.Button2.Name = "Button2";
            this.Button2.Size = new System.Drawing.Size(108, 20);
            this.Button2.TabIndex = 6;
            this.Button2.Text = "Dockable";
            // 
            // Button1
            // 
            this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button1.Location = new System.Drawing.Point(14, 16);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(108, 20);
            this.Button1.TabIndex = 5;
            this.Button1.Text = "Sample";
            // 
            // tmrDock
            // 
            this.tmrDock.Interval = 10;
            this.tmrDock.Tick += new System.EventHandler(this.tmrDock_Tick);
            // 
            // Floater
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(136, 166);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Button3,
                                                                          this.Button2,
                                                                          this.Button1,
                                                                          this.PictureBox2});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "Floater";
            this.Text = "Floater";
            this.Move += new System.EventHandler(this.Floater_Move);
            this.ResumeLayout(false);

        }
        #endregion


        private Point dockTestAt;

        private void Floater_Move(object sender, System.EventArgs e)
        {
            // Determine the current location in parent form coordinates.
            Point mouseAt = this.Owner.PointToClient(this.Location);

            // Determine if the floated is close enough to dock.
            if (mouseAt.X < 5 && mouseAt.X > -5)
            {
                if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
                {
                    dockTestAt = mouseAt;

                    // Show the dock focus rectangle.
                    ((MDIMain)this.Owner).DrawDockRectangle = true;

                    // Reset the timer to poll for the MouseUp event.
                    tmrDock.Enabled = false;
                    tmrDock.Enabled = true;
                }
            }

        }

        private void tmrDock_Tick(object sender, System.EventArgs e)
        {
            if (dockTestAt.X == this.Owner.PointToClient(this.Location).X 
                && dockTestAt.Y == this.Owner.PointToClient(this.Location).Y)
            {
                if (Control.MouseButtons == MouseButtons.None)
                {
                    // Dock in place.
                    tmrDock.Enabled = false;
                    ((MDIMain)this.Owner).AddToDock(this);
                }
            }
            else
            {
                // Mouse has moved. Disable this dock attempt.
                tmrDock.Enabled = false;
                ((MDIMain)this.Owner).DrawDockRectangle = false;
            }

        }


    }


}



           
       








Related examples in the same category

1.Set MDI parent windowSet MDI parent window
2.Maximized MDI window at startupMaximized MDI window at startup
3.MDI formMDI form
4.MDI RelativesMDI Relatives
5.Form OwnershipForm Ownership
6.MDI BasicsMDI Basics
7.MdiLayout.Cascade MdiLayout.Cascade
8.MdiLayout.TileHorizontal MdiLayout.TileHorizontal
9.MdiLayout.TileVerticalMdiLayout.TileVertical