最小化/最大化两个屏幕

本文关键字:两个 屏幕 最大化 最小化 | 更新日期: 2023-09-27 18:05:32

我有一个应用程序,运行在2个显示器(说主屏幕和副屏幕)。辅助屏幕没有为其设置控制框属性。这完全取决于主屏幕的功能。我不明白的是,如果我最小化主屏幕到系统任务栏,我也需要最小化副屏幕。而且,一旦我最大化了主要,次要也应该最大化。

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

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

谢谢

最小化/最大化两个屏幕

在你的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();
        }
    }
}

然而,无论你是从一个窗体还是一个单独的类中执行此操作,都需要对两个窗体的引用,以便它可以同步窗口状态。