在上篇文章在winform下实现左右布局多窗口界面的方法已经实现了左右布局多窗口界面,今天本来是研究基于winform的插件编程,没想到顺便又找到了另一种实现方案,这种实现方案更简单,要写的代码也很少,具体实现如下。
可视化设计部份:
1.父窗口:ParentForm的IsMdiContainer 设置为 true,即:this.IsMdiContainer=true;
2.在父窗口中添加一个顶部菜单:menuStrip1,并新增一个菜单项:windows,且将menuStrip1的MdiWindowListItem设置为该windows菜单对象,即: this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem;
3.在父窗口中添加一个树形菜单:treeVIEw1,并将其Dock设为左靠齐,即:this.treeVIEw1.Dock = System.windows.Forms.DockStyle.left;且将margin设为0;
4.在父窗口中添加一个Panel:panel1,且将其wIDth设为3;
以下是设计后自动生成的代码:
namespace WinFormTest{partial class ParentForm{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected overrIDe voID dispose(bool disposing){if (disposing && (components != null)){components.dispose();}base.dispose(disposing);}#region windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>private voID InitializeComponent(){this.menuStrip1 = new System.windows.Forms.MenuStrip();this.windowsToolStripMenuItem = new System.windows.Forms.ToolStripMenuItem();this.treeVIEw1 = new System.windows.Forms.TreeVIEw();this.panel1 = new System.windows.Forms.Panel();this.menuStrip1.SuspendLayout();this.SuspendLayout();// // menuStrip1// this.menuStrip1.Items.AddRange(new System.windows.Forms.ToolStripItem[] {this.windowsToolStripMenuItem});this.menuStrip1.Location = new System.Drawing.Point(0,0);this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem;this.menuStrip1.name = "menuStrip1";this.menuStrip1.Size = new System.Drawing.Size(684,25);this.menuStrip1.TabIndex = 0;this.menuStrip1.Text = "menuStrip1";// // windowsToolStripMenuItem// this.windowsToolStripMenuItem.name = "windowsToolStripMenuItem";this.windowsToolStripMenuItem.Size = new System.Drawing.Size(73,21);this.windowsToolStripMenuItem.Text = "windows";// // treeVIEw1// this.treeVIEw1.Dock = System.windows.Forms.DockStyle.left;this.treeVIEw1.Location = new System.Drawing.Point(0,25);this.treeVIEw1.margin = new System.windows.Forms.padding(0);this.treeVIEw1.name = "treeVIEw1";this.treeVIEw1.Size = new System.Drawing.Size(228,380);this.treeVIEw1.TabIndex = 3;this.treeVIEw1.NodeMouseDoubleClick += new System.windows.Forms.TreeNodeMouseClickEventHandler(this.treeVIEw1_NodeMouseDoubleClick);// // panel1// this.panel1.Anchor = ((System.windows.Forms.AnchorStyles)(((System.windows.Forms.AnchorStyles.top | System.windows.Forms.AnchorStyles.Bottom) | System.windows.Forms.AnchorStyles.left)));this.panel1.Backcolor = System.Drawing.color.Red;this.panel1.borderStyle = System.windows.Forms.borderStyle.FixedSingle;this.panel1.Cursor = System.windows.Forms.Cursors.VSplit;this.panel1.Location = new System.Drawing.Point(230,28);this.panel1.margin = new System.windows.Forms.padding(0);this.panel1.name = "panel1";this.panel1.Size = new System.Drawing.Size(3,100);this.panel1.TabIndex = 5;// // Form1// this.autoScaleDimensions = new System.Drawing.Sizef(6F,12F);this.autoScaleMode = System.windows.Forms.autoScaleMode.Font;this.ClIEntSize = new System.Drawing.Size(684,405);this.Controls.Add(this.panel1);this.Controls.Add(this.treeVIEw1);this.Controls.Add(this.menuStrip1);this.IsMdiContainer = true;this.MainMenuStrip = this.menuStrip1;this.name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.Resize += new System.EventHandler(this.Form1_Resize);this.menuStrip1.ResumeLayout(false);this.menuStrip1.Performlayout();this.ResumeLayout(false);this.Performlayout();}#endregionprivate System.windows.Forms.MenuStrip menuStrip1;private System.windows.Forms.ToolStripMenuItem windowsToolStripMenuItem;private System.windows.Forms.TreeVIEw treeVIEw1;private System.windows.Forms.Panel panel1;}}
编码部份:
其实上面的设计后,如果通过以下定义的方法打开一个子窗口,则直接实现了左右布局且包含多子窗口的界面。
private voID ShowChildForm<TForm>() where TForm : Form,new(){Form childForm = new TForm();childForm.MdiParent = this;childForm.name = "ChildForm - " + DateTime.Now.Millisecond.ToString();childForm.Text = childForm.name;childForm.Show();}
当然仍然有不完美的地方,那就是左边菜单栏宽度不能动态调整,而又没有用到splitContainer,故我们只有自己来实现,其实也很简单,步骤如下:
1.在父窗口构造函数中加入初始化panel1(用作分割器)位置及订阅相关事件,代码如下:
public ParentForm(){InitializeComponent();panel1.MouseDown += panel1_MouseDown;panel1.MouseUp += panel1_MouseUp;panel1.MouseMove += panel1_MouseMove;panel1.top = menuStrip1.Height;panel1.left = treeVIEw1.left + treeVIEw1.WIDth;panel1.Height = panel1.Parent.Height;}
上述代码的作用是:1.保证panel1的高度与位置与左侧树形菜单控件相匹配;2.订阅的三个Mouse事件主要是为了后面实现移动panel1。
2.实现订阅的三个Mouse事件所对应的方法,分别为鼠标按下、鼠标移动、鼠标松开,代码如下:
private bool startMove = false; //用于标记是否在移动中voID panel1_MouseMove(object sender,MouseEventArgs e){if (startMove){panel1.left += e.X;}}voID panel1_MouseUp(object sender,MouseEventArgs e){if (startMove){panel1.left += e.X;startMove = false;this.treeVIEw1.WIDth = panel1.left;}}voID panel1_MouseDown(object sender,MouseEventArgs e){startMove = true;}
上述代码作用:按下鼠标标记为开始移动,然后移动鼠标,若是标记移动中,说明是要移动panel1,故直接将鼠标当前的X坐标位置累加到panel1.left属性上,从而实现移动,当鼠标d起后,则将树形菜单的宽度设置为panel1.left,从而实现树形菜单随panel1的移动而改变大小。
同时为了保证panel1的高度始终与树形菜单相同,在父窗口的Resize方法加入动态调整panel1的高度,代码如下:
private voID ParentForm_Resize(object sender,EventArgs e){panel1.Height = panel1.Parent.Height;}
到此就完成了整个的实现方案,为了便于模拟在树形菜单中双击打开子窗口的效果,同时也添加了如下代码:
private voID ParentForm_Load(object sender,EventArgs e){LoadMenuNodes();}private voID LoadMenuNodes() //实现情况应该是从数据库及用户权限来进行动态创建菜单项{this.treeVIEw1.Nodes.Clear();var root = this.treeVIEw1.Nodes.Add("Root");for (int i = 1; i <= 10; i++){var section = root.Nodes.Add("Section-" + i);int maxNodes = new Random(i).Next(1,10);for (int n = 1; n <= maxNodes; n++){section.Nodes.Add(string.Format("Level-{0}-{1}",i,n));}}}private voID treeVIEw1_NodeMouseDoubleClick(object sender,TreeNodeMouseClickEventArgs e){if (e.Node.Nodes.Count <= 0)//当非父节点(即:实际的功能节点){ShowChildForm<ChildForm>();}}
附上完整的实现代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.linq;using System.Text;using System.Threading.Tasks;using System.windows.Forms;namespace WinFormTest{public partial class ParentForm : Form{private bool startMove = false;public ParentForm(){InitializeComponent();panel1.MouseDown += panel1_MouseDown;panel1.MouseUp += panel1_MouseUp;panel1.MouseMove += panel1_MouseMove;panel1.top = menuStrip1.Height;panel1.left = treeVIEw1.left + treeVIEw1.WIDth;panel1.Height = panel1.Parent.Height;}voID panel1_MouseMove(object sender,MouseEventArgs e){startMove = true;}private voID ParentForm_Load(object sender,EventArgs e){LoadMenuNodes();}private voID treeVIEw1_NodeMouseDoubleClick(object sender,TreeNodeMouseClickEventArgs e){if (e.Node.Nodes.Count <= 0)//当非父节点(即:实际的功能节点){ShowChildForm<ChildForm>();}}private voID ParentForm_Resize(object sender,EventArgs e){panel1.Height = panel1.Parent.Height;}private voID LoadMenuNodes() //实现情况应该是从数据库及用户权限来进行动态创建菜单项{this.treeVIEw1.Nodes.Clear();var root = this.treeVIEw1.Nodes.Add("Root");for (int i = 1; i <= 10; i++){var section = root.Nodes.Add("Section-" + i);int maxNodes = new Random(i).Next(1,n));}}}private voID ShowChildForm<TForm>() where TForm : Form,new(){Form childForm = new TForm();childForm.MdiParent = this;childForm.name = "ChildForm - " + DateTime.Now.Millisecond.ToString();childForm.Text = childForm.name;childForm.Show();}}}
最终效果如下图示:
说明:我这里为了体现分割器,故将其背景色设为红色,便于大家观察,这种解决方案与之前的解决方案功能上是相同的,但有一点小小区别,之前的解决方案中子窗口的标题栏是在父窗口的容器内,而本文的解决方案中子窗口在最大化后,子窗口的标题栏会与父窗口合并,如下图示,至于大家用哪种依实际场景。
关于在winform下实现左右布局多窗口界面的方法之续篇的相关知识就给大家介绍到这里,后续时间我会继续研究winform关于插件式编程(近期工作任务要求),到时候同样会分享给大家,也欢迎大家一起交流,当然高手可以无视。
以上是内存溢出为你收集整理的在winform下实现左右布局多窗口界面的方法之续篇全部内容,希望文章能够帮你解决在winform下实现左右布局多窗口界面的方法之续篇所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)