完全在第一个表格的位置显示第二个表格 [英] Showing the second form exactly on the place of first form

查看:35
本文介绍了完全在第一个表格的位置显示第二个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从主窗体 (Form1) 我打电话来显示另一个窗体 (Form2).但我希望它显示与 form1 完全相同的位置和大小,这样我们就无法再看到 form1,直到我们关闭 form2 或将它移到其他地方.所以我写了这些行:

From main form (Form1) I am calling to show another form(Form2). but I want it to show exactly the same place and size that form1 is, so that we wont be able to see form1 anymore until we either close form2 or move it somewhere else. so I wrote these lines:

        Form2 f2 = new Form2();
        f2.Left = this.Left;
        f2.Top = this.Top;
        f2.Size = this.Size;
        f2.Show();

但是还是有问题.form2我们不完全在form1上.我应该在代码中添加任何其他内容吗?

But it still has problems. form2 us not completely on form1. any other thing I should add to the code?

推荐答案

是的,您这样做是错误的.如果您运行它的机器具有完全相同的用户首选项、系统字体大小和视频 DPI 设置,则表单的实际大小仅与设计大小相同.如果它关闭很多,则 DPI 设置不同.如果它关闭一点,则用户偏好不同.像更大的标题栏字体或更大的按钮.修复:

Yes, you are doing this the wrong way around. The actual size of the form is only the same as the design size if the machine you run this on has the exact same user preferences, system font size and video DPI setting. If it is off a lot then the DPI setting is different. If it is off a little then the user preferences are different. Like a larger title bar font or bigger buttons. Fix:

    Form2 f2 = new Form2();
    f2.Show();
    f2.Left = this.Left;
    f2.Top = this.Top;
    f2.Size = this.Size;

如果这太明显了,那么您应该让 Form2 的 Load 事件执行此操作.传递对主窗体的引用或使用 Owner 属性和 Show(owner).换句话说:

If that's too noticeable then you should let the Form2's Load event do this. Pass a reference to your main form or use the Owner property and Show(owner). In other words:

    Form2 f2 = new Form2();
    f2.Show(this);

在 Form2 中:

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Location = Owner.Location;
        this.Size = Owner.Size;
    }

这篇关于完全在第一个表格的位置显示第二个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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