我无法在FormLoad中调整表单的大小 [英] I could not Resize the form in FormLoad

查看:58
本文介绍了我无法在FormLoad中调整表单的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 一半 屏幕大小的形式.这就是我写的内容

I want to set the size of the form half the size of the screen.this is what i have wrote

<pre lang="cs">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 ScreenSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
          int ScreenWidth, ScreenHeight;
          ScreenWidth =System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
          ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

          MessageBox.Show("ScreenWidth =" + ScreenWidth.ToString(), " ScreenHeight =" + ScreenHeight.ToString());

//none of the following works
        //Form1.ActiveForm.Size = new System.Drawing.Size(ScreenWidth /2, ScreenHeight /2);
        //Form1.Size = new System.Drawing.Size(ScreenWidth /2, ScreenHeight /2);
        //Form1.ActiveForm.Width=ScreenWidth/2;
        //Form1.ActiveForm.Height=ScreenHeight/2;

        }
    }
}


推荐答案

是的,但是您没有这样做:
Yeah, but you didn''t do this:
this.Size = new Size(ScreenWidth/2, ScreenHeight/2);


您不需要Form1.ActiveForm-您想影响当前实例.
试试:
You don''t need Form1.ActiveForm - you want to affect the current instance.
Try:
private void frmTesting_Load(object sender, EventArgs e)
    {
    int ScreenWidth, ScreenHeight;
    ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
    ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
    Width = ScreenWidth / 2;
    Height = ScreenHeight / 2;
    }


这应该可以解决您的问题
this should fix your problem
int ScreenWidth = Screen.GetWorkingArea(this).Width / 2;
            int ScreenHeight = Screen.GetWorkingArea(this).Height / 2;
this.Size = new Size(ScreenWidth, ScreenHeight);



如果您希望表单居中,请尝试使用此



if you want the form to be centered try to use this

this.StartPosition = FormStartPosition.Manual;            
            int ScreenWidth = Screen.GetWorkingArea(this).Width / 4;
            int ScreenHeight = Screen.GetWorkingArea(this).Height / 4;
            this.Location = new Point(ScreenWidth, ScreenHeight);
            this.Size = new Size(ScreenWidth*2, ScreenHeight*2);


这篇关于我无法在FormLoad中调整表单的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