MDI里(工具栏)按钮的隐藏、移动和保存备份(xml),子窗体的调用

来源:互联网 发布:手机屏幕锁解锁软件 编辑:程序博客网 时间:2024/06/05 01:50

 mdiParent窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace WindowsApplication2
{
    public partial class MDIParent1 : Form
    {
        private int childFormNumber = 0;
        public static bool beginmove = false; //停止移动
        private bool beginmove2 = false;//停止移动
        public static Point mousePos;         //得到鼠标的值
        private Point mousePos2;        //得到鼠标的值
        private Form1 f;
        private Point p1;               //得到父窗体在屏幕上的坐标
        private Point p2;               //得到子窗体在屏幕上的坐标
        private Point p;                //得到子窗体的起始位置
        private int x1;
        private int x2;
        private int y1;
        private int y2;
        private int x;
        private int y;
        private bool isUP = true;        //判断菜单栏是否在工具栏上方
        private string path;   
        private bool isException = false;//读取xml时判断是否有异常
        private int toolstripScreen;     //得到toolstrip在屏幕上的坐标

        public  MDIParent1()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            // 创建此子窗体的一个新实例。
            Form childForm = new Form();
            // 在显示该窗体前使其成为此 MDI 窗体的子窗体。
            childForm.MdiParent = this;
            childForm.Text = "窗口" + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
                // TODO: 在此处添加打开文件的代码。
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: 在此处添加代码,将窗体的当前内容保存到一个文件中。
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard.GetText() 或 System.Windows.Forms.GetData 从剪贴板中检索信息。
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        #region 判断是否显示toolstrip上的控件
        //分隔符|
        private void toolStripMenuItem16_Click(object sender, EventArgs e)
        {
            if(toolStripMenuItem16.Checked)
                toolStripSeparator1.Visible = false;
            else
                toolStripSeparator1.Visible = true;

        }
        //分隔符|
        private void toolStripMenuItem13_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem13.Checked)
                toolStripSeparator2.Visible = false;
            else
                toolStripSeparator2.Visible = true;
        }
        // 新建
        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem10.Checked)
                newToolStripButton.Visible = false;
            else
                newToolStripButton.Visible = true;

        }
        // 打开
        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem9.Checked)
                openToolStripButton.Visible = false;
            else
                openToolStripButton.Visible = true;
        }
        // 保存
        private void toolStripMenuItem17_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem17.Checked)
                saveToolStripButton.Visible = false;
            else
                saveToolStripButton.Visible = true;
        }
        // 打印
        private void toolStripMenuItem15_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem15.Checked)
                printToolStripButton.Visible = false;
            else
                printToolStripButton.Visible = true;

        }
        // 打印预览
        private void toolStripMenuItem14_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem14.Checked)
                printPreviewToolStripButton.Visible = false;
            else
                printPreviewToolStripButton.Visible = true;

        }
        // 取消
        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItem12.Checked)
                helpToolStripButton.Visible = false;
            else
                helpToolStripButton.Visible = true;
        }
        #endregion
      
        #region load事件
        private void MDIParent1_Load(object sender, EventArgs e)
        {
            toolstripScreen = this.toolStrip.PointToScreen(new Point(0, 0)).Y;
            f = new Form1(panel1);
            f.FormBorderStyle = FormBorderStyle.None;
            f.MdiParent = this;
            f.TopLevel = false;
            f.Parent = panel3;          
            f.StartPosition = FormStartPosition.Manual;
            p1 = this.PointToScreen(new Point(0, 0));
            p2 = f.PointToScreen(new Point(0, 0));
            x1 = p1.X;
            x2 = p2.X;
            y1 = p1.Y;
            y2 = p2.Y;      
            p = new Point(x2 - x1 , y2 - y1 + toolStrip.Height+menuStrip.Height);
            f.Location = p;
            f.Width =  this.Width-10;
            f.Height = this.Height-124;         
            f.WindowState = FormWindowState.Normal;
            f.Show();
                       
            try
            {
                path = AppDomain.CurrentDomain.BaseDirectory;
                string xml = path + "result.xml";
                if (File.Exists(xml))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(xml);
                    toolStripMenuItem10.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0][0]);
                    toolStripMenuItem9.Checked = Convert.ToBoolean(ds.Tables[0].Rows[1][0]);
                    toolStripMenuItem17.Checked = Convert.ToBoolean(ds.Tables[0].Rows[2][0]);
                    toolStripMenuItem16.Checked = Convert.ToBoolean(ds.Tables[0].Rows[3][0]);
                    toolStripMenuItem15.Checked = Convert.ToBoolean(ds.Tables[0].Rows[4][0]);
                    toolStripMenuItem14.Checked = Convert.ToBoolean(ds.Tables[0].Rows[5][0]);
                    toolStripMenuItem13.Checked = Convert.ToBoolean(ds.Tables[0].Rows[6][0]);
                    toolStripMenuItem12.Checked = Convert.ToBoolean(ds.Tables[0].Rows[7][0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(path+"里的xml文档已经损坏,请您先删除该文件!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                isException = true;
                Application.Exit();
            }
            if (toolStripMenuItem10.Checked)
                newToolStripButton.Visible = false;
            else
                newToolStripButton.Visible = true;
            if (toolStripMenuItem9.Checked)
                openToolStripButton.Visible = false;
            else
                openToolStripButton.Visible = true;
            if (toolStripMenuItem17.Checked)
                saveToolStripButton.Visible = false;
            else
                saveToolStripButton.Visible = true;
            if (toolStripMenuItem16.Checked)
                toolStripSeparator1.Visible = false;
            else
                toolStripSeparator1.Visible = true;
            if (toolStripMenuItem15.Checked)
                printToolStripButton.Visible = false;
            else
                printToolStripButton.Visible = true;
            if (toolStripMenuItem14.Checked)
                printPreviewToolStripButton.Visible = false;
            else
                printPreviewToolStripButton.Visible = true;
            if (toolStripMenuItem13.Checked)
                toolStripSeparator2.Visible = false;
            else
                toolStripSeparator2.Visible = true;
            if (toolStripMenuItem12.Checked)
                helpToolStripButton.Visible = false;
            else
                helpToolStripButton.Visible = true;
        }
        #endregion

        #region 关闭窗体的事件
        private void MDIParent1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isException == false)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                DialogResult result = MessageBox.Show(path + this.Text + ".txt" +
                    "的文字已经改变,想保存文件吗?", "", MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question);
                switch (result)
                {
                    case DialogResult.Yes:
                        Save();
                        break;
                    case DialogResult.No:
                        break;
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }
        #endregion

        #region 保存方法
        public void Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string xml = path + "result.xml";
            if (!File.Exists(xml))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(@"<?xml version='1.0' encoding='gb2312'?>
                             <DataResult>
                            
                             <result>" + toolStripMenuItem10.Checked + @"</result>
                            
                             <result>" + toolStripMenuItem9.Checked + @"</result>
                            
                             <result>" + toolStripMenuItem17.Checked + @"</result>
                            
                             <result>" + toolStripMenuItem16.Checked + @"</result>
                            
                             <result>" + toolStripMenuItem15.Checked + @"</result>
                           
                             <result>" + toolStripMenuItem14.Checked + @"</result>
                          
                             <result>" + toolStripMenuItem13.Checked + @"</result>
                           
                             <result>" + toolStripMenuItem12.Checked + @"</result>
                             </DataResult>");
                doc.Save(xml);
            }
            else
            {
                DataSet ds = new DataSet();
                ds.ReadXml(xml);
                ds.Tables[0].Rows[0][0] = toolStripMenuItem10.Checked;
                ds.Tables[0].Rows[1][0] = toolStripMenuItem9.Checked;
                ds.Tables[0].Rows[2][0] = toolStripMenuItem17.Checked;
                ds.Tables[0].Rows[3][0] = toolStripMenuItem16.Checked;
                ds.Tables[0].Rows[4][0] = toolStripMenuItem15.Checked;
                ds.Tables[0].Rows[5][0] = toolStripMenuItem14.Checked;
                ds.Tables[0].Rows[6][0] = toolStripMenuItem13.Checked;
                ds.Tables[0].Rows[7][0] = toolStripMenuItem12.Checked;
                ds.WriteXml(xml);
            }
        }
        #endregion

        #region 拖动事件
        private void toolStrip_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {

                contextMenuStrip1.Items[1].Select();
                contextMenuStrip1.Visible = true;
                contextMenuStrip1.Show(Control.MousePosition);
                beginmove = false;
            }
            if (e.Button == MouseButtons.Left)
            {
                beginmove = true;//开始移动
                mousePos = new Point(e.X, e.Y);
                this.Cursor = System.Windows.Forms.Cursors.SizeAll;

                f.Width = this.Width - 10;
                f.Height = this.Height - 84;
               
                x=e.X;
                y=e.Y;
                p = new Point(x2 - x1-(e.X-x), y2 - y1 + toolStrip.Height+menuStrip.Height-(e.Y-y));
                f.Location = p;
            }
 
        }
        private void toolStrip_MouseMove(object sender, MouseEventArgs e)
        {
            if (beginmove)
            {
                int diffx, diffy;
                diffx = mousePos.X - e.X;
                diffy = mousePos.Y - e.Y;
                this.panel1.Location = new Point(panel1.Location.X - diffx, panel1.Location.Y - diffy);
            }

            toolstripScreen = this.menuStrip.PointToScreen(new Point(0, 0)).Y + menuStrip.Height;
            if (this.toolstripScreen + 2 < toolStrip.PointToScreen(new Point(0, 0)).Y)
            {
                p = new Point(x2 - x1, y2 - y1 + menuStrip.Height);
                f.Location = p;
                isUP = true;
            }
        }

        private void toolStrip_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Cursor = System.Windows.Forms.Cursors.Default;
                if ((this.panel1.Location.Y < this.panel2.Location.Y + this.menuStrip.Height) && isUP == true)
                {
                    panel1.Location = new Point(0, 0);
                    panel2.Location = new Point(0, 24);
                    isUP = false;
                    p = new Point(x2 - x1, y2 - y1 + toolStrip.Height + menuStrip.Height);
                    f.Location = p;
                }
                else
                    if (isUP == false)
                    {
                        panel1.Location = new Point(0, 24);
                        panel2.Location = new Point(0, 0);
                        p = new Point(x2 - x1, y2 - y1 + toolStrip.Height + menuStrip.Height);
                        f.Location = p;
                        isUP = true;
                    }
                    else
                    {
                        p = new Point(x2 - x1, y2 - y1 + menuStrip.Height);
                        f.Location = p;
                        isUP = true;
                    }
                beginmove = false;//停止移动

            }                    
        }

        private void MDIParent1_SizeChanged(object sender, EventArgs e)
        {
            this.panel1.Width = this.Width;
            this.panel2.Width = this.Width;
            f.Width = this.Width - 10;
            f.Height = this.Height - 124;
        }

        private void toolStrip_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            panel1.Location = new Point(0, 24);
            panel2.Location = new Point(0, 0);
            isUP = false;
        }

        private void MDIParent1_ResizeEnd(object sender, EventArgs e)
        {
            f.Width = x1 + this.Width;
            f.Height = y1 + this.Height;
        }
        #endregion

    }
}

 

 

form1窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        private Panel panel1;
        public Form1(Panel panel1)
        {
            this.panel1 = panel1;
            InitializeComponent();a
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.AutoScroll = false;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            textBox2.Text = e.X + "  " + e.Y;

            if (MDIParent1.beginmove)
            {
                int diffx, diffy;
                diffx = e.X;
                diffy = e.Y;
                this.panel1.Location = new Point(diffx - MDIParent1.mousePos.X, diffy + panel1.Height);
            }
           
        }


    }
}

原创粉丝点击