开发者

Minimize/Maximize both the screens

I have an application which runs on 2 monitors开发者_Python百科 (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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