winform前端框架設計

折騰了幾天,先把初版成果記錄下來,吼吼....ide

1、效果圖this

 

2、實現過程源碼spa

 一、先建立winform工程 建立好以下文件設計

 

二、在Config.cs中構靜態數據code

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myDemoFrame.ConfigHelper
{
    public static class Config
    {
        #region 樹形導航欄靜態數據
        public static Dictionary<string, List<Dictionary<string, string>>> SetDicNavigationBar()
        {
            Dictionary<string, List<Dictionary<string, string>>> dic = new Dictionary<string, List<Dictionary<string, string>>>();

            List<Dictionary<string, string>> lst = new List<Dictionary<string, string>>();
            Dictionary<string, string> dL = new Dictionary<string, string>();
            dL.Add("訂單查詢", "Form1");
            dL.Add("訂單管理", "Form2");
            dL.Add("訂單導出", "Form3");
            lst.Add(dL);
            dic.Add("模塊1", lst);

            lst = new List<Dictionary<string, string>>();
            dL = new Dictionary<string, string>();
            dL.Add("管理員新增", "Form4");
            dL.Add("管理員查詢", "Form5");
            lst.Add(dL);
            dic.Add("模塊2", lst);

            return dic;
        }
        #endregion
    }
}
View Code

三、在LeftNavigationBar.cs中 拖入panel  treeview   其中Dock設置full   LeftNavigationBar 窗體的邊框設置none 也能夠不繼承Form  繼承UserControlorm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace myDemoFrame.MyContorls
{
    public partial class LeftNavigationBar : Form
    {
        /// <summary>
        /// Form名字的集合
        /// </summary>
        public Dictionary<string, List<Dictionary<string, string>>> dic = new Dictionary<string, List<Dictionary<string, string>>>();

        public Panel panel;
        public LeftNavigationBar()
        {
            InitializeComponent();
        }

        private void CreateTree(Dictionary<string, List<Dictionary<string, string>>> dicLst)
        {
            foreach (var item in dicLst)
            {
                TreeNode root = new TreeNode();
                root.Text = item.Key;
                treeView1.Nodes.Add(root);
                foreach (var child in item.Value)
                {
                    foreach (var it in child)
                    {
                        root.Nodes.Add(it.Key);
                    }
                }
            }

        }

        private Dictionary<string, string> GetValue(Dictionary<string, List<Dictionary<string, string>>> dicLst)
        {
            Dictionary<string, string> dicK = new Dictionary<string, string>();
            foreach (var item in dicLst)
            {

                foreach (var child in item.Value)
                {
                    foreach (var it in child)
                    {
                        dicK.Add(it.Key, it.Value);
                    }

                }
            }
            return dicK;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                if (e.Node.Nodes.Count == 0)
                {
                    string strFormName = e.Node.Text;

                    Dictionary<string, string> dicL = GetValue(dic);

                    ShowForm(dicL[strFormName]);
                }
            }
        }

        private void ShowForm(string strFormName)
        {
            Form frm = GetForm(strFormName);
            frm.BringToFront();
            frm.Show();
        }

        private Form GetForm(string strFormName)
        {
            Control.ControlCollection ctrls = panel.Controls;
            if (ctrls.ContainsKey(strFormName))
            {
                return ctrls[strFormName] as Form;
            }
            else
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                Type type = assembly.GetTypes().Where(item => item.Name.Equals(strFormName)).FirstOrDefault();
                Form frm = assembly.CreateInstance(type.FullName) as Form;
                frm.Name = strFormName;
                frm.TopLevel = false;
                frm.Dock = DockStyle.Fill;
                frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                panel.Controls.Add(frm);
                return frm;
            }
        }

        private void LeftNavigationBar_Load(object sender, EventArgs e)
        {
            CreateTree(dic);
        }
    }
}
View Code

四、MainForm的設計blog

    三個panel  一個menuStrip  其中panel的部署 Dock  => Top  \  Left  \ Fill   menuStrip注意幾個屬性的設置 ImageScaling爲none ,TexImageRelation爲ImageAboveText, Image插入美工作好的圖便可繼承

    

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 myDemoFrame
{
    public partial class ManForm : Form
    {
        public ManForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            MyContorls.LeftNavigationBar mu = new MyContorls.LeftNavigationBar();
            mu.dic = ConfigHelper.Config.SetDicNavigationBar();
            mu.panel = this.panelMain;
            mu.TopLevel = false;
            mu.Dock = DockStyle.Left;
            panelLeft.Controls.Add(mu);
            mu.Show();
        }
    }
}
View Code
相關文章
相關標籤/搜索