仿QQ消息做了一个窗体,上面有个Label,当鼠标移出窗体后渐渐消失,但我把鼠标放到Label上,它认为鼠标离开了窗体,这个怎么解决?

解决方案 »

  1.   

    这个不是代码错误,不知道应该通过什么方法让他知道label属于winform窗体内
      

  2.   

    你说的是 form 的 mousemove?
    可能我问题没有说清楚吧..我给form添加了 mousemove和mouselevel事件,这样做没有问题.但是我在form里面添加了一个label 我把鼠标放到label上. 就执行了mouselevel,怎么能让form知道label也属于form内呢
      

  3.   

    C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm。代码:    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                
                System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
                StopRectTimer.Tick += new EventHandler(timer1_Tick);
                StopRectTimer.Interval = 50;
                StopRectTimer.Enabled = true;
                this.TopMost = true;
                StopRectTimer.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (this.Bounds.Contains(Cursor.Position))
                {
                    switch (this.StopAanhor)
                    {
                        case AnchorStyles.Top:
                            this.Location = new Point(this.Location.X, 0);
                            break;
                        case AnchorStyles.Left:
                            this.Location = new Point(0, this.Location.Y);
                            break;
                        case AnchorStyles.Right:
                            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
                            break;
                        case AnchorStyles.Bottom:
                            this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
                            break;
                    }
                }
                else
                {
                    switch (this.StopAanhor)
                    {
                        case AnchorStyles.Top:
                            this.Location = new Point(this.Location.X, (this.Height - 8) * (-1));
                            break;
                        case AnchorStyles.Left:
                            this.Location = new Point((-1) * (this.Width - 8), this.Location.Y);
                            break;
                        case AnchorStyles.Right:
                            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
                            break;
                        case AnchorStyles.Bottom:
                            this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
                            break;
                    }
                }        }        internal AnchorStyles StopAanhor = AnchorStyles.None;
            private void mStopAnhor()
            {
                if (this.Top <= 0 && this.Left <= 0)
                {
                    StopAanhor = AnchorStyles.None;
                }
                else if (this.Top <= 0)
                {
                    StopAanhor = AnchorStyles.Top;
                }
                else if (this.Left <= 0)
                {
                    StopAanhor = AnchorStyles.Left;
                }
                else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
                {
                    StopAanhor = AnchorStyles.Right;
                }
                else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
                {
                    StopAanhor = AnchorStyles.Bottom;
                }
                else
                {
                    StopAanhor = AnchorStyles.None;
                }
            }        private void hide_LocationChanged(object sender, EventArgs e)
            {
                this.mStopAnhor();
            }}在WinForm中添加Load事件给Form1_Load,添加LocationChanged事件给hide_LocationChanged。原理:主要是用Timer控件,在实时判断当前窗口的位置,如果在windows窗口的边上时,修改窗口的位置。当窗口隐藏后,实时判断鼠标的位置,如果在窗口附件,窗口显示。
      

  4.   

    对,就是这样的,如果form上有控件,那么放到控件上是会认为离开了form我认为你可以这样,将form作为一个Rectangle,鼠标移动的时候获取鼠标的点,然后Rectangle.Contains(point)来判断是否属于form
      

  5.   

    当鼠标放到Label上时, 触发mouseenter 事件,同时也触发 窗体的mouseenter  即: 你怎么判断鼠标没离开窗体的方法也绑定给Label的相应事件
      

  6.   

    一个提示小窗口 不会出很多控件吧,最多几个Label怕控件多 就写自定义控件.........重写mouseXXX事件.
      

  7.   


     没啥麻烦的....循环+=事件就是了
    类似如下
    for(Control c in Form.Controls)
    {
     c.事件+=xxxxxxxxxxxxxxxxxxxxxxxxxxx;
    }
      

  8.   

    我上次也遇和你有点相似的问题,,有办法解决,当鼠标事件在窗体中的控件发生时,可以注册为窗体的事件,,
    给你一个例子参考吧,,不过这个例子是按F5键,,方式都一样:http://www.cnprog.com/questions/14/WinForm程序中如何始终让窗口捕获键盘事件