最小化/最大化两个屏幕

发布于 2024-11-30 04:52:35 字数 541 浏览 1 评论 0原文

我有一个在两个显示器上运行的应用程序(例如主屏幕和辅助屏幕)。辅助屏幕没有设置控制框属性。这完全取决于主屏幕的功能。我无法弄清楚的是,如果我将主屏幕最小化到系统任务栏,我也需要最小化辅助屏幕。此外,一旦我最大化主要部分,次要部分也应该最大化。

更新 我厌倦了做这样的事情,但搞砸了。应用程序一启动它就会创建 2 个表单,这是我不想要的。

private void MainForm_Resize(object sender, EventArgs e)
{
     SecondaryLayoutForm secondaryLayoutForm = new SecondaryLayoutForm();
     if (this.WindowState == FormWindowState.Minimized)
     {
         secondaryLayoutForm.Hide();
     }
     else
     {
         secondaryLayoutForm.Show();
     }
}

谢谢

I have an application which runs on 2 monitors (say Primary screen and Secondary screen). The secondary screen does not have the control box property set to it. It depends completely on the functionality of the primary screen. What I am not able to figure out is, if I minimize the primary screen to the system taskbar, I need to minimize the secondary screen too. Also as soon as I maximize the primary, the secondary should also be maximized.

UPDATE
I tired to do something like this, but messed it up. It creates 2 forms as soon as the application starts which I do not want.

private void MainForm_Resize(object sender, EventArgs e)
{
     SecondaryLayoutForm secondaryLayoutForm = new SecondaryLayoutForm();
     if (this.WindowState == FormWindowState.Minimized)
     {
         secondaryLayoutForm.Hide();
     }
     else
     {
         secondaryLayoutForm.Show();
     }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2024-12-07 04:52:36

在您的 MainForm 类上尝试一下:

private SecondaryLayoutForm secondaryLayoutForm;

private void MainForm_Resize(object sender, EventArgs e)
{
   if (this.secondaryLayoutForm == null)
   {
      this.secondaryLayoutForm = new SecondaryLayoutForm();
   }

   // replace the 'this.ShouldShowSecondaryForm()' call with whatever 
   // your special condition is for showing the secondary form.
   if (this.WindowState != FormWindowState.Minimized && this.ShouldShowSecondaryForm())
   {
        this.secondaryLayoutForm.Show();
   }
   else
   {
        this.secondaryLayoutForm.Hide();
   }
}

这使得 MainForm 负责创建您的 secondaryLayoutForm 并控制其窗口状态 - 如果您不希望这样做,请考虑创建一个 UIManager 类或其他东西来将其分开。

更新

我用 2 个表单编写的示例应用程序。这就是 Form1 中的内容,Form2 上只有一个文本框,因此它与 Form1 明显不同。

public partial class Form1 : Form
{
    private Form2 f2;

    public Form1()
    {
        InitializeComponent();
        this.Resize += new System.EventHandler(Form1_Resize);

        // Initialize Form2 
        f2 = new Form2();
    }

    void Form1_Resize(object sender, System.EventArgs e)
    {
       // whenever I change the size of Form1, make sure 
       // Form2 has the same WindowState
       f2.WindowState = this.WindowState;
    }

    // This is your condition to either show or hide the form.
    //
    // Rather than a checkbox, have something that will respond to whatever 
    // condition you have set out - probably an event on another class.
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            f2.Show();
        }
        else
        {
            f2.Hide();
        }
    }
}

更新:如何从不同的类执行此操作的示例

public partial class UIManager
{
    private Form2 f1;
    private Form2 f2;
    private bool shouldShowForm2 = false;

    public bool ShouldShowForm2 
    { 
       get { return shouldShowForm2; }
       set { shouldShowForm2 = value;  OnShouldShowForm2Changed(); }
    }

    public UIManager()
    {
        InitializeComponent();

        // Initialize Forms
        f1 = new Form1();
        f2 = new Form2();
        f1.Resize += new System.EventHandler(f1_Resize);
        f1.Show();
    }

    void f1_Resize(object sender, System.EventArgs e)
    {
       // whenever I change the size of Form1, make sure 
       // Form2 has the same WindowState
       f2.WindowState = f1.WindowState;
    }

    // This is your condition to either show or hide the form.
    private void OnShouldShowForm2Changed(object sender, EventArgs e)
    {
        if (ShouldShowForm2)
        {
            f2.Show();
        }
        else
        {
            f2.Hide();
        }
    }
}

但是,无论您是从其中一个表单还是单独的类执行此操作,都需要引用这两种表单,以便它可以同步窗口状态。

Try this, on your MainForm class:

private SecondaryLayoutForm secondaryLayoutForm;

private void MainForm_Resize(object sender, EventArgs e)
{
   if (this.secondaryLayoutForm == null)
   {
      this.secondaryLayoutForm = new SecondaryLayoutForm();
   }

   // replace the 'this.ShouldShowSecondaryForm()' call with whatever 
   // your special condition is for showing the secondary form.
   if (this.WindowState != FormWindowState.Minimized && this.ShouldShowSecondaryForm())
   {
        this.secondaryLayoutForm.Show();
   }
   else
   {
        this.secondaryLayoutForm.Hide();
   }
}

This makes the MainForm responsible for creating your SecondaryLayoutForm and controlling its window state - if you don't want that, then consider creating a UIManager class or something to separate this out.

Update

A sample app I wrote with 2 forms. This is what's in Form1, Form2 just has a text box on it so it's visibly different to Form1.

public partial class Form1 : Form
{
    private Form2 f2;

    public Form1()
    {
        InitializeComponent();
        this.Resize += new System.EventHandler(Form1_Resize);

        // Initialize Form2 
        f2 = new Form2();
    }

    void Form1_Resize(object sender, System.EventArgs e)
    {
       // whenever I change the size of Form1, make sure 
       // Form2 has the same WindowState
       f2.WindowState = this.WindowState;
    }

    // This is your condition to either show or hide the form.
    //
    // Rather than a checkbox, have something that will respond to whatever 
    // condition you have set out - probably an event on another class.
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            f2.Show();
        }
        else
        {
            f2.Hide();
        }
    }
}

Update: Example of how to do this from a different class

public partial class UIManager
{
    private Form2 f1;
    private Form2 f2;
    private bool shouldShowForm2 = false;

    public bool ShouldShowForm2 
    { 
       get { return shouldShowForm2; }
       set { shouldShowForm2 = value;  OnShouldShowForm2Changed(); }
    }

    public UIManager()
    {
        InitializeComponent();

        // Initialize Forms
        f1 = new Form1();
        f2 = new Form2();
        f1.Resize += new System.EventHandler(f1_Resize);
        f1.Show();
    }

    void f1_Resize(object sender, System.EventArgs e)
    {
       // whenever I change the size of Form1, make sure 
       // Form2 has the same WindowState
       f2.WindowState = f1.WindowState;
    }

    // This is your condition to either show or hide the form.
    private void OnShouldShowForm2Changed(object sender, EventArgs e)
    {
        if (ShouldShowForm2)
        {
            f2.Show();
        }
        else
        {
            f2.Hide();
        }
    }
}

However, whether you do this from one of the forms or a separate class, something will need a reference to both forms so it can synchronize the window states.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文