代码改变世界

动态设置窗体大小

2005-05-19 17:22  稻草人  阅读(1719)  评论(0)    收藏  举报

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 实例5
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.NumericUpDown numericUpDown1;
        
private System.Windows.Forms.NumericUpDown numericUpDown2;
        
private System.Windows.Forms.NumericUpDown numericUpDown3;
        
private System.Windows.Forms.NumericUpDown numericUpDown4;
        
private System.Windows.Forms.Button button1;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

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

            }

            
base.Dispose( disposing );
        }


        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.groupBox1 = new System.Windows.Forms.GroupBox();
            
this.label1 = new System.Windows.Forms.Label();
            
this.label2 = new System.Windows.Forms.Label();
            
this.label3 = new System.Windows.Forms.Label();
            
this.label4 = new System.Windows.Forms.Label();
            
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            
this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
            
this.numericUpDown3 = new System.Windows.Forms.NumericUpDown();
            
this.numericUpDown4 = new System.Windows.Forms.NumericUpDown();
            
this.button1 = new System.Windows.Forms.Button();
            
this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown4)).BeginInit();
            
this.SuspendLayout();
            
// 
            
// groupBox1
            
// 
            this.groupBox1.Controls.Add(this.label1);
            
this.groupBox1.Controls.Add(this.label2);
            
this.groupBox1.Controls.Add(this.label3);
            
this.groupBox1.Controls.Add(this.label4);
            
this.groupBox1.Controls.Add(this.numericUpDown2);
            
this.groupBox1.Controls.Add(this.numericUpDown3);
            
this.groupBox1.Controls.Add(this.numericUpDown4);
            
this.groupBox1.Location = new System.Drawing.Point(88);
            
this.groupBox1.Name = "groupBox1";
            
this.groupBox1.Size = new System.Drawing.Size(264256);
            
this.groupBox1.TabIndex = 0;
            
this.groupBox1.TabStop = false;
            
this.groupBox1.Text = "groupBox1";
            
// 
            
// label1
            
// 
            this.label1.Location = new System.Drawing.Point(1624);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(9632);
            
this.label1.TabIndex = 1;
            
this.label1.Text = "最小宽度";
            
// 
            
// label2
            
// 
            this.label2.Location = new System.Drawing.Point(1664);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(9632);
            
this.label2.TabIndex = 2;
            
this.label2.Text = "最大宽度";
            
// 
            
// label3
            
// 
            this.label3.Location = new System.Drawing.Point(16104);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(9640);
            
this.label3.TabIndex = 3;
            
this.label3.Text = "最小高度";
            
// 
            
// label4
            
// 
            this.label4.Location = new System.Drawing.Point(16152);
            
this.label4.Name = "label4";
            
this.label4.Size = new System.Drawing.Size(9640);
            
this.label4.TabIndex = 4;
            
this.label4.Text = "最大高度";
            
// 
            
// numericUpDown1
            
// 
            this.numericUpDown1.Location = new System.Drawing.Point(12816);
            
this.numericUpDown1.Name = "numericUpDown1";
            
this.numericUpDown1.Size = new System.Drawing.Size(12821);
            
this.numericUpDown1.TabIndex = 5;
            
// 
            
// numericUpDown2
            
// 
            this.numericUpDown2.Location = new System.Drawing.Point(12064);
            
this.numericUpDown2.Name = "numericUpDown2";
            
this.numericUpDown2.Size = new System.Drawing.Size(12821);
            
this.numericUpDown2.TabIndex = 6;
            
// 
            
// numericUpDown3
            
// 
            this.numericUpDown3.Location = new System.Drawing.Point(120104);
            
this.numericUpDown3.Name = "numericUpDown3";
            
this.numericUpDown3.Size = new System.Drawing.Size(12821);
            
this.numericUpDown3.TabIndex = 7;
            
// 
            
// numericUpDown4
            
// 
            this.numericUpDown4.Location = new System.Drawing.Point(120160);
            
this.numericUpDown4.Name = "numericUpDown4";
            
this.numericUpDown4.Size = new System.Drawing.Size(12821);
            
this.numericUpDown4.TabIndex = 8;
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(120224);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(12824);
            
this.button1.TabIndex = 9;
            
this.button1.Text = "设置";
            
this.button1.Click += new System.EventHandler(this.button1_Click);
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.AutoScroll = true;
            
this.BackColor = System.Drawing.SystemColors.ControlLight;
            
this.ClientSize = new System.Drawing.Size(292273);
            
this.Controls.Add(this.button1);
            
this.Controls.Add(this.numericUpDown1);
            
this.Controls.Add(this.groupBox1);
            
this.Name = "Form1";
            
this.Text = "设置窗体大小";
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.groupBox1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(
this.numericUpDown4)).EndInit();
            
this.ResumeLayout(false);

        }

        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

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


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
        
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
            
int widthmin=System.Decimal .ToInt32(numericUpDown1.Value);
            
int heightmin=System.Decimal .ToInt32 (numericUpDown3.Value);
            
int widthmax=System.Decimal .ToInt32 (numericUpDown2.Value);
            
int heightmax=System.Decimal .ToInt32(numericUpDown4.Value );
            DialogResult dr,dr2;
            
if(widthmin>widthmax)
            
{
                dr
=MessageBox.Show("设置的最小宽度比最大宽度还大,如果坚持此投置,窗体的宽度将固定为最大宽度","警告",MessageBoxButtons.OKCancel ,MessageBoxIcon.Warning );
                
if (dr ==DialogResult.OK )
                
{
                    
//没有任何处理
                }

                
else if (dr==DialogResult.Cancel )
                
{
                    numericUpDown1.Value
=300;
                    numericUpDown2.Value
=300;
                }

            }

            
if(heightmin>heightmax)
            
{
                dr2
=MessageBox.Show ("设置的最小高度比最大高度还大,如果坚持此设置,窗体的宽度将因定为最大高度",
                                                                                
"警告",MessageBoxButtons.OKCancel ,MessageBoxIcon.Warning );
                
//要椐对话框的返回值进行处理,如果是Cancel则将值的设置还原
                if(dr2==DialogResult.Cancel )
                
{
                    
//没有任何处理
                }

                
else if (dr2==DialogResult.Cancel )
                
{
                    numericUpDown3.Value
=300;
                    numericUpDown4.Value
=300;
                }

            }

            widthmin
=System.Decimal.ToInt32 (numericUpDown1.Value );
            heightmin
=System.Decimal .ToInt32 (numericUpDown2.Value );
            widthmax
=System.Decimal .ToInt32 (numericUpDown3.Value);
            heightmax
=System.Decimal .ToInt32 (numericUpDown4.Value);
            Size P1
=new Size (widthmin,heightmin);
            Size P2
=new Size (widthmax,heightmax);
            
//设置窗体的最小宽度和最小高度
            this.MinimumSize=P1;
            
//设置窗体的最大宽度和最大高度
            this.MaximumSize =P2;
        }

    }

}

前些时候完成了ASP.NET的配置,突然想做做网站的东西,可是真到做的时候又觉得懂的太少,所以就学学窗体编程,都是一些简单的东东,这是我早上下午花了两个小时才写完的代码,感觉写代码是件痛苦的事情...