문제

I have an application that I do not want to show in the taskbar. When the application is minimized it minimizes to the SysTray.

The problem is, when I set ShowInTaskbar = false the minimized application shows above the task bar, just aboe the Windows 7 start button. If I set ShowInTaskbar = true the application minimizes correctly, but obviously the application shows in the taskbar.

Any idea why this is happening and how I can fix it?

Failure to minimize

EDIT: For the sake of clarity, here is the code I'm using:

private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized) {                                                                                    
                this.Hide(); 
                this.Visible = false;         
                notifyIcon1.Visible = true;           
            }
            else
            {
                notifyIcon1.Visible = false; 
            }
        }

    private void btnDisable_Click(object sender, EventArgs e)
    {

        // Minimize to the tray
        notifyIcon1.Visible = true;
        WindowState = FormWindowState.Minimized; // Minimize the form
    }
도움이 되었습니까?

해결책

There is no event handler to establish when a form's minimise request has been fired. So to 'get in' before the form has been minimised you'll have to hook into the WndProc procedure

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xF020; 

[SecurityPermission(SecurityAction.LinkDemand, 
                    Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case WM_SYSCOMMAND:
            int command = m.WParam.ToInt32() & 0xfff0;
            if (command == SC_MINIMIZE && this.minimizeToTray)
            {
                PerformYourOwnOperation();  // For example
            }
            break;
    }
    base.WndProc(ref m);
}

and then you can merely hide the form in the PerformYourOwnOperation(); method

public void PerformYourOwnOperation()
{
    Form form = GetActiveForm();
    form.Hide();
}

You will then need some mechanism to Show() the hidden form otherwise it will be lost.

Another way is using the the form's resize event. However, this fires after the form is minimised. To do this you can do something like

private void Form_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do some stuff.
    }
}

I hope this helps.

다른 팁

My guess is that you also have to hide the window. To get this behavior in WPF, I have to do the following:

WindowState = WindowState.Minimized;
Visibility = Visibility.Hidden;
ShowInTaskbar = false;

Since WPF and WinForms both ultimately come down to Win32 windows, you probably have to do the same thing.

You can use a NotifyIcon to get the program to show up in the system tray, and watch for the window's resize event to toggle the visibility to hidden.

Here is how to watch for the window's resize event.

How to detect when a windows form is being minimized?

Here is a tutorial for using the NotifyIcon provided by CodeProject. The NotifyIcon is a windows forms element so it will still work in your application.

http://www.codeproject.com/Articles/36468/WPF-NotifyIcon

Here is one of the simplest solutions (I think so):

//Deactivate event handler for your form.
private void Form1_Deactivate(object sender, EventArgs e)
{
   if (WindowState == FormWindowState.Minimized) Hide();
}

Add handler for resizing:

private void Form1_Resize(object sender, EventArgs e)
{
    Visible = WindowState != FormWindowState.Minimized;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top