8

I have developed winform application and I have set formborderstyle=none. Thatz why when I am running application I can't minimize it through taskbar. Does any body knows solution for this?

I tried following code.. adding it in my form.

    const int WS_CLIPCHILDREN = 0x2000000;
    const int WS_MINIMIZEBOX = 0x20000;
    const int WS_MAXIMIZEBOX = 0x10000;
    const int WS_SYSMENU = 0x80000;
    const int CS_DBLCLKS = 0x8;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style = WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU;
            cp.ClassStyle = CS_DBLCLKS;
            return cp;
        }
    }

I am now able to minimize the application from taskbar. But the problem is it is creating two intances of my application one which I need and the other which is unneccessary.

Does any body knows solution for this.. or does anyone has some other solution which works ?

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
Sachin Patil
  • 171
  • 1
  • 3
  • 10
  • 1
    Every style flag that you've overridden `CreateParams` to include is already exposed by the .NET Framework. There's absolutely no reason you should have to use code like that in the first place. I also don't understand what you mean by "minimize form from taskbar". When the application is open, click on its icon in the taskbar, and it will minimize. Quite simple, no code required. Windows natively supports this. Beyond that, I can guarantee that the code you've shown will *not* create two instances of your application. Something else is wrong, but you haven't given us enough information. – Cody Gray - on strike Mar 03 '11 at 12:22
  • 2
    The thing is when you set formborderstyle=none then at runtime you cannnot minimize application from taskbar ... try it yourself.. – Sachin Patil Mar 03 '11 at 12:24
  • and that two instances problem is only creeted because of above code that I can guarantee because when I am running application on commenting above code it's working fine. guys plz help.. – Sachin Patil Mar 03 '11 at 12:26
  • Yeah, you're right. I missed the part where you said you were disabling the form's border. Seemed illogical that you'd want to minimize a form that didn't have a border. Doesn't change the fact that I can't repro the behavior you describe. Forms don't just decide to instantiate another instance of themselves when the user clicks on their icon in the taskbar. You have some code that's responsible for doing that. Take that out and your problem is solved. – Cody Gray - on strike Mar 03 '11 at 12:58

1 Answers1

18

A borderless form should always be one that the user is not expected to minimize. The discoverability principle starts to apply here: most users don't know that you can minimize a window by clicking on its taskbar icon. They're going to expect to be able to do it by clicking the button next to the big red x.

The right solution is to choose a different border style for your form, one that includes the title bar and the minimize box. Windows will automatically behave as expected. Things are much easier when you follow the standard conventions of your platform, not only for you as a programmer, but for your users. It also fixes that nasty flickering effect when your form is created or restored where I can see the standard caption bar for a few seconds.

Of course, you'll inevitably want to do this anyway, so despite my better judgement, I'll try to provide a solution. The first problem is I can't reproduce the behavior you describe (Windows Server 2008 R2, .NET 4.0). Adding exactly the code shown to a new WinForms project, and setting the form's FormBorderStyle property to "None", there's no way I can get two windows to show up. Clicking on the taskbar icon causes the form to minimize, clicking it again restores it.

But there is a way to simplify your code. And you should probably be OR-ing the style flags that you're adding with the existing style flags, rather than replacing the existing flags. Replace your code with this:

const int WS_MINIMIZEBOX = 0x20000;
const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style |= WS_MINIMIZEBOX;
        cp.ClassStyle |= CS_DBLCLKS;
        return cp;
    }
}

If that doesn't fix your problem (and I'm skeptical that it will), then as I suspected, there's something else wrong in your code that you haven't shown us. Just because you can comment out a few lines of code and your program works as expected doesn't necessarily imply that the problem lies in those lines of code. They can be perfectly correct, but interfering with a hack you've used elsewhere.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks Code Gray !!! Your code works fine for me... No problem with two instances of the application. It really helped me a lot !!! – Sachin Patil Mar 03 '11 at 13:10
  • I had the same issue with net framework 3.5 and the borderstyle=none. I was going to reflect it, but decided on a web search first. This worked great. – nate Apr 10 '12 at 14:41