调整窗体大小时调整4个图片框的大小

时间:2016-11-04 09:52:03

标签: c# winforms user-interface resize picturebox

我在c#中实现了一个winform应用程序,因此它在一个角落里包含四个图片框和一些控制按钮。

enter image description here

我的主要问题是我无法实现图片中显示的行为。这个想法是在调整表单大小时自动调整图片框的大小。控制面板的区域应保持不变,只有包含框的区域应相应调整大小。

我一直在玩锚和码头属性,但我没有得到欲望输出。

任何人都可以给我一个提示吗?

谢谢和亲切的问候, Bilbinight

2 个答案:

答案 0 :(得分:0)

四个图像框是否应该支持在运行时调整大小(允许最终用户调整大小)?如果没有,那么下面将给你想要的结果。

1个TableLayoutPanel,包含2行和2列。设置初始大小以填充所需区域,将锚点设置为所有四个方向。 将4个图片框添加到4个单元格中,并将Dock设置为填充所有图片框。

在表单的右侧添加一个面板,使其适合覆盖您的控件区域。设置锚点Top,Bottom,Right。

以下是示例代码:

//frmResizing.cs
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmResizing : Form
    {
        public frmResizing()
        {
            InitializeComponent();
        }
    }
}
//frmResizing.Designer.cs code
namespace WindowsFormsApplication1
{
    partial class frmResizing
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.pictureBox4 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.tableLayoutPanel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.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.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.pictureBox4, 1, 1);
            this.tableLayoutPanel1.Controls.Add(this.pictureBox3, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.pictureBox2, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 0, 0);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(2, 1);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(544, 561);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.BackColor = System.Drawing.Color.Red;
            this.panel1.Location = new System.Drawing.Point(553, 1);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(107, 561);
            this.panel1.TabIndex = 1;
            // 
            // pictureBox4
            // 
            this.pictureBox4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox4.Image = global::WindowsFormsApplication1.Properties.Resources.download;
            this.pictureBox4.Location = new System.Drawing.Point(275, 283);
            this.pictureBox4.Name = "pictureBox4";
            this.pictureBox4.Size = new System.Drawing.Size(266, 275);
            this.pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox4.TabIndex = 3;
            this.pictureBox4.TabStop = false;
            // 
            // pictureBox3
            // 
            this.pictureBox3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox3.Image = global::WindowsFormsApplication1.Properties.Resources.pizza_page;
            this.pictureBox3.Location = new System.Drawing.Point(3, 283);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(266, 275);
            this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox3.TabIndex = 2;
            this.pictureBox3.TabStop = false;
            // 
            // pictureBox2
            // 
            this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox2.Image = global::WindowsFormsApplication1.Properties.Resources.images;
            this.pictureBox2.Location = new System.Drawing.Point(275, 3);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(266, 274);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox2.TabIndex = 1;
            this.pictureBox2.TabStop = false;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Image = global::WindowsFormsApplication1.Properties.Resources.download__1_;
            this.pictureBox1.Location = new System.Drawing.Point(3, 3);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(266, 274);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(660, 562);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.tableLayoutPanel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.PictureBox pictureBox4;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Panel panel1;
    }
}

答案 1 :(得分:0)

所以,假设您有以下图片框:

pictureBox1 | pictureBox2 | panel
------------|------------
pictureBox3 | pictureBox4

然后以下应该做的伎俩: 将表单Resize事件设置为此事件处理程序:

private void Form1_Resize(object sender, EventArgs e)
{
    int spaceBetweenPictures = 19;
    int widthToFill = (this.Width - 40 - panel.Width) - spaceBetweenPictures;
    int heightToFill = this.Height - 80;

    pictureBox1.Width = widthToFill / 2;
    pictureBox1.Height = heightToFill / 2;

    // Setting the sizes of all the three pictureboxes to the sizes of the first one.
    pictureBox2.Width = pictureBox1.Width;
    pictureBox2.Height = pictureBox1.Height;
    pictureBox3.Width = pictureBox1.Width;
    pictureBox3.Height = pictureBox1.Height;
    pictureBox4.Width = pictureBox1.Width;
    pictureBox4.Height = pictureBox1.Height;

    // Setting the positions:
    pictureBox2.Location = new Point(pictureBox1.Width + spaceBetweenPictures, pictureBox1.Location.Y);
    pictureBox3.Location = new Point(pictureBox1.Location.X, pictureBox1.Height + spaceBetweenPictures);
    pictureBox4.Location = new Point(pictureBox2.Location.X, pictureBox3.Location.Y);
}

当然,您应该相应地修改此代码(19,40,80)中的幻数,以适合您的程序(这在很大程度上取决于您是否在表单上使用边框)。

更新:

如果你想让你的图片盒成方形,那么只需忽略heightToFill变量,并在设置pictureBox1的高度时使用widthToFill。或者设置:

pictureBox1.Height = pictureBox.Width;

而且我也忘了提到小组当然应该对齐Top,Right。因此,将面板的Anchor属性设置为:

AnchorStyles.Top | AnchorStyles.Right;