| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1228 人关注过本帖
标题:C# 界面思路
只看楼主 加入收藏
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
结帖率:0
收藏
已结贴  问题点数:10 回复次数:11 
C# 界面思路
我想模仿TI的packet sniffer 做这样一个界面。中间协议解析部分,也就是块状部分不知道如何着手设计,请大家指导
 
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 如何 设计 模仿 
2012-11-11 13:12
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:3 
用户控件即可

为提高中华编程水平而奋斗
2012-11-11 14:09
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
收藏
得分:0 
但是用户控件怎么设置一行呢,一行的方块数不定。我看很多控件都只能一行一行添加。不能一个单元格单元格添加控件啊。请继续指导
2012-11-11 15:50
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:3 
那个是表格控件

成功贵在坚持
2012-11-11 16:08
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
收藏
得分:0 
不是吧,表格控件实现不了吧。表格会固定列,但是图片上列数是在变化的。
2012-11-11 16:25
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

一、新建一个类库名曰“UserControls”,在里面添加两个用户控件:
    1、Block,代表一个块,根据你发的图来看,每一块基本分三部分:标题、数据名和数据,那么在Block中依次放一个Label来显示标题,放置一个分隔线,放一个TableLayoutPanel来显示数据表,TableLayoutPanel初始为三行一列,第0行用来放置数据名称、第1行放置分隔线、第2行放置数据。至于列则根据设置Columns属性的值在运行时生成。Block的颜色请设置BackColor;
    2、BlockLine,代表一个块行,设置Blocks属性的值来定义这行里面的一堆N个Block,就一个FlowLayoutPanel来管理布局;
二、在FormMain里引用UserControls库,你就可以根据你的业务逻辑生成N个BlockLine,这样就和TI那个差不多了;
三、有些块是没有数据名称的,这样的话你可以对Block进行修改,增加一个ColumnVisible属性并根据这个属性决定是否显示数据名称就好了,但是要注意这时列的数量就要由Values的值的所包含的数据数量来决定了,要修改代码;
四、还有一点,这里使用现有的控件组成用户控件来达到需求的,如果想进一步控制Block的外观及其它一些细节,那么建议Block做成自定义控件的形式自己绘制控件;
五、这个例子在FormMain里面放了3个BlockLine,各有3、5、2个Block,颜色不同,各部分代码如下:
程序代码:
//Block.Designer.cs
namespace UserControls
{
    partial class Block
    {
        private  components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.LabName = new System.Windows.Forms.Label();
            this.Tlp = new System.Windows.Forms.TableLayoutPanel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.Tlp.SuspendLayout();
            this.SuspendLayout();
            this.LabName.Dock = System.Windows.Forms.DockStyle.Top;
            this.LabName.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold,
                                                        System.Drawing.GraphicsUnit.Point, ((byte) (134)));
            this.LabName.Location = new System.Drawing.Point(0, 0);
            this.LabName.Name = "LabName";
            this.LabName.Size = new System.Drawing.Size(8, 15);
            this.LabName.TabIndex = 0;
            this.LabName.Text = "[Name]";
            this.LabName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            this.Tlp.AutoSize = true;
            this.Tlp.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Tlp.ColumnCount = 1;
            this.Tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            this.Tlp.Controls.Add(this.panel2, 0, 1);
            this.Tlp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Tlp.Location = new System.Drawing.Point(0, 15);
            this.Tlp.Name = "Tlp";
            this.Tlp.RowCount = 3;
            this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 2F));
            this.Tlp.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.Tlp.Size = new System.Drawing.Size(8, 2);
            this.Tlp.TabIndex = 1;

            this.panel1.AutoSize = true;
            this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 15);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(8, 2);
            this.panel1.TabIndex = 2;

            this.panel2.AutoSize = true;
            this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Tlp.SetColumnSpan(this.panel2, 3);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel2.Location = new System.Drawing.Point(3, 3);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(2, 1);
            this.panel2.TabIndex = 3;

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.BackColor = System.Drawing.Color.Khaki;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.Tlp);
            this.Controls.Add(this.LabName);
            this.Name = "Block";
            this.Size = new System.Drawing.Size(8, 17);
            this.Tlp.ResumeLayout(false);
            this.Tlp.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private System.Windows.Forms.Label LabName;
        private System.Windows.Forms.TableLayoutPanel Tlp;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel1;
    }
}

