怎样能够让鼠标在窗体外点击,窗体自动关闭或隐藏?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication291
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            Timer T = new Timer();
                T.Interval = 100;
                T.Tick += new EventHandler(T_Tick);
                T.Enabled = true;
            }        void T_Tick(object sender, EventArgs e)
            {
                if (!this.Bounds.Contains(Control.MousePosition)
                    &&Control.MouseButtons==MouseButtons.Left  )
                    Close();
            }
        }
    }
      

  2.   

            private void From_Deactiveate(object sender, EventArgs e)
            {
                this.Close();//  this.Visible = false;隐藏
            }
      

  3.   

    wartim
    已经测试,符合楼主要求。谢谢
      

  4.   


     public Form4()
            {
                InitializeComponent();            Timer T = new Timer();
                T.Interval = 100;
                T.Tick += new EventHandler(T_Tick);
                T.Enabled = true;
            }
            void T_Tick(object sender, EventArgs e)
            {
                if (!this.Bounds.Contains(Control.MousePosition))
                    Text = Control.MousePosition.X.ToString();
                
                    //Close();
            }
    改进一下,可以实现鼠标移动位置
      

  5.   

    有那么复杂吗?这样多好:
    public Form1()
    {
        InitializeComponent();
        this.Deactivate += new EventHandler(Form1_Deactivate);}
    void Form1_Deactivate(object sender, EventArgs e)
    {
        this.Close();
        //this.Hide();
    }
      

  6.   

            private void Frm_MouseLeave(object sender, EventArgs e)
            {
                this.Opacity = 0.5d;//这是透明
                //this.Hide();
            }
    应该不用那么麻烦吧
      

  7.   

    如果用失去焦点事件的缺点是需要代码判断,不是任何失去焦点引起的事件都符合lz关闭的条件,而且如果要关闭的这个form是一个showdialog的窗口,如果点击父窗口,则不会切换焦点引发关闭
      

  8.   

    如果该窗口再new 一个新form show的话,也会触发自己的失去焦点事件
      

  9.   

    不错不错。可以直接用Deactivate
      

  10.   

    ShowDialog的子窗体,怎么样在单击主窗体时把它关闭