Windows 窗体编程
如何:设计适合本地化的 Windows 窗体布局

创建适合本地化的窗体可极大地提高国际市场的开发速度。使用 TableLayoutPanel 控件可实现能在控件因其 Text 属性值更改而调整大小时作出适当响应的布局。

示例

此窗体演示如何创建能在将显示的字符串值翻译为其他语言时按比例进行调整的布局。这个翻译过程称为“本地化”。有关更多信息,请参见本地化

Visual Studio 中对此任务提供了广泛的支持。 有关更多信息,请参见演练:创建可根据本地化需要调整比例的布局

Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class LocalizableForm
    Inherits Form

   Private tableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
   Private listView1 As System.Windows.Forms.ListView
   Private panel1 As System.Windows.Forms.Panel
   Private WithEvents button3 As System.Windows.Forms.Button
   Private WithEvents button2 As System.Windows.Forms.Button
   Private WithEvents button1 As System.Windows.Forms.Button
   Private tableLayoutPanel2 As System.Windows.Forms.TableLayoutPanel
   Private WithEvents button4 As System.Windows.Forms.Button
   Private WithEvents button5 As System.Windows.Forms.Button
   Private WithEvents button6 As System.Windows.Forms.Button
   Private label1 As System.Windows.Forms.Label
   
   Private components As System.ComponentModel.IContainer = Nothing
   
   Public Sub New()
      InitializeComponent()
    End Sub
   
   Private Sub AddText(sender As Object, e As EventArgs) Handles button3.Click, button2.Click, button1.Click, button4.Click, button5.Click, button6.Click
      CType(sender, Button).Text += "x"
    End Sub
   
   Protected Overrides Sub Dispose(disposing As Boolean)
      If disposing AndAlso Not (components Is Nothing) Then
         components.Dispose()
      End If
      MyBase.Dispose(disposing)
    End Sub 
   
   Private Sub InitializeComponent()
      Dim listViewGroup16 As New System.Windows.Forms.ListViewGroup("First Group", System.Windows.Forms.HorizontalAlignment.Left)
      Dim listViewGroup17 As New System.Windows.Forms.ListViewGroup("Second Group", System.Windows.Forms.HorizontalAlignment.Left)
      Dim listViewGroup18 As New System.Windows.Forms.ListViewGroup("Third Group", System.Windows.Forms.HorizontalAlignment.Left)
      Dim listViewItem26 As New System.Windows.Forms.ListViewItem("Item 1")
      Dim listViewItem27 As New System.Windows.Forms.ListViewItem("Item 2")
      Dim listViewItem28 As New System.Windows.Forms.ListViewItem("Item 3")
      Dim listViewItem29 As New System.Windows.Forms.ListViewItem("Item 4")
      Dim listViewItem30 As New System.Windows.Forms.ListViewItem("Item 5")
      Me.tableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
      Me.listView1 = New System.Windows.Forms.ListView()
      Me.panel1 = New System.Windows.Forms.Panel()
      Me.button3 = New System.Windows.Forms.Button()
      Me.button2 = New System.Windows.Forms.Button()
      Me.button1 = New System.Windows.Forms.Button()
      Me.tableLayoutPanel2 = New System.Windows.Forms.TableLayoutPanel()
      Me.button4 = New System.Windows.Forms.Button()
      Me.button5 = New System.Windows.Forms.Button()
      Me.button6 = New System.Windows.Forms.Button()
      Me.label1 = New System.Windows.Forms.Label()
      Me.tableLayoutPanel1.SuspendLayout()
      Me.panel1.SuspendLayout()
      Me.tableLayoutPanel2.SuspendLayout()
      Me.SuspendLayout()
      ' 
      ' tableLayoutPanel1
      ' 
      Me.tableLayoutPanel1.ColumnCount = 2
      Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle())
      Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F))
      Me.tableLayoutPanel1.Controls.Add(Me.listView1, 1, 0)
      Me.tableLayoutPanel1.Controls.Add(Me.panel1, 0, 0)
      Me.tableLayoutPanel1.Location = New System.Drawing.Point(2, 52)
      Me.tableLayoutPanel1.Name = "tableLayoutPanel1"
      Me.tableLayoutPanel1.RowCount = 1
      Me.tableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F))
      Me.tableLayoutPanel1.Size = New System.Drawing.Size(524, 270)
      Me.tableLayoutPanel1.TabIndex = 1
      ' 
      ' listView1
      ' 
      Me.listView1.Dock = System.Windows.Forms.DockStyle.Fill
      listViewGroup16.Header = "First Group"
      listViewGroup16.Name = Nothing
      listViewGroup17.Header = "Second Group"
      listViewGroup17.Name = Nothing
      listViewGroup18.Header = "Third Group"
      listViewGroup18.Name = Nothing
      Me.listView1.Groups.AddRange(New System.Windows.Forms.ListViewGroup() {listViewGroup16, listViewGroup17, listViewGroup18})
      'this.listView1.IsBackgroundImageTiled = false;
      listViewItem26.Group = listViewGroup16
      listViewItem27.Group = listViewGroup16
      listViewItem28.Group = listViewGroup17
      listViewItem29.Group = listViewGroup17
      listViewItem30.Group = listViewGroup18
      Me.listView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {listViewItem26, listViewItem27, listViewItem28, listViewItem29, listViewItem30})
      Me.listView1.Location = New System.Drawing.Point(90, 3)
      Me.listView1.Name = "listView1"
      Me.listView1.Size = New System.Drawing.Size(431, 264)
      Me.listView1.TabIndex = 1
      ' 
      ' panel1
      ' 
      Me.panel1.Anchor = System.Windows.Forms.AnchorStyles.Top
      Me.panel1.AutoSize = True
      Me.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
      Me.panel1.Controls.Add(Me.button3)
      Me.panel1.Controls.Add(Me.button2)
      Me.panel1.Controls.Add(Me.button1)
      Me.panel1.Location = New System.Drawing.Point(3, 3)
      Me.panel1.Name = "panel1"
      Me.panel1.Size = New System.Drawing.Size(81, 86)
      Me.panel1.TabIndex = 2
      ' 
      ' button3
      ' 
      Me.button3.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button3.AutoSize = True
      Me.button3.Location = New System.Drawing.Point(3, 60)
      Me.button3.Name = "button3"
      Me.button3.TabIndex = 2
      Me.button3.Text = "Add Text"
      ' 
      ' button2
      ' 
      Me.button2.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button2.AutoSize = True
      Me.button2.Location = New System.Drawing.Point(3, 31)
      Me.button2.Name = "button2"
      Me.button2.TabIndex = 1
      Me.button2.Text = "Add Text"
      ' 
      ' button1
      ' 
      Me.button1.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button1.AutoSize = True
      Me.button1.Location = New System.Drawing.Point(3, 2)
      Me.button1.Name = "button1"
      Me.button1.TabIndex = 0
      Me.button1.Text = "Add Text"
      ' 
      ' tableLayoutPanel2
      ' 
      Me.tableLayoutPanel2.Anchor = CType(System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.tableLayoutPanel2.AutoSize = True
      Me.tableLayoutPanel2.ColumnCount = 3
      Me.tableLayoutPanel2.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F))
      Me.tableLayoutPanel2.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F))
      Me.tableLayoutPanel2.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F))
      Me.tableLayoutPanel2.Controls.Add(Me.button4, 0, 0)
      Me.tableLayoutPanel2.Controls.Add(Me.button5, 1, 0)
      Me.tableLayoutPanel2.Controls.Add(Me.button6, 2, 0)
      Me.tableLayoutPanel2.Location = New System.Drawing.Point(284, 328)
      Me.tableLayoutPanel2.Name = "tableLayoutPanel2"
      Me.tableLayoutPanel2.RowCount = 1
      Me.tableLayoutPanel2.RowStyles.Add(New System.Windows.Forms.RowStyle())
      Me.tableLayoutPanel2.Size = New System.Drawing.Size(243, 34)
      Me.tableLayoutPanel2.TabIndex = 2
      ' 
      ' button4
      ' 
      Me.button4.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button4.AutoSize = True
      Me.button4.Location = New System.Drawing.Point(3, 5)
      Me.button4.Name = "button4"
      Me.button4.TabIndex = 0
      Me.button4.Text = "Add Text"
      ' 
      ' button5
      ' 
      Me.button5.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button5.AutoSize = True
      Me.button5.Location = New System.Drawing.Point(84, 5)
      Me.button5.Name = "button5"
      Me.button5.TabIndex = 1
      Me.button5.Text = "Add Text"
      ' 
      ' button6
      ' 
      Me.button6.Anchor = CType(System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
      Me.button6.AutoSize = True
      Me.button6.Location = New System.Drawing.Point(165, 5)
      Me.button6.Name = "button6"
      Me.button6.TabIndex = 2
      Me.button6.Text = "Add Text"
      ' 
      ' label1
      ' 
      Me.label1.Location = New System.Drawing.Point(8, 7)
      Me.label1.Name = "label1"
      Me.label1.Size = New System.Drawing.Size(518, 40)
      Me.label1.TabIndex = 3
      Me.label1.Text = "Click on any button to add text to the button. This simulates localizing strings," + " and provides a good demonstration of how the dialog will automatically adjust w" + "hen those longer strings are added to the UI."
      ' 
      ' LocalizableForm
      ' 
      Me.ClientSize = New System.Drawing.Size(539, 374)
      Me.Controls.Add(label1)
      Me.Controls.Add(tableLayoutPanel2)
      Me.Controls.Add(tableLayoutPanel1)
      Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
      Me.Name = "LocalizableForm"
      Me.Text = "Localizable Dialog"
      Me.tableLayoutPanel1.ResumeLayout(False)
      Me.tableLayoutPanel1.PerformLayout()
      Me.panel1.ResumeLayout(False)
      Me.panel1.PerformLayout()
      Me.tableLayoutPanel2.ResumeLayout(False)
      Me.tableLayoutPanel2.PerformLayout()
      Me.ResumeLayout(False)
      Me.PerformLayout()
    End Sub 
   
   <STAThread()>  _
   Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New LocalizableForm())
    End Sub
