我有一个在2台显示器上运行的应用程序(比如主屏幕和辅助屏幕)。辅助屏幕没有设置控制框属性。它完全取决于主屏幕的功能。我无法弄清楚的是,如果我将主屏幕最小化到系统任务栏,我也需要最小化辅助屏幕。一旦我最大化了主要,次要也应该最大化。
更新 我厌倦了做这样的事情,但搞砸了。一旦应用程序启动它就会创建两个表单,这是我不想要的。
private void MainForm_Resize(object sender, EventArgs e)
{
SecondaryLayoutForm secondaryLayoutForm = new SecondaryLayoutForm();
if (this.WindowState == FormWindowState.Minimized)
{
secondaryLayoutForm.Hide();
}
else
{
secondaryLayoutForm.Show();
}
}
由于
答案 0 :(得分:1)
在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();
}
}
}
但是,无论您是从其中一个表单还是单独的类执行此操作,都需要对两个表单进行引用,以便它可以同步窗口状态。