Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize form from taskbar?

Tags:

c#

winforms

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 ?

like image 778
Sachin Patil Avatar asked Mar 03 '11 12:03

Sachin Patil


People also ask

How do I minimize a window to the taskbar?

Right-click on any minimize button to minimize its window to the notification area. Alternatively, hold Shift while right-clicking on the title bar of any Window for the same effect. You can minimize the active window with the keyboard shortcut WIN+Alt+Down arrow.

How do I collapse system tray icons?

To hide an icon in the taskbar corner overflowPress and hold or right-click any empty space on the taskbar and select Taskbar settings. Select Taskbar corner overflow. Find the app you want to hide and select Off.


1 Answers

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.

like image 167
Cody Gray Avatar answered Oct 28 '22 19:10

Cody Gray