C#设计树形程序界面的方法:创建特殊窗体

目录

1.TreeView控件

2.实例 

(1)Resources.Designer.cs

(2)Form1.Designer.cs

(3)Form1.cs

(4)生成效果


        以树形来显示程序的菜单,可以更直观、更快捷地对窗体进行操作,而且树型菜单比菜单栏更加美观实用。

1.TreeView控件

        TreeView控件又称为树控件,它可以为用户显示节点层次结构,而每个节点又可以包含子节点,包含子节点的节点叫父节点,其效果就像在Windows操作系统的Windows资源管理器功能的左窗口中显示文件和文件夹一样。TreeView控件的ExpandAll方法用来展开所有树节点,语法格式如下:

public void ExpandAll()

        TreeView控件经常用来设计Windows窗体的左侧导航菜单。

2.实例 

        本实例在制作以树形显示的程序界面时,主要用TreeView控件及其ExpandAll方法。 

(1)Resources.Designer.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace _194.Properties {
    using System;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 StronglyTypedResourceBuilder
    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    // (以 /str 作为命令选项),或重新生成 VS 项目。
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_194.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 CurrentUICulture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap 上 {
            get {
                object obj = ResourceManager.GetObject("上", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap 右 {
            get {
                object obj = ResourceManager.GetObject("右", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
    }
}

(2)Form1.Designer.cs

namespace _194
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            TreeNode treeNode1 = new TreeNode("企业类型设置");
            TreeNode treeNode2 = new TreeNode("企业性质设置");
            TreeNode treeNode3 = new TreeNode("企业级别设置");
            TreeNode treeNode4 = new TreeNode("企业资信设置");
            TreeNode treeNode5 = new TreeNode("基础信息维护", new TreeNode[] { treeNode1, treeNode2, treeNode3, treeNode4 });
            TreeNode treeNode6 = new TreeNode("客户信息");
            TreeNode treeNode7 = new TreeNode("联系人信息");
            TreeNode treeNode8 = new TreeNode("业务往来");
            TreeNode treeNode9 = new TreeNode("客户信息维护", new TreeNode[] { treeNode6, treeNode7, treeNode8 });
            TreeNode treeNode10 = new TreeNode("客户投诉");
            TreeNode treeNode11 = new TreeNode("客户反馈");
            TreeNode treeNode12 = new TreeNode("客户服务", new TreeNode[] { treeNode10, treeNode11 });
            TreeNode treeNode13 = new TreeNode("联系人信息查询");
            TreeNode treeNode14 = new TreeNode("客户信息查询");
            TreeNode treeNode15 = new TreeNode("客户信息查询", new TreeNode[] { treeNode13, treeNode14 });
            TreeNode treeNode16 = new TreeNode("客户信息报表");
            TreeNode treeNode17 = new TreeNode("业务往来报表");
            TreeNode treeNode18 = new TreeNode("联系人信息表");
            TreeNode treeNode19 = new TreeNode("打印报表", new TreeNode[] { treeNode16, treeNode17, treeNode18 });
            TreeNode treeNode20 = new TreeNode("调用Word");
            TreeNode treeNode21 = new TreeNode("调用EXCEL");
            TreeNode treeNode22 = new TreeNode("辅助工具", new TreeNode[] { treeNode20, treeNode21 });
            menuStrip1 = new MenuStrip();
            toolStripMenuItem2 = new ToolStripMenuItem();
            toolStripMenuItem3 = new ToolStripMenuItem();
            toolStripMenuItem4 = new ToolStripMenuItem();
            toolStripMenuItem1 = new ToolStripMenuItem();
            panel1 = new Panel();
            splitContainer1 = new SplitContainer();
            treeView1 = new TreeView();
            pictureBox1 = new PictureBox();
            menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
            splitContainer1.Panel1.SuspendLayout();
            splitContainer1.Panel2.SuspendLayout();
            splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
            SuspendLayout();
            // 
            // menuStrip1
            // 
            menuStrip1.Dock = DockStyle.None;
            menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3, toolStripMenuItem4, toolStripMenuItem1 });
            menuStrip1.Location = new Point(0, 0);
            menuStrip1.Name = "menuStrip1";
            menuStrip1.Size = new Size(352, 25);
            menuStrip1.TabIndex = 0;
            menuStrip1.Text = "menuStrip1";
            // 
            // toolStripMenuItem2
            // 
            toolStripMenuItem2.Name = "toolStripMenuItem2";
            toolStripMenuItem2.Size = new Size(92, 21);
            toolStripMenuItem2.Text = "客户信息维护";
            // 
            // toolStripMenuItem3
            // 
            toolStripMenuItem3.Name = "toolStripMenuItem3";
            toolStripMenuItem3.Size = new Size(68, 21);
            toolStripMenuItem3.Text = "客户服务";
            // 
            // toolStripMenuItem4
            // 
            toolStripMenuItem4.Name = "toolStripMenuItem4";
            toolStripMenuItem4.Size = new Size(92, 21);
            toolStripMenuItem4.Text = "客户信息查询";
            // 
            // toolStripMenuItem1
            // 
            toolStripMenuItem1.Name = "toolStripMenuItem1";
            toolStripMenuItem1.Size = new Size(92, 21);
            toolStripMenuItem1.Text = "基础信息查询";
            // 
            // panel1
            // 
            panel1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            panel1.BackgroundImage = Properties.Resources.上;
            panel1.BackgroundImageLayout = ImageLayout.Stretch;
            panel1.Location = new Point(0, 28);
            panel1.Name = "panel1";
            panel1.Size = new Size(496, 40);
            panel1.TabIndex = 1;
            // 
            // splitContainer1
            // 
            splitContainer1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            splitContainer1.Location = new Point(0, 65);
            splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            splitContainer1.Panel1.Controls.Add(treeView1);
            // 
            // splitContainer1.Panel2
            // 
            splitContainer1.Panel2.BackgroundImageLayout = ImageLayout.Stretch;
            splitContainer1.Panel2.Controls.Add(pictureBox1);
            splitContainer1.Size = new Size(496, 282);
            splitContainer1.SplitterDistance = 165;
            splitContainer1.TabIndex = 2;
            // 
            // treeView1
            // 
            treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
            treeView1.Location = new Point(0, 0);
            treeView1.Name = "treeView1";
            treeNode1.Name = "节点6";
            treeNode1.Text = "企业类型设置";
            treeNode2.Name = "节点7";
            treeNode2.Text = "企业性质设置";
            treeNode3.Name = "节点8";
            treeNode3.Text = "企业级别设置";
            treeNode4.Name = "节点9";
            treeNode4.Text = "企业资信设置";
            treeNode5.Name = "节点0";
            treeNode5.Text = "基础信息维护";
            treeNode6.Name = "节点10";
            treeNode6.Text = "客户信息";
            treeNode7.Name = "节点11";
            treeNode7.Text = "联系人信息";
            treeNode8.Name = "节点12";
            treeNode8.Text = "业务往来";
            treeNode9.Name = "节点1";
            treeNode9.Text = "客户信息维护";
            treeNode10.Name = "节点13";
            treeNode10.Text = "客户投诉";
            treeNode11.Name = "节点14";
            treeNode11.Text = "客户反馈";
            treeNode12.Name = "节点2";
            treeNode12.Text = "客户服务";
            treeNode13.Name = "节点15";
            treeNode13.Text = "联系人信息查询";
            treeNode14.Name = "节点16";
            treeNode14.Text = "客户信息查询";
            treeNode15.Name = "节点3";
            treeNode15.Text = "客户信息查询";
            treeNode16.Name = "节点17";
            treeNode16.Text = "客户信息报表";
            treeNode17.Name = "节点18";
            treeNode17.Text = "业务往来报表";
            treeNode18.Name = "节点19";
            treeNode18.Text = "联系人信息表";
            treeNode19.Name = "节点4";
            treeNode19.Text = "打印报表";
            treeNode20.Name = "节点20";
            treeNode20.Text = "调用Word";
            treeNode21.Name = "节点21";
            treeNode21.Text = "调用EXCEL";
            treeNode22.Name = "节点5";
            treeNode22.Text = "辅助工具";
            treeView1.Nodes.AddRange(new TreeNode[] { treeNode5, treeNode9, treeNode12, treeNode15, treeNode19, treeNode22 });
            treeView1.Size = new Size(165, 282);
            treeView1.TabIndex = 0;
            // 
            // pictureBox1
            // 
            pictureBox1.BackgroundImage = Properties.Resources.右;
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox1.Dock = DockStyle.Fill;
            pictureBox1.Location = new Point(0, 0);
            pictureBox1.Name = "pictureBox1";
            pictureBox1.Size = new Size(327, 282);
            pictureBox1.TabIndex = 0;
            pictureBox1.TabStop = false;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(497, 347);
            Controls.Add(splitContainer1);
            Controls.Add(panel1);
            Controls.Add(menuStrip1);
            MainMenuStrip = menuStrip1;
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            menuStrip1.ResumeLayout(false);
            menuStrip1.PerformLayout();
            splitContainer1.Panel1.ResumeLayout(false);
            splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();
            splitContainer1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private MenuStrip menuStrip1;
        private ToolStripMenuItem toolStripMenuItem1;
        private ToolStripMenuItem toolStripMenuItem2;
        private ToolStripMenuItem toolStripMenuItem3;
        private ToolStripMenuItem toolStripMenuItem4;
        private Panel panel1;
        private SplitContainer splitContainer1;
        private TreeView treeView1;
        private PictureBox pictureBox1;
    }
}

(3)Form1.cs

namespace _194
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 展开所有节点
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
        }
    }
}

