winform 에서 다 중 창 인 터 페 이 스 를 좌우 로 배치 하 는 방법의 속편

11161 단어 winform배치
위의 글 에서winform 에서 다 중 창 창 창 을 좌우 로 배치 하 는 방법좌우 레이아웃 다 중 창 인터페이스 를 실 현 했 습 니 다.오늘 은 winform 을 바탕 으로 하 는 플러그 인 프로 그래 밍 을 연 구 했 는데 다른 실현 방안 도 찾 았 습 니 다.이런 실현 방안 은 더욱 간단 하고 쓸 코드 도 적 습 니 다.구체 적 으로 다음 과 같 습 니 다.
시각 화 디자인 부분:
1.부모 창:ParentForm 의 IsMdi Container 를 true 로 설정 합 니 다.즉,this.IsMdi Container=true;
2.부모 창 에 상단 메뉴 를 추가 합 니 다:menuStrip 1.메뉴 항목 을 추가 합 니 다:Windows,그리고 menuStrip 1 의 MdiWindowList Item 을 이 Windows 메뉴 대상 으로 설정 합 니 다.즉,this.menuStrip 1.MdiWindowList Item=this.windows ToolStrip Menu Item;
3.부모 창 에 트 리 메뉴 를 추가 합 니 다:treeView 1.Dock 을 왼쪽 으로 정렬 합 니 다.즉,this.treeView 1.Dock=System.Windows.Forms.DockStyle.Left;margin 을 0 으로 설정 합 니 다.
4.부모 창 에 Panel:panel 1 을 추가 하고 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();
}
#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;
}
}
인 코딩 부분:
사실 위의 디자인 을 통 해 다음 과 같은 정의 방법 으로 하위 창 을 열 면 좌우 레이아웃 과 여러 하위 창 을 포함 하 는 인터페이스 가 직접 실 현 됩 니 다.

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.부모 창 구조 함수 에 panel 1(분할 기 로 사용)위치 초기 화 및 구독 관련 이 벤트 를 추가 합 니 다.코드 는 다음 과 같 습 니 다.

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.panel 1 의 높이 와 위치 가 왼쪽 트 리 메뉴 컨트롤 과 일치 하도록 확보 하 는 것 입 니 다.2.구독 하 는 세 개의 마우스 이 벤트 는 주로 뒤에서 이동 패 널 1 을 실현 하기 위 한 것 입 니 다.
2.구독 을 실현 하 는 세 개의 마우스 이벤트 에 대응 하 는 방법 은 마우스 누 르 기,마우스 이동,마우스 풀 기 입 니 다.코드 는 다음 과 같 습 니 다.

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;
} 
위 코드 기능:마 우 스 를 눌 러 이동 을 시작 한 다음 마 우 스 를 이동 합 니 다.마 우 스 를 이동 할 때 panel 1 을 이동 하 는 것 을 설명 하기 때문에 마우스 의 현재 X 좌표 위 치 를 panel 1.Left 속성 에 누적 하여 이동 을 실현 합 니 다.마우스 가 켜 지면 트 리 메뉴 의 너 비 를 panel 1.Left 로 설정 합 니 다.패 널 1 의 이동 에 따라 트 리 메뉴 의 크기 를 변경 합 니 다.
또한 패 널 1 의 높이 가 트 리 메뉴 와 같 도록 부모 창의 Resize 방법 에 패 널 1 의 높이 를 동적 으로 조정 하기 위해 코드 는 다음 과 같 습 니 다.

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)
{
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)//     ( :       )
{
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, 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();
}
}
}
최종 효 과 는 다음 과 같다.


설명:저 는 분할 기 를 나타 내기 위해 배경 색 을 빨간색 으로 설정 하여 여러분 이 관찰 할 수 있 도록 합 니 다.이런 해결 방안 은 예전 의 해결 방안 기능 과 같 지만 작은 차이 가 있 습 니 다.예전 의 해결 방안 인 중성자 창의 제목 표시 줄 은 부모 창의 용기 안에 있 었 고 본 고의 해결 방안 인 중성자 창 은 최대 화 된 후에하위 창의 제목 표시 줄 은 부모 창 과 합 쳐 집 니 다.다음 그림 은 실제 장면 에 따라 어떤 것 을 사용 하 는 지 보 여 줍 니 다.

winform 에서 다 중 창 인 터 페 이 스 를 좌우 로 배치 하 는 방법 에 대한 속편 에 대한 지식 을 소개 합 니 다.후속 시간 에 저 는 winform 에서 플러그 인 프로 그래 밍(최근 작업 임무 요구)을 계속 연구 할 것 입 니 다.그때 도 여러분 께 공유 할 것 입 니 다.여러분 과 함께 교류 하 는 것 도 환영 합 니 다.물론 고수 들 은 무시 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기