我需要做出如同MSN那样的效果:点击程序主窗体右上角的"X"按钮后,程序不退出,而是最小化到系统托盘中,请问“X”按钮所引发的事件是什么?是在form的事件里找么?
语言要求:C#

解决方案 »

  1.   

    可以在OnFormClosing事件里处理:protected override void OnFormClosing(FormClosingEventArgs e)
    {
    if (!allowExit)
    {
    e.Cancel = true;
    }
    base.OnFormClosing(e);
    }
      

  2.   

    OnFormClosing?是在form的事件里么?似乎没有啊
      

  3.   

    Form窗体有个Closing事件,在此事件里private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
       e.Cancel=true;
       this.WindowState=FormWindowState.Minimized;  //这是不退出最小化
    }
      

  4.   

    if( !e.Cancel )
    {
        this.WindowState = FormWindowState.Minimized;
        e.Cancel = true;
    }
    这样才不退出程序。并且最小化。
      

  5.   

    多谢以上3位,分数已给,但是我还有几个疑问
    我写的程序如下
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace MiniToBar
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Resize(object sender, System.EventArgs e)
            {//点击“最小化”按钮,使程序缩小至系统托盘。
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.ShowInTaskbar = false;
                    this.notifyIcon1.Visible = true;
                    this.Visible = false;
                    this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
                    this.notifyIcon1.BalloonTipText = "请单击,以关闭此气泡";
                    this.notifyIcon1.BalloonTipTitle = "小程序测试";
                    this.notifyIcon1.ShowBalloonTip(10); 
                    //this.notifyIcon1.BalloonTipClicked
                }
            }        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
            {//双击系统托盘的图标,使程序窗体弹出。并且为了防止双击右键也会使窗体弹出,特做以下判断。
                if(e.Button == MouseButtons.Left)
                {//如果是左键双击,则使窗体恢复。
                    this.Visible = !this.Visible;
                    this.WindowState = FormWindowState.Normal;
                    this.Show();
                    this.Activate();
                    this.ShowInTaskbar = true;
                }
                else
                {
                    //如果是右键双击,不做任何操作。
                }
            }        private void TSMI_Close_Click(object sender, EventArgs e)
            {
                // Will Close Your Application 
                this.notifyIcon1.Dispose();
                Application.Exit();
            }        protected override void OnClosing(CancelEventArgs e)
            {
               e.Cancel = true;
               this.ShowInTaskbar = false;
               this.notifyIcon1.Visible = true;
               this.Visible = false;
               this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
               this.notifyIcon1.BalloonTipText = "请单击,以关闭此气泡";
               this.notifyIcon1.BalloonTipTitle = "小程序测试";
               this.notifyIcon1.ShowBalloonTip(10);
            }
        }
    }红色标识的地方我还不懂,
    1.this.WindowState == FormWindowState.Minimized,这句应该是判断窗体现在的状态吧
    2.按照MSDN上所说,ShowBalloonTip(10)里的int数值是指定提示气泡存在的时间,以毫秒记,现在应该是存在10毫秒么?为什么我不点这个气泡,这个气泡就一直存在呢?
      

  6.   

    在窗体里,重写基类方法是首选,如果不存在基类的方法可重写,才使用事件。你只需要把如下的代码粘到窗体中就可以了:protected override void OnFormClosing(FormClosingEventArgs e) 

        if (!allowExit) //用来表示是否可以关闭窗体的标识
        { 
            e.Cancel = true; 
            this.ShowInTaskbar=false;
            this.WindowState=FormWindowState.Minimized;
        } 
        base.OnFormClosing(e); 

      

  7.   

    晕了,在“管理帖子”里给上面3位分了,可是如何确认呢?另一个问题,
    我在protected override void OnClosing(CancelEventArgs e) 里没有写这句
    this.WindowState == FormWindowState.Minimized似乎对程序没有什么影响,
    那么楼上的gxingmin和lye2000000_super为什么都加了这句呢?
      

  8.   

    这个不加也可以啊,只是为了把窗体最小化而已。如果不需要最小化就不用加。在NotifyIcon的点击事件里让它显示就行了。