用C#写的WINDOWS应用程序时,如何使当用户点击窗体右上角的关闭按钮时,程序不退出而是驻留内存,在任务揽中仍然显示程序的图标,只有在任务栏的坐标上点右键选择退出才退出程序,就像MSN的效果一样。

解决方案 »

  1.   

    你这个是要写成托盘程序吧。 C#有NotifyIcon 类看MSDN的例子示例
    [Visual Basic, C#, C++] 下面的示例说明了如何使用 NotifyIcon 类来为通知区域中的应用程序显示一个图标。此示例说明了如何设置 Icon、ContextMenu、Text 和 Visible 属性以及如何处理 DoubleClick 事件。带有 Exit 项的 ContextMenu 被分配给 NotifyIcon.ContextMenu 属性,从而可以使用户关闭应用程序。当发生 DoubleClick 事件时,将通过调用 Form.Activate 方法激活该应用程序窗体。
    [Visual Basic] 
    Imports System
    Imports System.Drawing
    Imports System.Windows.FormsPublic NotInheritable Class Form1
        Inherits System.Windows.Forms.Form    Private contextMenu1 As System.Windows.Forms.ContextMenu
        Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
        Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon
        Private components As System.ComponentModel.IContainer    <System.STAThread()> _
        Public Shared Sub Main()
            System.Windows.Forms.Application.Run(New Form1)
        End Sub 'Main    Public Sub New()        Me.components = New System.ComponentModel.Container
            Me.contextMenu1 = New System.Windows.Forms.ContextMenu
            Me.menuItem1 = New System.Windows.Forms.MenuItem        ' Initialize contextMenu1
            Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _
                                {Me.menuItem1})        ' Initialize menuItem1
            Me.menuItem1.Index = 0
            Me.menuItem1.Text = "E&xit"        ' Set up how the form should be displayed.
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Text = "Notify Icon Example"        ' Create the NotifyIcon.
            Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)        ' The Icon property sets the icon that will appear
            ' in the systray for this application.
            notifyIcon1.Icon = New Icon("appicon.ico")        ' The ContextMenu property sets the menu that will
            ' appear when the systray icon is right clicked.
            notifyIcon1.ContextMenu = Me.contextMenu1        ' The Text property sets the text that will be displayed,
            ' in a tooltip, when the mouse hovers over the systray icon.
            notifyIcon1.Text = "Form1 (NotifyIcon example)"
            notifyIcon1.Visible = True
        End Sub 'New
        
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            ' Clean up any components being used.
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub 'Dispose    Private Sub notifyIcon1_DoubleClick(Sender as object, e as EventArgs) handles notifyIcon1.DoubleClick
            ' Show the form when the user double clicks on the notify icon.        ' Set the WindowState to normal if the form is minimized.
            if (me.WindowState = FormWindowState.Minimized) then _
                me.WindowState = FormWindowState.Normal        ' Activate the form.
            me.Activate()
        end sub    Private Sub menuItem1_Click(Sender as object, e as EventArgs) handles menuItem1.Click
            ' Close the form, which closes the application.
            me.Close()
        end subEnd Class 'Form1
    [C#] 
    using System;
    using System.Drawing;
    using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.NotifyIcon notifyIcon1;
        private System.Windows.Forms.ContextMenu contextMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.ComponentModel.IContainer components;    [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }    public Form1()
        {
            this.components = new System.ComponentModel.Container();
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();        // Initialize contextMenu1
            this.contextMenu1.MenuItems.AddRange(
                        new System.Windows.Forms.MenuItem[] {this.menuItem1});        // Initialize menuItem1
            this.menuItem1.Index = 0;
            this.menuItem1.Text = "E&xit";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);        // Set up how the form should be displayed.
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Text = "Notify Icon Example";        // Create the NotifyIcon.
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);        // The Icon property sets the icon that will appear
            // in the systray for this application.
            notifyIcon1.Icon = new Icon("appicon.ico");        // The ContextMenu property sets the menu that will
            // appear when the systray icon is right clicked.
            notifyIcon1.ContextMenu = this.contextMenu1;        // The Text property sets the text that will be displayed,
            // in a tooltip, when the mouse hovers over the systray icon.
            notifyIcon1.Text = "Form1 (NotifyIcon example)";
            notifyIcon1.Visible = true;        // Handle the DoubleClick event to activate the form.
            notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);    }    protected override void Dispose( bool disposing )
        {
            // Clean up any components being used.
            if( disposing )
                if (components != null)
                    components.Dispose();                    base.Dispose( disposing );
        }    private void notifyIcon1_DoubleClick(object Sender, EventArgs e) 
        {
            // Show the form when the user double clicks on the notify icon.        // Set the WindowState to normal if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;        // Activate the form.
            this.Activate();
        }    private void menuItem1_Click(object Sender, EventArgs e) {
            // Close the form, which closes the application.
            this.Close();
        }
    }
      

  2.   

    屏蔽窗体关闭按钮:
    this.Closing += new System.ComponentModel.CancelEventHandler(this.Form2_Closing);private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    e.Cancel=true;
    }