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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…