網(wǎng)站首頁 編程語言 正文
場景
現(xiàn)在90%的管理系統(tǒng)都是在用上左右這種布局方式,真可謂是經(jīng)典永流傳。不過,由于現(xiàn)在基本都是Web做的后臺管理系統(tǒng),所以樣式、效果等控制起來都比較方便。但是在WinForm上就很頭疼了,現(xiàn)在還有很大一部分的的布局是采用的上下或者上中下的布局方式,也有一些由于使用了第三方的控件,做了上左右的布局,我本人也是。即便做了好多年Winform了,也沒做過原生上左右布局的主頁面。
前一段時間突然想起來做個小工具,想著就搭個架子出來吧,一直以為使用Mid屬性會很容易實現(xiàn),現(xiàn)實卻告訴我想的太簡單了。
上面的菜單欄和下面的提示欄不用多說。中間左右布局使用splitContainer
即可,當(dāng)我滿心歡喜的把窗口放到panel2中的時候,才發(fā)現(xiàn)一個嚴(yán)重的問題,帶邊框的窗體太丑了,去掉邊框的話,沒辦法對頁面進行更好的管理。而使用Menu的一些屬性監(jiān)聽不到panel中的Form,只能玩Mdi。
需求
所以,綜上場景所述,結(jié)合現(xiàn)在的Web后臺管理系統(tǒng)(Tab布局)。而且就連Win11和Win10都有一些插件支持資源管理器Tab標(biāo)簽了,何不簡單點直接使用TabControl
來實現(xiàn)呢?既方便管理了窗體,又在一定程度解決了窗體的邊框樣式問題。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
Visual Studio 2013
實現(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); } }
實現(xiàn)效果
代碼解析
首先是寫了個自定義控件,用來封裝一些TabControl
的操作,主要實現(xiàn)的有:tab頁加關(guān)閉按鈕,增加選中標(biāo)記,增加右鍵菜單。
然后是主頁面采用了硬編碼的方式加載了菜單,其菜單顯示值對應(yīng)的是Text(自定義),Name對應(yīng)的是Form的名稱,然后通過反射顯示頁面到TabPage中。
原文鏈接:https://mp.weixin.qq.com/s/A-TXv8RogHJ9Kyu4Pzj0cQ
相關(guān)推薦
- 2022-01-11 <meta name=“description“ content=““ />代碼中加入這行代碼的作用
- 2022-09-15 Pycharm虛擬環(huán)境創(chuàng)建并使用命令行指定庫的版本進行安裝_python
- 2022-07-28 Redis基本數(shù)據(jù)類型哈希Hash常用操作命令_Redis
- 2022-09-25 ORACLE數(shù)據(jù)庫數(shù)據(jù)泵的導(dǎo)入導(dǎo)出
- 2023-10-10 微信授權(quán)與拒絕授權(quán)的彈窗處理
- 2023-05-30 Python賦值邏輯的實現(xiàn)_python
- 2022-06-29 Tomcat配置訪問日志和線程數(shù)的實現(xiàn)步驟_Tomcat
- 2022-12-12 BAT腳本接收輸入數(shù)字_DOS/BAT
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支