2016-11-04 123 views
0

我在c#中實現了一個winform應用程序,以便它包含四個圖片框和一個角落中的一些控件按鈕。在調整窗體大小時調整4個圖片框

enter image description here

我的主要問題是,我無法實現在圖片中顯示的行爲。這個想法是在窗體大小調整時自動調整圖片框的大小。控制面板的區域應該保持不變,並且只有包含這些框的區域應該相應地調整大小。

我一直在玩錨和碼頭屬性,但我沒有得到慾望輸出。

任何人都可以給我一個提示?

感謝和親切的問候, Bilbinight

+0

你試過了什麼?顯示一些代碼。 – Vikrant

+3

使用['TableLayoutPanel'](https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel%28v=vs.110%29.aspx)容器。 – Dmitry

+1

您是否嘗試過使用SplitContainer一側的錨定屬性? - 實際上,根據@ Dmitry的評論,在嘗試TableLayoutPanel之後,這是一個更好的解決方案。 –

回答

0

四個圖像框應該支持在運行時調整(允許最終用戶調整)?如果不是,那麼下面會給你想要的結果。

1個具有2行和2列的TableLayoutPanel。設置初始大小以填充所需區域,將錨點設置爲全部四個方向。 將4個圖片框添加到4個單元格,並設置Dock for Fill for all Picture Boxes。

在面板的右側添加面板,使其適合覆蓋您的控制區域。設置錨點頂部,底部,右側。

下面是示例代碼:

//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; 
    } 
} 
0

所以,讓我們說你有下面的圖片框:

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

然後下面應該做的伎倆: 設置形式調整事件給這個eventhandler:

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),以適應您的程序(這取決於是否在表單上使用邊框)。

UPDATE:

如果你希望你的pictureboxes方形那麼就忽略heightToFill變量和設置pictureBox1高度時使用widthToFill代替。或設置:

pictureBox1.Height = pictureBox.Width; 

而且我也忘了提到,面板當然應該對齊頂部,右側。因此將面板的Anchor屬性設置爲:

AnchorStyles.Top | AnchorStyles.Right; 
相關問題