End Class
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TableLayoutPanelSample
{
    public class LocalizableForm : Form
    {
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Label label1;

        private System.ComponentModel.IContainer components = null;

        public LocalizableForm()
        {
            InitializeComponent();
        }

        private void AddText(object sender, EventArgs e)
        {
            ((Button)sender).Text += "x";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            System.Windows.Forms.ListViewGroup listViewGroup16 = new System.Windows.Forms.ListViewGroup("First Group", System.Windows.Forms.HorizontalAlignment.Left);
            System.Windows.Forms.ListViewGroup listViewGroup17 = new System.Windows.Forms.ListViewGroup("Second Group", System.Windows.Forms.HorizontalAlignment.Left);
            System.Windows.Forms.ListViewGroup listViewGroup18 = new System.Windows.Forms.ListViewGroup("Third Group", System.Windows.Forms.HorizontalAlignment.Left);
            System.Windows.Forms.ListViewItem listViewItem26 = new System.Windows.Forms.ListViewItem("Item 1");
            System.Windows.Forms.ListViewItem listViewItem27 = new System.Windows.Forms.ListViewItem("Item 2");
            System.Windows.Forms.ListViewItem listViewItem28 = new System.Windows.Forms.ListViewItem("Item 3");
            System.Windows.Forms.ListViewItem listViewItem29 = new System.Windows.Forms.ListViewItem("Item 4");
            System.Windows.Forms.ListViewItem listViewItem30 = new System.Windows.Forms.ListViewItem("Item 5");
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.listView1 = new System.Windows.Forms.ListView();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.tableLayoutPanel1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.tableLayoutPanel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.listView1, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 0);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(2, 52);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(524, 270);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // listView1
            // 
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            listViewGroup16.Header = "First Group";
            listViewGroup16.Name = null;
            listViewGroup17.Header = "Second Group";
            listViewGroup17.Name = null;
            listViewGroup18.Header = "Third Group";
            listViewGroup18.Name = null;
            this.listView1.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
            listViewGroup16,
            listViewGroup17,
            listViewGroup18});
            //this.listView1.IsBackgroundImageTiled = false;
            listViewItem26.Group = listViewGroup16;
            listViewItem27.Group = listViewGroup16;
            listViewItem28.Group = listViewGroup17;
            listViewItem29.Group = listViewGroup17;
            listViewItem30.Group = listViewGroup18;
            this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem26,
            listViewItem27,
            listViewItem28,
            listViewItem29,
            listViewItem30});
            this.listView1.Location = new System.Drawing.Point(90, 3);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(431, 264);
            this.listView1.TabIndex = 1;
            // 
            // panel1
            // 
            this.panel1.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.panel1.AutoSize = true;
            this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.panel1.Controls.Add(this.button3);
            this.panel1.Controls.Add(this.button2);
            this.panel1.Controls.Add(this.button1);
            this.panel1.Location = new System.Drawing.Point(3, 3);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(81, 86);
            this.panel1.TabIndex = 2;
            // 
            // button3
            // 
            this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button3.AutoSize = true;
            this.button3.Location = new System.Drawing.Point(3, 60);
            this.button3.Name = "button3";
            this.button3.TabIndex = 2;
            this.button3.Text = "Add Text";
            this.button3.Click += new System.EventHandler(this.AddText);
            // 
            // button2
            // 
            this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button2.AutoSize = true;
            this.button2.Location = new System.Drawing.Point(3, 31);
            this.button2.Name = "button2";
            this.button2.TabIndex = 1;
            this.button2.Text = "Add Text";
            this.button2.Click += new System.EventHandler(this.AddText);
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.AutoSize = true;
            this.button1.Location = new System.Drawing.Point(3, 2);
            this.button1.Name = "button1";
            this.button1.TabIndex = 0;
            this.button1.Text = "Add Text";
            this.button1.Click += new System.EventHandler(this.AddText);
            // 
            // tableLayoutPanel2
            // 
            this.tableLayoutPanel2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.tableLayoutPanel2.AutoSize = true;
            this.tableLayoutPanel2.ColumnCount = 3;
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
            this.tableLayoutPanel2.Controls.Add(this.button4, 0, 0);
            this.tableLayoutPanel2.Controls.Add(this.button5, 1, 0);
            this.tableLayoutPanel2.Controls.Add(this.button6, 2, 0);
            this.tableLayoutPanel2.Location = new System.Drawing.Point(284, 328);
            this.tableLayoutPanel2.Name = "tableLayoutPanel2";
            this.tableLayoutPanel2.RowCount = 1;
            this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.tableLayoutPanel2.Size = new System.Drawing.Size(243, 34);
            this.tableLayoutPanel2.TabIndex = 2;
            // 
            // button4
            // 
            this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button4.AutoSize = true;
            this.button4.Location = new System.Drawing.Point(3, 5);
            this.button4.Name = "button4";
            this.button4.TabIndex = 0;
            this.button4.Text = "Add Text";
            this.button4.Click += new System.EventHandler(this.AddText);
            // 
            // button5
            // 
            this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button5.AutoSize = true;
            this.button5.Location = new System.Drawing.Point(84, 5);
            this.button5.Name = "button5";
            this.button5.TabIndex = 1;
            this.button5.Text = "Add Text";
            this.button5.Click += new System.EventHandler(this.AddText);
            // 
            // button6
            // 
            this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.button6.AutoSize = true;
            this.button6.Location = new System.Drawing.Point(165, 5);
            this.button6.Name = "button6";
            this.button6.TabIndex = 2;
            this.button6.Text = "Add Text";
            this.button6.Click += new System.EventHandler(this.AddText);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 7);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(518, 40);
            this.label1.TabIndex = 3;
            this.label1.Text = "Click on any button to add text to the button. This simulates localizing strings," +
                " and provides a good demonstration of how the dialog will automatically adjust w" +
                "hen those longer strings are added to the UI.";
            // 
            // LocalizableForm
            // 
            this.ClientSize = new System.Drawing.Size(539, 374);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.tableLayoutPanel2);
            this.Controls.Add(this.tableLayoutPanel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "LocalizableForm";
            this.Text = "Localizable Dialog";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.tableLayoutPanel2.ResumeLayout(false);
            this.tableLayoutPanel2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new LocalizableForm());
        }
    }
}

请参见

标记 :


Page view tracker