简体   繁体   English

如何在打开至少一个模态窗口时最小化winforms应用程序

[英]How to minimize a winforms app when there is at least one modal window opened

I have two form classes (Form1 and Form2) in a Winforms App. 我在Winforms应用程序中有两个表单类(Form1和Form2)。

Form1 is like this: Form1是这样的:

Form1上有一个按钮

Form2 is like this (ShowInTaskbar = false): Form2是这样的(ShowInTaskbar = false):

Form2带有“确定”和“取消”按钮

And this code on Form1: 这个代码在Form1上:

    Form2 someForm = new Form2();
    private void btOpenAnotherWindow_Click(object sender, EventArgs e)
    {
        if (someForm.ShowDialog(this) == DialogResult.OK)
            MessageBox.Show("OK!!!");
        else
            MessageBox.Show("Not OK.");
    }

That is, a window with a button which opens modally another windows when clicked, and waits for the user to close the second window (by clicking the "OK" or "Cancel" button). 也就是说,一个带有按钮的窗口,在单击时以模态方式打开另一个窗口,并等待用户关闭第二个窗口(通过单击“确定”或“取消”按钮)。 And depending how it was closed, do alternating actions (represented here by the MessageBox.Show() calls). 并根据它是如何关闭的,执行交替操作(此处由MessageBox.Show()调用表示)。

I need: 我需要:

  1. The user can use only one window at a time. 用户一次只能使用一个窗口。 (Modal forms, That's why I used ShowDialog() instead of Show()) (模态形式,这就是我使用ShowDialog()而不是Show()的原因)
  2. When the form closes, do something depending on how the form was closed (the "if (someForm.ShowDialog(this)...") 当表单关闭时,根据表单的关闭方式做一些事情(“if(someForm.ShowDialog(this)...”)
  3. To be able (as a user) to minimize the WHOLE APP. 能够(作为用户)最小化整个APP。
  4. To be able to "unminimize" the app to the former state correctly. 能够正确地将应用程序“最小化”到前一状态。
  5. The program to respond to WIN+M (minimize all) keys combination. 该程序响应WIN + M(最小化所有)键组合。

the above example fails in two ways: 以上示例以两种方式失败:

  1. (need 5) Doesn't respond to WIN+M (需要5)不响应WIN + M
  2. (need 3) The app seems to minimize when the Minimize title bar button is clicked, but it is an illusion because the main form (Form1) does not minimize and it is in fact just hidden behind the other opened windows. (需要3)应用程序似乎在点击最小化标题栏按钮时最小化,但它是一种错觉,因为主窗体(Form1)没有最小化,实际上它只是隐藏在其他打开的窗口后面。 Only running the example with an empty desktop shows what really happens. 只运行带有空桌面的示例才能显示实际发生的情况。 Pics follow: 照片如下:

Before Minimize button is clicked: 单击最小化按钮之前:

应用程序运行的空桌面

After: 后: 使用Form2(someForm)的空桌面最小化

Note: 注意:

  1. The Main form is not minimized 主窗体未最小化
  2. The Form2 is in the left botton corner of the screen. Form2位于屏幕的左下角。

Form2 is a full blown window (not a dialog window per se) and I need the user to interact with it only until it is closed and I also need the user to be able to miminize the whole app in case he needs it. Form2是一个完整的窗口(本身不是一个对话框窗口),我需要用户与它进行交互,直到它关闭,我还需要用户能够在需要时将整个应用程序缩小。

It is a shame I can't post here the real forms, It would be clearer than these mock-ups. 遗憾的是我无法在这里发布真实的形式,这将比这些模型更清晰。

I need a solution that works with many levels of modal windows (not only two as this example shows). 我需要一个适用于多级模态窗口的解决方案(不仅如本例所示,还有两个)。 Any suggestions? 有什么建议?

I may need a little more information about what you're trying to do here. 我可能需要更多关于你在这里尝试做什么的信息。 I have a simple form (Form1) with a button on it, which calls this code: 我有一个简单的表单(Form1),上面有一个按钮,调用此代码:

  private void button1_Click(object sender, EventArgs e)
  {
     Form1 form2 = new Form1();
     form2.ShowDialog();
  }

When I click the button, I get a second instance of the same form, but it's modal. 当我单击按钮时,我得到了相同表单的第二个实例,但它是模态的。 I still have the option to minimize this second modal form (I obviously can't interact with the first form), and when I do minimize the second form it does minimize the entire application (both forms). 我仍然可以选择最小化第二个模态形式(我显然不能与第一个形式交互),当我最小化第二个形式时,它确实最小化了整个应用程序(两种形式)。 Now obviously you're asking the question, so I don't think I'm understanding you. 现在显然你在问这个问题,所以我不认为我理解你。 =) What about this scenario do you wish to change? =)你希望改变这种情况怎么样?

  • C C

There is probably some way to hack this functionality using API calls, but I would probably suggest doing some type of overlay with a control inside your main form rather than an actual window. 可能有一些方法可以使用API​​调用破解这个功能,但我可能会建议在主窗体中使用控件而不是实际窗口进行某种类型的叠加。 This would allow you to make it "modal" and still have the ability to minimize/resize the main window. 这将允许您使其“模态”并仍然能够最小化/调整主窗口的大小。

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

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