开发者

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.

Form1 is like this:

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

Form2 is like this (ShowInTaskbar = false):

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

And this code on 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() ca开发者_运维问答lls).

I need:

  1. The user can use only one window at a time. (Modal forms, That's why I used ShowDialog() instead of Show())
  2. When the form closes, do something depending on how the form was closed (the "if (someForm.ShowDialog(this)...")
  3. To be able (as a user) to minimize the WHOLE 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.

the above example fails in two ways:

  1. (need 5) Doesn't respond to 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. Only running the example with an empty desktop shows what really happens. Pics follow:

Before Minimize button is clicked:

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

After:

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

Note:

  1. The Main form is not minimized
  2. The Form2 is in the left botton corner of the screen.

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.

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:

  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


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. This would allow you to make it "modal" and still have the ability to minimize/resize the main window.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