//Block.cs
using System.Collections.Generic;
using using System.Globalization;
using System.Windows.Forms;

namespace UserControls
{
    public partial class Block : UserControl
    {
        #region 全局字段

        private List<string>    _columns;
        private List<int>       _values;

        #endregion

        #region 构造函数

        public Block()
        {
            InitializeComponent();
        }

        #endregion

        #region 公共属性
   
        [Browsable(false)]
        public List<string> Columns
        {
            get { return _columns; }
            set
            {
                if (value == null || value.Count == 0) return;
                _columns = value;
                Tlp.ColumnCount = _columns.Count;
                Tlp.Controls.Clear();
                var panelSeparator = new Panel
                                         {
                                             BorderStyle  = BorderStyle.FixedSingle,
                                             AutoSize     = true,
                                             AutoSizeMode = AutoSizeMode.GrowAndShrink,
                                             Height       = 1,
                                             Dock         = DockStyle.Top,
                                             Margin       = new Padding(0)
                                         };
                Tlp.Controls.Add(panelSeparator, 0, 1);
                Tlp.SetColumnSpan(panelSeparator, _columns.Count);
                Tlp.ColumnStyles.Clear();
                for (var i = 0; i < _columns.Count; i++)
                {
                    Tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                    var labelColumnName = new Label
                                              {
                                                  Text     = _columns[i],
                                                  AutoSize = true,
                                                  Margin   = new Padding(3, 3, 0, 0)
                                              };

                    Tlp.Controls.Add(labelColumnName, i, 0);
                    var labelValue = new Label
                                         {
                                             Text     = "0",
                                             AutoSize = true,
                                             Margin   = new Padding(3, 0, 0, 0)
                                         };
                    Tlp.Controls.Add(labelValue, i, 2);
                }
            }
        }

        public string Title
        {
            get { return LabName.Text; }
            set { LabName.Text = value; }
        }

        public List<int> Values
        {
            get { return _values; }
            set
            {
                if (value == null || value.Count == 0) return;
                _values = value;
                for (var i = 0; i < _columns.Count; i++)
                {
                    var labelValue = (Label)Tlp.Controls[(i + 1) * 2];
                    labelValue.Text = _values[i].ToString(CultureInfo.InvariantCulture);
                }
            }
        }

        #endregion
    }
}

//BlockLine.Designer.cs
namespace UserControls
{
    partial class BlockLine
    {
        private  components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.Flp = new System.Windows.Forms.FlowLayoutPanel();
            this.SuspendLayout();

            this.Flp.AutoSize = true;
            this.Flp.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Flp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Flp.Location = new System.Drawing.Point(0, 0);
            this.Flp.Name = "Flp";
            this.Flp.Size = new System.Drawing.Size(0, 0);
            this.Flp.TabIndex = 0;

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Controls.Add(this.Flp);
            this.Name = "BlockLine";
            this.Size = new System.Drawing.Size(0, 0);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private System.Windows.Forms.FlowLayoutPanel Flp;
    }
}

//BlockLine.cs
using System.Collections.Generic;
using System.Windows.Forms;

namespace UserControls
{
    public partial class BlockLine : UserControl
    {
        #region 全局字段

        private List<Block> _blocks;

        #endregion

        #region 构造函数

        public BlockLine()
        {
            InitializeComponent();
        }

        #endregion

        #region 公共属性

        public List<Block> Blocks
        {
            get { return _blocks; }
            set
            {
                if (value == null || value.Count == 0) return;
                _blocks = value;
                Flp.Controls.Clear();
                foreach (var block in _blocks)
                {
                    block.Margin = new Padding(0, 2, 2, 0);
                    Flp.Controls.Add(block);
                }
            }
        }

        #endregion
    }
}

//FormMain.Designer.cs
namespace LikeTI
{
    partial class FormMain
    {
        private  components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.blockLine2 = new UserControls.BlockLine();
            this.blockLine1 = new UserControls.BlockLine();
            this.blockLine3 = new UserControls.BlockLine();
            this.SuspendLayout();

