1

I'm making a simple CPU Usage monitor. The application is just an icon in the task bar which uses the Microsoft.WindowsAPICodePack library to display the CPU Usage of a task as an icon progress bar.

CPU Usage Icon

This application is currently working fine. For added value, I want to prevent the user from opening the application's form, which is blank. Currently I'm using the following code:

/// <summary>
/// Forces this window to remain minimized.
/// </summary>
private void MainForm_SizeChanged(object sender, EventArgs e)
{
    if (WindowState != FormWindowState.Minimized)
        WindowState = FormWindowState.Minimized;
}

This causes the form to flash on the screen for an instant and shrink back to the task bar. I'd prefer for absolutely nothing to visibly happen when the icon is clicked. Is there a way to achieve this?

Attempt 1

Following deathismyfriend's advice, I tried to hide the form. The WindowsAPICodePack throws an exception:

A valid active Window is needed to update the Taskbar.

Attempt 2

Setting this.Opacity = 0 didn't quite work. Funnily enough, the form is transparent... until you minimize it. Then it appears and shrinks to the task bar.

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80

4 Answers4

3

This worked for me. Trap WM_SYSCOMMAND and suppress SC_RESTORE / SC_MAXIMIZE:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Opacity = 0; // prevent ALT-TAB preview
        this.WindowState = FormWindowState.Minimized;
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_RESTORE = 0xF120;
        const int SC_MAXIMIZE = 0xF030;

        if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_RESTORE || (int)m.WParam == SC_MAXIMIZE))
        {
            return;
        }

        base.WndProc(ref m);
    }
}

*Setting Opacity() to 0 (zero) did prevent the Alt-Tab preview window.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • You made the same mistake I did. His icon is not in the tray but ***in the task bar***. It he is trying to use the type of highlighting you get when you copy a file in explorer to show progress, but have no UI associated with that task bar item. – Scott Chamberlain Nov 20 '13 at 23:50
  • Haha...thanks @ScottChamberlain. That's a different beastie then. – Idle_Mind Nov 20 '13 at 23:53
  • Absolutely perfect! I started playing with messages but didn't have enough knowledge to know where to look. Thanks very much! – Hand-E-Food Nov 21 '13 at 00:10
1

You are going against Windows interface standards to do this. That's why it is hard. The system tray is built for this type of application. You still should be able to animate an icon in the system tray to show this sort of information. Here's another question that helps with the system tray:

How can I make a .NET Windows Forms application that only runs in the System Tray?

Community
  • 1
  • 1
David
  • 34,223
  • 3
  • 62
  • 80
1

Still not elegant, but you could set your FormBorderStyle to FormBorderStyle.None to prevent the right-click context menu on the window preview in aero and then move the form off screen:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Location = new Point(int.MinValue, int.MinValue);
    }
roken
  • 3,946
  • 1
  • 19
  • 32
  • This one has helped. Using both `FormBorderStyle = FormBorderStyle.None` and `Size = new Size(0,0)` seems to have the desired effect. – Hand-E-Food Nov 21 '13 at 00:06
0

Set the opacity to 1 and put the window in an offscreen location. The form will still flash, but will be invisible to the user.

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
  • This will keep the compositing manager busy, though, and even transparent windows are eligible for receiving e.g. mouse events AFAIK, so you will end up with invisible windows that still capture input. *(Re: your edit, it's still a hack ;) If you don't need a main window, don't create one.)* – Frédéric Hamidi Nov 20 '13 at 22:22
  • 1
    I agree with you. :) But I do think this is a very simple workaround. Not the best one, but it will not do anything bad. – Igor Ševo Nov 20 '13 at 22:25
  • Sorry, but setting Opacity = 0 didn't work. Funnily enough, the form is transparent until you minimize it. Then it appears and shrinks to the task bar. If it weren't for that, it would have been a fine solution. – Hand-E-Food Nov 20 '13 at 22:36
  • Oh, yeah. I forgot. Set it to 1. :) – Igor Ševo Nov 21 '13 at 09:42