選單欄[MenuStrip]

清空回聲發表於2015-12-03

1設定完選單後,每一個子選單對應的事件中,來展示當前選單的窗體

  foreach (Form f in this.MdiChildren)
            {
                if (f.Name == "窗體名稱")
                {
                    return;
                }
            }
            FrmYieldTransitEdit fyte = new FrmYieldTransitEdit();
            fyte.MdiParent = this;
            fyte.Show();


2以選項卡的方式開啟一個窗體

  FrmYieldTransitEdit fyte = new FrmYieldTransitEdit();
            //動態建立一個tabpage
            TabPage tabPage1 = new TabPage();
            foreach (TabPage p in tabShowSubMenu.Controls )
            {
                if (p.Name == "FrmYieldTransitEdit")
                {
                    return;
                }
            }
            tabPage1.Text = "生產運送管理";
            tabPage1.Name = "FrmYieldTransitEdit";
          
            fyte.MdiParent = this;
            fyte.TopLevel = false;
            //把窗體作為page新增到tabPage中
            tabPage1.Controls.Add(fyte);
            //新增選項卡tabPage到TabControl中
            tabShowSubMenu.TabPages.Add(tabPage1);
            fyte.Show();
3動態的自定義一個選項卡,把窗體加入到選項卡中

        /// 把窗體作為選項卡的page
        /// </summary>
        /// <param name="str"></param>
        /// <param name="f"></param>
        public void addtabPage(string str, Form myForm)
        {
              //是否存在相同的,相同的就不能再次新增到選項卡中
            if (tabControlCheckHave(this.tabShowSubMenu, str))
            {
                return;
            }
            else
            {
                tabShowSubMenu.TabPages.Add(str);
                tabShowSubMenu.SelectTab(tabShowSubMenu.TabPages.Count - 1);
                myForm.FormBorderStyle = FormBorderStyle.None;
                myForm.Dock = DockStyle.Fill;
                myForm.TopLevel = false;
               
                if (str == "生產任務管理")
                {
                    myForm.Show();//如果在子窗體控制了dgv預設第一行的游標效果去掉後,在這裡面最好先show然後再設定tab
                    myForm.Parent = tabShowSubMenu.SelectedTab;
                   
                }
            }
        }

        /// <summary>
        /// 判斷是否已經存在
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="tabName"></param>
        /// <returns></returns>
        public bool tabControlCheckHave(System.Windows.Forms.TabControl tab, String tabName)
        {

            for (int i = 0; i < tab.TabCount; i++)
            {
                if (tab.TabPages[i].Text == tabName)
                {
                    tab.SelectedIndex = i;
                    return true;
                }
            }
            return false;
        }

  private void tabShowSubMenu_DrawItem(object sender, DrawItemEventArgs e)
        {
            /*如果將 DrawMode 屬性設定為 OwnerDrawFixed, 
            則每當 TabControl 需要繪製它的一個選項卡時,它就會引發 DrawItem 事件*/
            try
            {
                //this.tabControl1.TabPages[e.Index].BackColor = Color.LightBlue;
                Rectangle tabRect = this.tabShowSubMenu.GetTabRect(e.Index);
                e.Graphics.DrawString(this.tabShowSubMenu.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, (float)(tabRect.X + 3), (float)(tabRect.Y + 3));//顯示的字型 的位置
                using (Pen pen = new Pen(Color.Wheat))//x四周的顏色
                {
                    tabRect.Offset(tabRect.Width - 15, 2);
                    tabRect.Width = 15;
                    tabRect.Height = 15;
                    e.Graphics.DrawRectangle(pen, tabRect);
                }
                Color color = (e.State == DrawItemState.Selected) ? Color.GhostWhite : Color.White;
                using (Brush brush = new SolidBrush(color))
                {
                    e.Graphics.FillRectangle(brush, tabRect);
                }
                using (Pen pen2 = new Pen(Color.Red))//x的顏色
                {
                    Point point = new Point(tabRect.X + 3, tabRect.Y + 3);
                    Point point2 = new Point((tabRect.X + tabRect.Width) - 3, (tabRect.Y + tabRect.Height) - 3);
                    e.Graphics.DrawLine(pen2, point, point2);
                    Point point3 = new Point(tabRect.X + 3, (tabRect.Y + tabRect.Height) - 3);
                    Point point4 = new Point((tabRect.X + tabRect.Width) - 3, tabRect.Y + 3);
                    e.Graphics.DrawLine(pen2, point3, point4);
                }
                e.Graphics.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tabShowSubMenu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X;
                int y = e.Y;
                Rectangle tabRect = this.tabShowSubMenu.GetTabRect(this.tabShowSubMenu.SelectedIndex);
                tabRect.Offset(tabRect.Width - 0x12, 2);
                tabRect.Width = 15;
                tabRect.Height = 15;
                if ((((x > tabRect.X) && (x < tabRect.Right)) && (y > tabRect.Y)) && (y < tabRect.Bottom))
                {
                    this.tabShowSubMenu.TabPages.Remove(this.tabShowSubMenu.SelectedTab);
                }
            }
        }



4在點選事件中直接呼叫


 private void YieldtransitMenu_Click(object sender, EventArgs e)
        {
            FrmYieldTransitEdit fyte = new FrmYieldTransitEdit();
            addtabPage("生產運送管理", fyte);

        }


相關文章