            this.blockLine2.AutoSize = true;
            this.blockLine2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.blockLine2.Blocks = null;
            this.blockLine2.Dock = System.Windows.Forms.DockStyle.Top;
            this.blockLine2.Location = new System.Drawing.Point(0, 0);
            this.blockLine2.Name = "blockLine2";
            this.blockLine2.Size = new System.Drawing.Size(647, 0);
            this.blockLine2.TabIndex = 1;

            this.blockLine1.AutoSize = true;
            this.blockLine1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.blockLine1.Blocks = null;
            this.blockLine1.Dock = System.Windows.Forms.DockStyle.Top;
            this.blockLine1.Location = new System.Drawing.Point(0, 0);
            this.blockLine1.Name = "blockLine1";
            this.blockLine1.Size = new System.Drawing.Size(647, 0);
            this.blockLine1.TabIndex = 0;

            this.blockLine3.AutoSize = true;
            this.blockLine3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.blockLine3.Blocks = null;
            this.blockLine3.Dock = System.Windows.Forms.DockStyle.Top;
            this.blockLine3.Location = new System.Drawing.Point(0, 0);
            this.blockLine3.Name = "blockLine3";
            this.blockLine3.Size = new System.Drawing.Size(647, 0);
            this.blockLine3.TabIndex = 2;

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(647, 262);
            this.Controls.Add(this.blockLine3);
            this.Controls.Add(this.blockLine2);
            this.Controls.Add(this.blockLine1);
            this.Name = "FormMain";
            this.ShowIcon = false;
            this.Text = "Like TI by ";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private UserControls.BlockLine blockLine1;
        private UserControls.BlockLine blockLine2;
        private UserControls.BlockLine blockLine3;
    }
}

//FormMain.cs
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using UserControls;

namespace LikeTI
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();

            blockLine1.Blocks = new List<Block>
                                    {
                                        new Block
                                            {
                                                Title = "Block00",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                BackColor = Color.Coral
                                            },
                                        new Block
                                            {
                                                Title = "Block01",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                BackColor = Color.Coral
                                            },
                                        new Block
                                            {
                                                Title = "Block02",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                BackColor = Color.Coral
                                            }
                                    };
            blockLine2.Blocks = new List<Block>
                                    {
                                        new Block
                                            {
                                                Title = "Block10",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {0, 1, 2}
                                            },
                                        new Block
                                            {
                                                Title = "Block11",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {3, 4, 5}
                                            },
                                        new Block
                                            {
                                                Title = "Block12",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {6, 7, 8}
                                            },
                                        new Block
                                            {
                                                Title = "Block13",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {9, 10, 11}
                                            },
                                        new Block
                                            {
                                                Title = "Block14",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {12, 13, 14}
                                            }
                                    };
            blockLine3.Blocks = new List<Block>
                                    {
                                        new Block
                                            {
                                                Title = "Block10",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {0, 1, 2},
                                                BackColor = Color.Gainsboro
                                            },
                                        new Block
                                            {
                                                Title = "Block11",
                                                Columns = new List<string> {"C1", "C2", "C3"},
                                                Values = new List<int> {3, 4, 5},
                                                BackColor = Color.Gainsboro
                                            }
                                    };
        }
    }
}


[ 本帖最后由 mmxo 于 2012-11-12 15:32 编辑 ]

为提高中华编程水平而奋斗
2012-11-11 20:52
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
收藏
得分:0 
首先看到你的答复,非常感谢。我先测试一下你的代码,想想你给的思路。关于你说发图的事情,我是粘贴的附件。发帖的时候有那么一项的。先感谢!!
2012-11-12 09:45
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:0 
郁闷了,怎么我发帖时只见有“添加附件”而没有“粘贴附件”,而且怎么也添加不了图片,唉~~~~~

为提高中华编程水平而奋斗
2012-11-12 10:43
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
收藏
得分:0 
谢谢mmxo的详细解答,代码实现了我想要的功能。思路,和建议都是很好的。
2012-11-12 11:16
iampig
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2012-11-11
收藏
得分:0 
就是添加附件。注册用户访问的时候,附件是直接显示的。游客访问的时候是显示,请登录查看附件。
2012-11-12 11:17
快速回复:C# 界面思路
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049570 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved