实例042-设置窗体尺寸-根据屏幕尺寸设置窗口尺寸

上一节讲到了获取屏幕尺寸的方法。

本例程序下载地址:https://download.csdn.net/download/chongxing01/12266008

这一节讲设置窗体尺寸的方法,并且可以将窗体的尺寸设置为和屏幕尺寸相同。

 

实例042-设置窗体尺寸-根据屏幕尺寸设置窗口尺寸_第1张图片

1.首先创建项目。

2.按照上面的图,添加控件。

3.添加尺寸参数变量。

        int left, top, right, bottom;
        int max_hight, max_width;

4.程序启动的时候,获取参数值。

        private void Form1_Load(object sender, EventArgs e)
        {
            left = Left;
            right = Right;
            top = Top;
            bottom = Bottom;

            max_hight = Screen.PrimaryScreen.WorkingArea.Height;
            max_width = Screen.PrimaryScreen.WorkingArea.Width;

            textBox3.Text = (bottom - top).ToString();
            textBox4.Text = (right - left).ToString();

            textBox2.Text = max_hight.ToString();
            textBox1.Text = max_width.ToString();
        }

5.添加设置为桌面大小的按钮事件

        private void button2_Click(object sender, EventArgs e)
        {
            Left = 0;
            Top = 0;
            Width = max_width;
            Height = max_hight;
        }

6.添加恢复为原始尺寸的按钮事件

        private void button3_Click(object sender, EventArgs e)
        {
            Left = left;
            Top = top;
            Width = right - left;
            Height = bottom - top;
        }

7.后面有下载程序,可以下载后测试。

 

你可能感兴趣的:(C#程序开发范例宝典)