winform 自适应屏幕分辨率

每个FORM设计的内容格式会不一样,但设计时遵循一种规律就是 :不要为了简单方便省略容器的使用;我基本上在设计时所有的控件一半都不直接放在form上;比如panel不要和控件放在一层,panel与panel之间使用dock属性排好,除非特殊需求一般不要对非容器性控件直接使用dock,form界面设计不像一个函数一样是什么功能就是什么功能,没有一个可以应用于所有form 的设计方法,它只有规律!


1、判断窗体的控件布局
2、根据布局放置Panel控件,根据需要可以嵌套N层的Panel
3、设置每个Panel的Dock属性
4、把对应的控件放入合适的Panel中,设置每个控件的Anchor属性
5、在FormResize事件中调整Panel的大小

这样可以适应任何分辨率的显示问题

//窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Test
{
    public partial class Form1 : Form
    {
      


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Resize(object sender, EventArgs e)
        {
            panel6.Width = this.Width / 2;
            panel4.Width = this.Width / 2;
        }
    }
}



//设计器

using System;
namespace Test
{
    partial class Form1
    {
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.Panel panel4;
        private System.Windows.Forms.Panel panel5;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Panel panel6;
        private System.Windows.Forms.Panel panel7;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.TextBox textBox6;


        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows 窗体设计器生成的代码


        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.panel4 = new System.Windows.Forms.Panel();
this.panel5 = new System.Windows.Forms.Panel();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel6 = new System.Windows.Forms.Panel();
this.panel7 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.panel4.SuspendLayout();
this.panel5.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.panel6.SuspendLayout();
this.panel7.SuspendLayout();
this.SuspendLayout();
// 
// panel1
// 
this.panel1.Controls.Add(this.panel7);
this.panel1.Controls.Add(this.panel6);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(496, 48);
this.panel1.TabIndex = 0;
// 
// panel2
// 
this.panel2.Controls.Add(this.textBox5);
this.panel2.Controls.Add(this.textBox6);
this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel2.Location = new System.Drawing.Point(0, 237);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(496, 48);
this.panel2.TabIndex = 1;
// 
// panel3
// 
this.panel3.Controls.Add(this.panel5);
this.panel3.Controls.Add(this.panel4);
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(0, 48);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(496, 189);
this.panel3.TabIndex = 2;
// 
// panel4
// 
this.panel4.Controls.Add(this.listBox1);
this.panel4.Dock = System.Windows.Forms.DockStyle.Left;
this.panel4.Location = new System.Drawing.Point(0, 0);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(200, 189);
this.panel4.TabIndex = 0;
// 
// panel5
// 
this.panel5.Controls.Add(this.dataGrid1);
this.panel5.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel5.Location = new System.Drawing.Point(200, 0);
this.panel5.Name = "panel5";
this.panel5.Size = new System.Drawing.Size(296, 189);
this.panel5.TabIndex = 1;
// 
// dataGrid1
//


// 
// dataGrid1
// 
this.dataGrid1.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.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(296, 189);
this.dataGrid1.TabIndex = 0;
// 
// listBox1
// 
this.listBox1.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.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(200, 184);
this.listBox1.TabIndex = 0;
// 
// panel6
// 
this.panel6.Controls.Add(this.textBox2);
this.panel6.Controls.Add(this.textBox1);
this.panel6.Dock = System.Windows.Forms.DockStyle.Left;
this.panel6.Location = new System.Drawing.Point(0, 0);
this.panel6.Name = "panel6";
this.panel6.Size = new System.Drawing.Size(240, 48);
this.panel6.TabIndex = 0;
// 
// panel7
// 
this.panel7.Controls.Add(this.textBox3);
this.panel7.Controls.Add(this.textBox4);
this.panel7.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel7.Location = new System.Drawing.Point(240, 0);
this.panel7.Name = "panel7";
this.panel7.Size = new System.Drawing.Size(256, 48);
this.panel7.TabIndex = 1;
// 
// textBox1
// 
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(4, 4);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(236, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
// 
// textBox2
// 
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(4, 25);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(236, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
// 
// textBox3
// 
this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox3.Location = new System.Drawing.Point(4, 24);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(252, 21);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "textBox3";
// 
// textBox4
// 
this.textBox4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox4.Location = new System.Drawing.Point(4, 3);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(252, 21);
this.textBox4.TabIndex = 2;
this.textBox4.Text = "textBox4";
// 
// textBox5
// 
this.textBox5.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox5.Location = new System.Drawing.Point(4, 24);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(492, 21);
this.textBox5.TabIndex = 3;
this.textBox5.Text = "textBox5";
// 
// textBox6
// 
this.textBox6.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox6.Location = new System.Drawing.Point(4, 3);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(492, 21);
this.textBox6.TabIndex = 2;
this.textBox6.Text = "textBox6";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 285);
this.Controls.Add(this.panel3);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.panel4.ResumeLayout(false);
this.panel5.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.panel6.ResumeLayout(false);
this.panel7.ResumeLayout(false);
this.ResumeLayout(false);
this.Resize +=new EventHandler(Form1_Resize);


        }


        #endregion
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值