调整表单大小时调整 4 个图片框的大小
Resize 4 picture boxes when form is resized
我正在用 c# 实现一个 winform 应用程序,它在一个角上包含四个图片框和一些控制按钮。
我的主要问题是我无法实现图片中显示的行为。这个想法是在调整表单大小时自动调整图片框的大小。控制面板的区域应保持不变,只有包含框的区域应相应调整大小。
我一直在玩锚和停靠属性,但我没有得到想要的输出。
谁能给我提示?
谢谢和亲切的问候,
比比奈特
四个图像框是否应该支持在运行时调整大小(允许最终用户调整大小)?如果没有,那么以下会给你想要的结果。
1 个具有 2 行和 2 列的 TableLayoutPanel。设置初始大小以填充所需区域,将锚点设置为所有四个方向。
将 4 个图片框添加到 4 个单元格,并将所有图片框的 Dock 设置为 Fill。
在表单的右侧添加一个面板,使其适合覆盖您的控件区域。设置锚点顶部、底部、右侧。
示例代码如下:
//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;
}
}
那么,假设您有以下图片框:
pictureBox1 | pictureBox2 | panel
------------|------------
pictureBox3 | pictureBox4
那么下面应该可以解决问题:
将表单调整大小事件设置为此事件处理程序:
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) 以适合您的程序(这在很大程度上取决于您是否在表单上使用边框)。
更新:
如果您希望您的图片框是方形的,那么在设置 pictureBox1 的高度时忽略 heightToFill 变量并使用 widthToFill 代替。或者设置:
pictureBox1.Height = pictureBox.Width;
而且我还忘了说,面板当然应该对齐顶部,右侧。所以设置panel的Anchor 属性为:
AnchorStyles.Top | AnchorStyles.Right;
我正在用 c# 实现一个 winform 应用程序,它在一个角上包含四个图片框和一些控制按钮。
我的主要问题是我无法实现图片中显示的行为。这个想法是在调整表单大小时自动调整图片框的大小。控制面板的区域应保持不变,只有包含框的区域应相应调整大小。
我一直在玩锚和停靠属性,但我没有得到想要的输出。
谁能给我提示?
谢谢和亲切的问候, 比比奈特
四个图像框是否应该支持在运行时调整大小(允许最终用户调整大小)?如果没有,那么以下会给你想要的结果。
1 个具有 2 行和 2 列的 TableLayoutPanel。设置初始大小以填充所需区域,将锚点设置为所有四个方向。 将 4 个图片框添加到 4 个单元格,并将所有图片框的 Dock 设置为 Fill。
在表单的右侧添加一个面板,使其适合覆盖您的控件区域。设置锚点顶部、底部、右侧。
示例代码如下:
//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;
}
}
那么,假设您有以下图片框:
pictureBox1 | pictureBox2 | panel
------------|------------
pictureBox3 | pictureBox4
那么下面应该可以解决问题: 将表单调整大小事件设置为此事件处理程序:
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) 以适合您的程序(这在很大程度上取决于您是否在表单上使用边框)。
更新:
如果您希望您的图片框是方形的,那么在设置 pictureBox1 的高度时忽略 heightToFill 变量并使用 widthToFill 代替。或者设置:
pictureBox1.Height = pictureBox.Width;
而且我还忘了说,面板当然应该对齐顶部,右侧。所以设置panel的Anchor 属性为:
AnchorStyles.Top | AnchorStyles.Right;