如何将面板四张图片链接到一张。只有winForm

时间:2013-09-13 12:26:55

标签: c# winforms

我需要在一个中制作4个不同的图像,它将在面板中。面板大小将从180到320不等。我试图做一个主面板,并在她的位置4,由锚固定..但事实证明很糟糕

enter image description here

private void Form1_Load(object sender, EventArgs e)
{
    Panel main_panel = new Panel();
    main_panel.BackColor = Color.Azure;
    Panel panel_top_left = new Panel();
    Panel panel_top_right = new Panel();
    Panel panel_bottom_left = new Panel();
    Panel panel_bottom_right = new Panel();

    Bitmap btm_msg_panel_top_left = new Bitmap(Properties.Resources.blue_t_l);
    panel_top_left.BackgroundImage = btm_msg_panel_top_left;
    Bitmap btm_msg_panel_top_right = new Bitmap(Properties.Resources.blue_t_r);
    panel_top_right.BackgroundImage = btm_msg_panel_top_right;
    Bitmap btm_msg_panel_bottom_left = new Bitmap(Properties.Resources.blue_b_l);
    panel_bottom_left.BackgroundImage = btm_msg_panel_bottom_left;
    Bitmap btm_msg_panel_bottom_right = new Bitmap(Properties.Resources.blue_b_r);
    panel_bottom_right.BackgroundImage = btm_msg_panel_bottom_right;

    main_panel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
    panel_top_left.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    panel_top_right.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left;
    panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
    panel_bottom_right.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

    main_panel.Controls.Add(panel_top_left);
    main_panel.Controls.Add(panel_top_right);
    main_panel.Controls.Add(panel_bottom_left);
    main_panel.Controls.Add(panel_bottom_right);

    panel1.Controls.Add(main_panel);
}

这是4张照片的来源

enter image description here enter image description here enter image description here enter image description here

我试着让他们像这样

enter image description here

1 个答案:

答案 0 :(得分:0)

enter image description here

我只是使用 tableLayoutPanel 而不是面板我希望它能为您提供帮助。
代码如下。

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

    private void Form1_Load(object sender, EventArgs e)
    {
        TableLayoutPanel myTable = new TableLayoutPanel();
        myTable.Location = new Point(0, 0);
        myTable.Size = new Size(506, 417);
        myTable.AutoSize = true;
        myTable.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        myTable.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddRows;
        myTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        this.Controls.Add(myTable);

        Panel panel1 = new Panel();
        Panel panel2 = new Panel();
        Panel panel3 = new Panel();
        Panel panel4 = new Panel();

        panel1.BackColor = Color.Red;          
        panel2.BackColor = Color.Black;           
        panel3.BackColor = Color.Blue;
        panel4.BackColor = Color.Yellow;

        myTable.Controls.Add(panel1, 0, 0);
        myTable.Controls.Add(panel2, 0, 1);
        myTable.Controls.Add(panel3, 0, 2);
        myTable.Controls.Add(panel4, 0, 3);
    }
}