利用WinForm實(shí)現(xiàn)上左右布局的方法詳解
場景
現(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)文章
C#中List集合使用Max()方法查找到最大值的實(shí)例
這篇文章主要介紹了C#中List集合使用Max()方法查找到最大值的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12WPF實(shí)現(xiàn)動畫效果(四)之緩動函數(shù)
這篇文章介紹了WPF實(shí)現(xiàn)動畫效果之緩動函數(shù),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個專門為開發(fā)者和研究者基于C#框架設(shè)計(jì)的,這個框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11Unity報(bào)錯InvalidOperationException: out of sync的解決
今天在做個東西,發(fā)現(xiàn)報(bào)錯,特此來記錄一下,本文介紹了Unity報(bào)錯InvalidOperationException: out of sync的解決,感興趣的可以了解一下2021-05-05在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器
這篇文章主要介紹了在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器的方法,是C#事件編程中的基礎(chǔ)知識,需要的朋友可以參考下2016-02-02