(4)生成效果

 

 

        这是一个缺点,当拖拽窗体放大时,panel2里的图片不能stretch,请网友看到给一个修复的建议吧。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 10:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 10:56:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 10:56:01       82 阅读
  4. Python语言-面向对象

    2024-04-25 10:56:01       91 阅读

热门阅读

  1. 使用Spring Boot整合定时任务(Schedule)

    2024-04-25 10:56:01       38 阅读
  2. C#基础内容之枚举

    2024-04-25 10:56:01       30 阅读
  3. 汇编实现缓冲区溢出1

    2024-04-25 10:56:01       30 阅读
  4. C++ //练习 13.34 编写本节所描述的Message。

    2024-04-25 10:56:01       32 阅读
  5. 学习 Rust 的第十天:枚举和模式匹配

    2024-04-25 10:56:01       34 阅读
  6. 桌面运维类面试非技术问题

    2024-04-25 10:56:01       29 阅读
  7. yolov5 C3改进|深度可分离卷积轻量化主干

    2024-04-25 10:56:01       35 阅读
  8. mysql的基本用法

    2024-04-25 10:56:01       33 阅读
  9. Netty websocket配置wss

    2024-04-25 10:56:01       33 阅读
  10. 【QEMU系统分析之启动篇(十一)】

    2024-04-25 10:56:01       36 阅读
  11. Edge 浏览器的使用心得与深度探索

    2024-04-25 10:56:01       35 阅读