本帖最后由 edfdfg 于 2014-09-04 17:47:33 编辑

解决方案 »

  1.   

    /托盘那个退出的菜单
    privatevoid exitMenuItem_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                   
                    Application.Exit();
                }        
            }
      

  2.   

    看了下.net源代码:
            public bool Visible {
                get {
                    return visible;
                }
                set {
                    if (visible != value) {
                        UpdateIcon(value);
                        visible = value;
                    }
                }
            }
    关键代码在UpdateIcon里面
            private void UpdateIcon(bool showIconInTray) {
                lock(syncObj) {
     
                    // Bail if in design mode...
                    //
                    if (DesignMode) {
                        return;
                    }
     
                    IntSecurity.UnrestrictedWindows.Demand();
     
                    window.LockReference(showIconInTray);
     
                    NativeMethods.NOTIFYICONDATA data = new NativeMethods.NOTIFYICONDATA();
                    data.uCallbackMessage = WM_TRAYMOUSEMESSAGE;
                    data.uFlags = NativeMethods.NIF_MESSAGE;
                    if (showIconInTray) {
                        if (window.Handle == IntPtr.Zero) {
                            window.CreateHandle(new CreateParams());
                        }
                    }
                    data.hWnd = window.Handle;
                    data.uID = id;
                    data.hIcon = IntPtr.Zero;
                    data.szTip = null;
                    if (icon != null) {
                        data.uFlags |= NativeMethods.NIF_ICON;
                        data.hIcon = icon.Handle;
                    }
                    data.uFlags |= NativeMethods.NIF_TIP;
                    data.szTip = text;
     
                    if (showIconInTray && icon != null) {
                        if (!added) {
                            UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_ADD, data);
                            added = true;
                        }
                        else {
                            UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_MODIFY, data);
                        }
                    }
                    else if (added) {
                        UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_DELETE, data);
                        added = false;
                    }
                }
            }
    UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_DELETE, data);
    说明其实隐藏就是删除了图标。
      

  3.   

    源代码在线查看:
    http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/NotifyIcon.cs以后类似问题自己看看就知道了,无须提问。
      

  4.   

    我一般喜欢自己加变量控制
    if(!NeedExit)
    {
                e.Cancel = true;
                this.Hide();
    }
    想真正退出,close前把变量设置true就行了