简体   繁体   English

当我/拖动/移动新窗体后面的form1使其同时与form1一起移动时,我该如何做?

[英]How can i make that when i /drag/move form1 around the new form behind it will move with form1 on the same time?

I don't want to see the new form. 我不想看到新表格。 I need him to be maximized and same size as form1 but not to see it. 我需要最大化他,使其大小与form1相同,但不要看到它。 So when i move/drag form1 around with the mouse i need that the new form behind it will be move/drag with form1. 因此,当我用鼠标来回移动/拖动form1时,我需要它后面的新窗体将与form1一起移动/拖动。

This is a button click event in form1 that show the new form: 这是显示新表单的form1中的按钮单击事件:

private void BeginOperationBut_Click(object sender, EventArgs e)
        {
            if (this.imageList.Count == 0)
            {
                wbss = new WebBrowserScreenshots();
                wbss.Show();
            }
        }

wbss is the new form. wbss是新形式。

I tried in the new form designer to set the property Locked to True but it didn't change anything. 我尝试在新的表单设计器中将属性Locked设置为True,但它没有做任何更改。

What i want to do is two things: 我想做的是两件事:

  1. The new form should be show/start behind form1. 新表格应显示/在form1之后开始。
  2. The new form should be locked so when i drag/move around the form it will move the new form also. 新表格应被锁定,因此当我在表格上拖动/移动时,它也会同时移动新表格。 So i will not see the new form only form1. 因此,我将不会看到仅form1的新表格。

The way it is now when i click the button the new form is showing over in front of form1. 现在,当我单击按钮时,新表单将显示在form1前面。

There are a lot of things that need to be taken into consideration to make sure that this works as intended. 为了确保此功能能够按预期工作,需要考虑很多因素。 Here is a snippet of code you can use to get started: 这是您可以用来入门的代码片段:

public partial class FollowForm : Form {

    readonly Form _master;

    private FollowForm() {
        InitializeComponent();
    }

    public FollowForm(Form master) {
        if (master == null)
            throw new ArgumentNullException("master");
        _master = master;
        _master.LocationChanged += (s, e) => Location = _master.Location;
        _master.SizeChanged += (s, e) => Size = _master.Size;
        ShowInTaskbar = false;
    }

    protected override void OnShown(EventArgs e) {
        base.OnShown(e);
        Location = _master.Location;
        Size = _master.Size;
        _master.Activate();
    }

    protected override void OnActivated(EventArgs e) {
        _master.Activate();
    }
}

I tried experimenting with the ShowWithoutActivation property, but the results were not as I expected. 我尝试使用ShowWithoutActivation属性进行实验,但结果与我预期的不同。

The main aspects are tracking the change of size or location of the master form, and updating the follower form's size and location accordingly. 主要方面是跟踪主表单的大小或位置的变化,并相应地更新跟随者表单的大小和位置。 Also, to send the follower form to the back when shown and re-activating the master form. 同样,在显示时将关注者表单发送到背面并重新激活主表单。 Give it a try. 试试看。 This is from a .NET4.0 project, VS2013. 这来自.NET4.0项目VS2013。

Usage 用法

public partial class MasterForm : Form {

    FollowForm _follower;

    public MasterForm() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        _follower = new FollowForm(this);
        _follower.Show();
    }
}

For a better approach to the no-activate feature, take a look at: 为了更好地使用无激活功能,请查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM