FlowLayoutPanel可以根据Excel或xml等文件配置显示内容,自动绘画显示。解决新需求变更参数还需要更改界面的的问题。
显示内容可以在配置表中设置,动态添加显示,事件统一处理(对应不同事件参考:C# 自定义按钮及其事件处理_花开莫与流年错_的博客-CSDN博客_c# 自定义按钮)
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 study.NewFolder1.Right.Basic;
namespace study.NewFolder1.Right
{
public partial class Controls : UserControl
{
List controls = new List();
Button button1 = new Button();
public Controls()
{
InitializeComponent();
Init();
}
void Init()
{
this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.Dock = DockStyle.Fill;
label1.Text = this.Width + ", " + this.Height;
this.SizeChanged += new EventHandler(ThisSizeChanged);
controls.Add(TitlePanel);
// 右边动态显示内容,可以是读取本地配置表来显示
List list = new List();
for (int i = 0; i < 2; i++)
list.Add(Convert.ToString(i + 1));
for (int i = 0; i < list.Count; i++)
{
// 添加显示内容,并添加事件处理。事件统一用一个函数处理,通过显示内容区分点了哪一个
Title title = new Title(list[i]);
title.Changed += new MyEventHandler(Title_Changed);
// 添加到controls最终显示出来
controls.Add(title);
}
// 添加很多时挂起会更快(不触发界面更改重新绘图事件)
this.flowLayoutPanel1.SuspendLayout();
for(int i = 0; i < controls.Count; i++)
{
this.flowLayoutPanel1.Controls.Add(controls[i]);
}
this.flowLayoutPanel1.ResumeLayout();
}
private void Add_Click(object sender, EventArgs e)
{
// 添加很多时挂起会更快(不触发界面更改重新绘图事件)
this.flowLayoutPanel1.SuspendLayout();
Title title = new Title("laoshijidaidaiwo");
title.Width = this.Width - 25;
title.Changed += new MyEventHandler(Title_Changed);
controls.Add(title);
this.flowLayoutPanel1.Controls.Add(title);
this.flowLayoutPanel1.ResumeLayout();
}
void Title_Changed(object obj, string str)
{
int i = 0;
}
// 自适应大小
void ThisSizeChanged(object sender, EventArgs e)
{
for (int i = 0; i < controls.Count; i++)
{
controls[i].Width = this.Width - 25;
}
label1.Text = this.Width + ", " + this.Height;
}
#region 组件设计器生成的代码
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
///
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.TitlePanel = new System.Windows.Forms.Panel();
this.TitlePicture = new System.Windows.Forms.PictureBox();
this.Title = new System.Windows.Forms.Label();
this.Add = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.flowLayoutPanel1.SuspendLayout();
this.TitlePanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.TitlePicture)).BeginInit();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.flowLayoutPanel1.Controls.Add(this.TitlePanel);
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(954, 435);
this.flowLayoutPanel1.TabIndex = 0;
//
// TitlePanel
//
this.TitlePanel.AutoScroll = true;
this.TitlePanel.Controls.Add(this.TitlePicture);
this.TitlePanel.Controls.Add(this.Title);
this.TitlePanel.Font = new System.Drawing.Font("宋体", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.TitlePanel.Location = new System.Drawing.Point(3, 3);
this.TitlePanel.Name = "TitlePanel";
this.TitlePanel.Size = new System.Drawing.Size(636, 48);
this.TitlePanel.TabIndex = 2;
//
// TitlePicture
//
this.TitlePicture.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.TitlePicture.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.TitlePicture.Location = new System.Drawing.Point(3, 34);
this.TitlePicture.Name = "TitlePicture";
this.TitlePicture.Size = new System.Drawing.Size(600, 2);
this.TitlePicture.TabIndex = 1;
this.TitlePicture.TabStop = false;
//
// Title
//
this.Title.AutoSize = true;
this.Title.Location = new System.Drawing.Point(12, 12);
this.Title.Name = "Title";
this.Title.Size = new System.Drawing.Size(47, 19);
this.Title.TabIndex = 0;
this.Title.Text = "主页";
//
// Add
//
this.Add.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.Add.Location = new System.Drawing.Point(52, 441);
this.Add.Name = "Add";
this.Add.Size = new System.Drawing.Size(82, 32);
this.Add.TabIndex = 1;
this.Add.Text = "添加";
this.Add.UseVisualStyleBackColor = true;
this.Add.Click += new System.EventHandler(this.Add_Click);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(177, 458);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 15);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
//
// Controls
//
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.AutoSize = true;
this.Controls.Add(this.label1);
this.Controls.Add(this.Add);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "Controls";
this.Size = new System.Drawing.Size(954, 489);
this.flowLayoutPanel1.ResumeLayout(false);
this.TitlePanel.ResumeLayout(false);
this.TitlePanel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.TitlePicture)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button Add;
private System.Windows.Forms.Panel TitlePanel;
private System.Windows.Forms.PictureBox TitlePicture;
private System.Windows.Forms.Label Title;
private System.Windows.Forms.Label label1;
private Basic.Title title2;
}
}