创建自定义布局/按钮

时间:2014-12-13 19:59:46

标签: c# winforms

我正在开发一个c#windows窗体程序,我有一个表单,它接收某些信息并将其转换为布局框中的按钮。

是否可以将自定义按钮/表单列在流布局框中? 我想制作一个看起来像分成3个按钮的按钮,每个部分都有不同的信息。

照片显示了我试图制作的按钮的布局,这个按钮会重复几次,但信息不同

enter image description here

2 个答案:

答案 0 :(得分:2)

这是使用TableLayoutPanel启动的UserControl:

  1. 点击项目 - >添加用户控件 - >输入名称(我使用过 " TriButton") - >添加按钮。
  2. 添加一个TableLayout面板并设置它 Dock Property to Fill。
  3. 点击" ..."在列属性中。将Column1设置为25%,并将Column2保留为50%。 *这将使第1列占据宽度的1/3,而第2列占据宽度的2/3,因为50是25的两倍大。
  4. 在TableLayout的左上角单元格中添加一个按钮(button1) 面板。将其Dock属性设置为Fill,将其RowSpan属性设置为2。
  5. 在TableLayoutPanel的右上角单元格中添加一个按钮(button2)。将其Dock属性设置为Fill。
  6. 在TableLayoutPanel的右下角单元格中添加一个按钮(button3)。将其Dock属性设置为Fill。
  7. 尝试调整UserControl的大小以查看其行为: TriButton UserControl

    编译或运行应用程序,新的UserControl应出现在工具箱的顶部。

    如果您希望能够从UserControl外部获取/设置Button值,则需要添加适当的属性,并且最好给UserControl自定义Button Click事件。

    替代方法

    以下是将布局创建为按钮的背景图像的示例:

    TriButton layout via a background image

    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Size sz = new Size(300, 150);
            TriButton btn = new TriButton(sz, textBox1.Text, textBox2.Text, dateTimePicker1.Value);
            flowLayoutPanel1.Controls.Add(btn);
        }
    
    }
    
    public class TriButton : Button
    {
    
        private String ID;
        private String Information;
        private DateTime Date;
    
        public TriButton(Size initialSize, String ID, String Information, DateTime Date)
        {
            this.ID = ID;
            this.Information = Information;
            this.Date = Date;
            this.Size = initialSize;
            this.SizeChanged += TriButton_SizeChanged;
            this.CreateBackgroundImage();
            this.Text = "";
            this.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.BackgroundImageLayout = ImageLayout.None;
            this.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
        }
    
        private void TriButton_SizeChanged(object sender, EventArgs e)
        {
            this.CreateBackgroundImage();
        }
    
        private void CreateBackgroundImage()
        {
            Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            Rectangle A = new Rectangle(new Point(0, 0), new Size(this.ClientRectangle.Width / 3, this.ClientRectangle.Height));
            Rectangle B = new Rectangle(new Point(A.Right, 0), new Size(this.ClientRectangle.Width - A.Width, this.ClientRectangle.Height / 2));
            Rectangle C = new Rectangle(new Point(A.Right, B.Bottom), new Size(B.Width, B.Height));
            using(Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(this.BackColor);
                using (Pen p = new Pen(SystemColors.ActiveBorder))
                {
                    g.DrawRectangle(p, A);
                    g.DrawRectangle(p, B);
                    g.DrawRectangle(p, C);
                }
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                using (Brush b = new SolidBrush(this.ForeColor))
                {
                    g.DrawString(this.ID, this.Font, b, A, sf);
                    g.DrawString(this.Information, this.Font, b, B, sf);
                    g.DrawString(this.Date.ToShortDateString(), this.Font, b, C, sf);
                }  
            }
            this.BackgroundImage = bmp;
        }
    
    }
    

答案 1 :(得分:0)

添加一个新类并粘贴以下代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsLayoutButton
{
    class FlowLayoutButton : System.Windows.Forms.Button
    {
        public FlowLayoutButton()
        {
            Label labelLeft = new Label();
            labelLeft.Text = "12345478";
            labelLeft.Dock = DockStyle.Fill;
            labelLeft.TextAlign = ContentAlignment.MiddleCenter;

            TableLayoutPanel horizontalLayout = new TableLayoutPanel();
            horizontalLayout.Dock = DockStyle.Fill;
            horizontalLayout.ColumnCount = 2;
            horizontalLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            horizontalLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            horizontalLayout.Controls.Add(labelLeft, 0, 0);
            horizontalLayout.BackColor = Color.FromArgb(0, 0, 0, 0);

            Label labelTop = new Label();
            labelTop.Text = "Some Information";
            labelTop.Dock = DockStyle.Fill;
            labelTop.TextAlign = ContentAlignment.MiddleCenter;

            Label labelBottom = new Label();
            labelBottom.Text = "Date";
            labelBottom.Dock = DockStyle.Fill;
            labelBottom.TextAlign = ContentAlignment.MiddleCenter;

            TableLayoutPanel verticalLayout = new TableLayoutPanel();
            verticalLayout.Dock = DockStyle.Fill;
            verticalLayout.RowCount = 2;
            verticalLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            verticalLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            verticalLayout.Controls.Add(labelTop, 0, 0);
            verticalLayout.Controls.Add(labelBottom, 0, 1);

            horizontalLayout.Controls.Add(verticalLayout, 1, 0);

            this.Controls.Add(horizontalLayout);

            Control[] controls = new Control[] { horizontalLayout, verticalLayout, labelLeft, labelTop, labelBottom };
            forEachControl(controls, (control) => { control.Click += (sender, e) => { this.OnClick(e); }; });
            forEachControl(controls, (control) => { control.MouseDown += (sender, e) => { this.OnMouseDown(e); }; });
            forEachControl(controls, (control) => { control.MouseUp += (sender, e) => { this.OnMouseUp(e); }; });
            forEachControl(controls, (control) => { control.MouseEnter += (sender, e) => { this.OnMouseEnter(e); }; });
            forEachControl(controls, (control) => { control.MouseLeave += (sender, e) => { this.OnMouseLeave(e); }; });
            forEachControl(controls, (control) => { control.Enter += (sender, e) => { this.OnEnter(e); }; });
            forEachControl(controls, (control) => { control.Leave += (sender, e) => { this.OnLeave(e); }; });
            forEachControl(controls, (control) => { control.GotFocus += (sender, e) => { this.OnGotFocus(e); }; });
            forEachControl(controls, (control) => { control.LostFocus += (sender, e) => { this.OnLostFocus(e); }; });
            forEachControl(controls, (control) => { control.KeyPress += (sender, e) => { this.OnKeyPress(e); }; });
            forEachControl(controls, (control) => { control.KeyDown += (sender, e) => { this.OnKeyDown(e); }; });
            forEachControl(controls, (control) => { control.KeyUp += (sender, e) => { this.OnKeyUp(e); }; });
            forEachControl(controls, (control) => { control.MouseClick += (sender, e) => { this.OnMouseClick(e); }; });
            forEachControl(controls, (control) => { control.MouseDoubleClick += (sender, e) => { this.OnMouseDoubleClick(e); }; });
        }

        private void forEachControl(Control[] controls, Action<Control> action)
        {
            foreach(Control control in controls) { action(control); }
        }
    }
}

现在您可以动态创建按钮:

namespace WindowsFormsLayoutButton
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FlowLayoutButton button = new FlowLayoutButton();
            button.Left = 16;
            button.Top = 8;
            button.Width = 256;
            button.Height = 96;
            this.Controls.Add(button);

            button.Click += (o, args) => { MessageBox.Show("clicked"); };
        }
    }
}