在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇
在上篇文章在winform下實(shí)現(xiàn)左右布局多窗口界面的方法已經(jīng)實(shí)現(xiàn)了左右布局多窗口界面,今天本來(lái)是研究基于winform的插件編程,沒(méi)想到順便又找到了另一種實(shí)現(xiàn)方案,這種實(shí)現(xiàn)方案更簡(jiǎn)單,要寫(xiě)的代碼也很少,具體實(shí)現(xiàn)如下。
可視化設(shè)計(jì)部份:
1.父窗口:ParentForm的IsMdiContainer 設(shè)置為 true,即:this.IsMdiContainer=true;
2.在父窗口中添加一個(gè)頂部菜單:menuStrip1,并新增一個(gè)菜單項(xiàng):Windows,且將menuStrip1的MdiWindowListItem設(shè)置為該Windows菜單對(duì)象,即: this.menuStrip1.MdiWindowListItem = this.windowsToolStripMenuItem;
3.在父窗口中添加一個(gè)樹(shù)形菜單:treeView1,并將其Dock設(shè)為左靠齊,即:this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;且將margin設(shè)為0;
4.在父窗口中添加一個(gè)Panel:panel1,且將其width設(shè)為3;
以下是設(shè)計(jì)后自動(dòng)生成的代碼:
namespace WinFormTest
{
partial class ParentForm
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem windowsToolStripMenuItem;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel1;
}
}
編碼部份:
其實(shí)上面的設(shè)計(jì)后,如果通過(guò)以下定義的方法打開(kāi)一個(gè)子窗口,則直接實(shí)現(xià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();
}
當(dāng)然仍然有不完美的地方,那就是左邊菜單欄寬度不能動(dòng)態(tài)調(diào)整,而又沒(méi)有用到splitContainer,故我們只有自己來(lái)實(shí)現(xiàn),其實(shí)也很簡(jiǎn)單,步驟如下:
1.在父窗口構(gòu)造函數(shù)中加入初始化panel1(用作分割器)位置及訂閱相關(guān)事件,代碼如下:
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的高度與位置與左側(cè)樹(shù)形菜單控件相匹配;2.訂閱的三個(gè)Mouse事件主要是為了后面實(shí)現(xiàn)移動(dòng)panel1。
2.實(shí)現(xiàn)訂閱的三個(gè)Mouse事件所對(duì)應(yīng)的方法,分別為鼠標(biāo)按下、鼠標(biāo)移動(dòng)、鼠標(biāo)松開(kāi),代碼如下:
private bool startMove = false; //用于標(biāo)記是否在移動(dòng)中
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;
}
上述代碼作用:按下鼠標(biāo)標(biāo)記為開(kāi)始移動(dòng),然后移動(dòng)鼠標(biāo),若是標(biāo)記移動(dòng)中,說(shuō)明是要移動(dòng)panel1,故直接將鼠標(biāo)當(dāng)前的X坐標(biāo)位置累加到panel1.Left屬性上,從而實(shí)現(xiàn)移動(dòng),當(dāng)鼠標(biāo)彈起后,則將樹(shù)形菜單的寬度設(shè)置為panel1.Left,從而實(shí)現(xiàn)樹(shù)形菜單隨panel1的移動(dòng)而改變大小。
同時(shí)為了保證panel1的高度始終與樹(shù)形菜單相同,在父窗口的Resize方法加入動(dòng)態(tài)調(diào)整panel1的高度,代碼如下:
private void ParentForm_Resize(object sender, EventArgs e)
{
panel1.Height = panel1.Parent.Height;
}
到此就完成了整個(gè)的實(shí)現(xiàn)方案,為了便于模擬在樹(shù)形菜單中雙擊打開(kāi)子窗口的效果,同時(shí)也添加了如下代碼:
private void ParentForm_Load(object sender, EventArgs e)
{
LoadMenuNodes();
}
private void LoadMenuNodes() //實(shí)現(xiàn)情況應(yīng)該是從數(shù)據(jù)庫(kù)及用戶(hù)權(quán)限來(lái)進(jìn)行動(dòng)態(tài)創(chuàng)建菜單項(xiàng)
{
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)//當(dāng)非父節(jié)點(diǎn)(即:實(shí)際的功能節(jié)點(diǎn))
{
ShowChildForm<ChildForm>();
}
}
附上完整的實(shí)現(xiàn)代碼:
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)
{
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;
}
private void ParentForm_Load(object sender, EventArgs e)
{
LoadMenuNodes();
}
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Nodes.Count <= 0)//當(dāng)非父節(jié)點(diǎn)(即:實(shí)際的功能節(jié)點(diǎn))
{
ShowChildForm<ChildForm>();
}
}
private void ParentForm_Resize(object sender, EventArgs e)
{
panel1.Height = panel1.Parent.Height;
}
private void LoadMenuNodes() //實(shí)現(xiàn)情況應(yīng)該是從數(shù)據(jù)庫(kù)及用戶(hù)權(quán)限來(lái)進(jìn)行動(dòng)態(tài)創(chuàng)建菜單項(xiàng)
{
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 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();
}
}
}
最終效果如下圖示:


說(shuō)明:我這里為了體現(xiàn)分割器,故將其背景色設(shè)為紅色,便于大家觀察,這種解決方案與之前的解決方案功能上是相同的,但有一點(diǎn)小小區(qū)別,之前的解決方案中子窗口的標(biāo)題欄是在父窗口的容器內(nèi),而本文的解決方案中子窗口在最大化后,子窗口的標(biāo)題欄會(huì)與父窗口合并,如下圖示,至于大家用哪種依實(shí)際場(chǎng)景。

關(guān)于在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇的相關(guān)知識(shí)就給大家介紹到這里,后續(xù)時(shí)間我會(huì)繼續(xù)研究winform關(guān)于插件式編程(近期工作任務(wù)要求),到時(shí)候同樣會(huì)分享給大家,也歡迎大家一起交流,當(dāng)然高手可以無(wú)視。
相關(guān)文章
C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
本文主要介紹了C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成
web程序的高訪問(wèn)量、大數(shù)據(jù)量、高效的用戶(hù)體驗(yàn)度,使靜態(tài)頁(yè)技術(shù)在越來(lái)越多的網(wǎng)站上發(fā)揮作用。這篇文章主要介紹asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成,有需要的朋友可以參考下2015-08-08
C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#讀取Excel表數(shù)據(jù)及導(dǎo)入Sql Server數(shù)據(jù)庫(kù)的具體操作步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
unity實(shí)現(xiàn)場(chǎng)景切換進(jìn)度條顯示
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景切換進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
C#結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新
MinIO是一個(gè)開(kāi)源的對(duì)象存儲(chǔ)服務(wù)器,專(zhuān)門(mén)設(shè)計(jì)用于在大規(guī)模數(shù)據(jù)存儲(chǔ)環(huán)境中運(yùn)行,這篇文章主要為大家介紹了C#如何結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新,需要的可以參考下2024-03-03
c#中SAPI使用總結(jié)——SpVoice的使用方法
最近使用C#重做了點(diǎn)名系統(tǒng)(要用到TTS,讓計(jì)算機(jī)點(diǎn)名)使用了SAPI,在這里總結(jié)一下SpVoice的使用方法。2011-10-10

