欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用WinForm實(shí)現(xiàn)上左右布局的方法詳解

 更新時間:2022年09月06日 09:35:41   作者:Csharp小記  
現(xiàn)在90%的管理系統(tǒng)都是在用上左右這種布局方式,真可謂是經(jīng)典永流傳。本文將利用WinForm實(shí)現(xiàn)上左右布局這一布局效果,感興趣的可以學(xué)習(xí)一下

場景

現(xiàn)在90%的管理系統(tǒng)都是在用上左右這種布局方式,真可謂是經(jīng)典永流傳。不過,由于現(xiàn)在基本都是Web做的后臺管理系統(tǒng),所以樣式、效果等控制起來都比較方便。但是在WinForm上就很頭疼了,現(xiàn)在還有很大一部分的的布局是采用的上下或者上中下的布局方式,也有一些由于使用了第三方的控件,做了上左右的布局,我本人也是。即便做了好多年Winform了,也沒做過原生上左右布局的主頁面。

前一段時間突然想起來做個小工具,想著就搭個架子出來吧,一直以為使用Mid屬性會很容易實(shí)現(xiàn),現(xiàn)實(shí)卻告訴我想的太簡單了。

上面的菜單欄和下面的提示欄不用多說。中間左右布局使用splitContainer即可,當(dāng)我滿心歡喜的把窗口放到panel2中的時候,才發(fā)現(xiàn)一個嚴(yán)重的問題,帶邊框的窗體太丑了,去掉邊框的話,沒辦法對頁面進(jìn)行更好的管理。而使用Menu的一些屬性監(jiān)聽不到panel中的Form,只能玩Mdi。

需求

所以,綜上場景所述,結(jié)合現(xiàn)在的Web后臺管理系統(tǒng)(Tab布局)。而且就連Win11和Win10都有一些插件支持資源管理器Tab標(biāo)簽了,何不簡單點(diǎn)直接使用TabControl來實(shí)現(xiàn)呢?既方便管理了窗體,又在一定程度解決了窗體的邊框樣式問題。

開發(fā)環(huán)境

.NET Framework版本:4.5

開發(fā)工具

Visual Studio 2013

實(shí)現(xiàn)代碼

System.Windows.Forms.ContextMenuStrip MenuStrip = new ContextMenuStrip();
        public UserTabPage()
        {
            InitializeComponent();
            DrawMode = TabDrawMode.OwnerDrawFixed;
            SizeMode = TabSizeMode.Fixed;
            ItemSize = new Size(100, 24);
           


            MenuStrip.Items.Add(new System.Windows.Forms.ToolStripLabel("關(guān)閉其他", null, false, (s, e) =>
            {
                for (int i = 0; i < TabPages.Count; i++)
                {
                    if (i != SelectedIndex)
                    {
                        TabPages.RemoveAt(i);
                        i--;
                    }
                }
            }));


            MenuStrip.Items.Add(new System.Windows.Forms.ToolStripLabel("關(guān)閉所有", null, false, (s, e) =>
            {
                for (int i = 0; i < TabPages.Count; i++)
                {
                    TabPages.RemoveAt(i);
                    i--;
                }
            }));


        }


        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            try
            {
                Rectangle rect = GetTabRect(e.Index);
                string title = TabPages[e.Index].Text;
                if (title.Length > 5)
                {
                    title = title.SubStringByte(10) + "..";
                }
                Brush brush = new SolidBrush(Color.Black);
                Font font = new Font("宋體", 10);


                e.Graphics.DrawString(title, font, brush, new PointF(rect.X + 2, rect.Y + 5));


                e.Graphics.DrawString("X", font, new SolidBrush(Color.OrangeRed), new Point((e.Index + 1) * rect.Width - 15, rect.Y + 5));


                Point x1 = new Point(rect.X, rect.Height);
                Point x2 = new Point((e.Index + 1) * rect.Width, rect.Height);
                if (e.Index == SelectedIndex)
                {
                    e.Graphics.DrawLine(new Pen(Color.Red, 1), x1, x2);
                }
                else
                {
                    e.Graphics.DrawRectangle(new Pen(Color.White, 1), rect);
                }


            }
            catch { }


        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            try
            {
                Point point = e.Location;
                if (e.Button == MouseButtons.Left)
                {
                    Rectangle rect = GetTabRect(SelectedIndex);
                    if (point.X >= (SelectedIndex + 1) * rect.Width - 15)
                    {
                        TabPages.Remove(SelectedTab);
                    }


                }
                else if (e.Button == MouseButtons.Right)
                {
                    for (int i = 0; i < TabPages.Count; i++)
                    {
                        if (GetTabRect(i).Contains(point))
                        {
                            Point p = this.PointToScreen(new Point(e.X, e.Y));
                            SelectedIndex = i;
                            MenuStrip.Show(p);
                            return;
                        }
                    }
                }
            }
            catch { }
        }
  private void MainForm_Load(object sender, EventArgs e)
        {
            TreeNode node = new TreeNode("Form1");
            node.Name = "Form1";
            treeMenu.Nodes.Add(node);
            node = new TreeNode("Form2");
            node.Name = "Form2";
            treeMenu.Nodes.Add(node);
        }


        private void ShowForm(string name, string text)
        {
            try
            {
                foreach (TabPage page in tabForm.TabPages)
                {
                    if (page.Text == text)
                    {
                        tabForm.SelectedTab = page;
                        return;
                    }
                }
                Type t = this.GetType();
                Assembly ass = this.GetType().Assembly;
                Type type = ass.GetType(Assembly.GetExecutingAssembly().GetName().Name + "." + name);


                Form form = System.Activator.CreateInstance(type) as Form;
                form.TopLevel = false;
                form.Text = text;
                form.FormBorderStyle = FormBorderStyle.None;
                form.Dock = DockStyle.Fill;


                TabPage tabPage = new TabPage(form.Text);
                tabPage.AutoScroll = true;
                tabPage.Controls.Add(form);
                tabForm.TabPages.Add(tabPage);
                tabForm.SelectedTab = tabPage;


                form.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private void treeMenu_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Node.Name))
            {
                ShowForm(e.Node.Name, e.Node.Text);
            }
        }

實(shí)現(xiàn)效果

代碼解析

首先是寫了個自定義控件,用來封裝一些TabControl的操作,主要實(shí)現(xiàn)的有:tab頁加關(guān)閉按鈕,增加選中標(biāo)記,增加右鍵菜單。

然后是主頁面采用了硬編碼的方式加載了菜單,其菜單顯示值對應(yīng)的是Text(自定義),Name對應(yīng)的是Form的名稱,然后通過反射顯示頁面到TabPage中。

到此這篇關(guān)于利用WinForm實(shí)現(xiàn)上左右布局的方法詳解的文章就介紹到這了,更多相關(guān)WinForm上左右布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論