Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

c# - How to minimize form from taskbar?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...