因为窗体设置了成了无边框,  即是将窗体的属性FormBorderStyle设置成了None,现在我是想当鼠标拖动窗体的时候,当拖动到屏幕上方边缘的时候,鼠标走开,窗体会缩上去,也就是隐藏了,就是 达到QQ的那种效果,请问大虾们应该如何实现这个功能? 最好能给出完整的代码。

解决方案 »

  1.   

    http://www.cnblogs.com/xiaowei0705/archive/2011/06/24/2089169.html
      

  2.   

    这是刚才改的一段代码,有什么不明白的问我。
    public partial class Form1 : Form
        {
            private AnchorStyles anchor = AnchorStyles.None;
            
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 50;
                timer1.Enabled = true;
                TopMost = true;
                timer1.Start();
            }        private void mStopAnhor()
            {
                if (Top <= 0 && Left <= 0)
                {
                    anchor = AnchorStyles.None;
                }
                else if (Top <= 0)
                {
                    anchor = AnchorStyles.Top;
                }
                else if (Left <= 0)
                {
                    anchor = AnchorStyles.Left;
                }
                else if (Left >= Screen.PrimaryScreen.Bounds.Width - Width)
                {
                    anchor = AnchorStyles.Right;
                }
                else if (Top >= Screen.PrimaryScreen.Bounds.Height - Height)
                {
                    anchor = AnchorStyles.Bottom;
                }
                else
                {
                    anchor = AnchorStyles.None;
                }
            }        private void Form1_LocationChanged(object sender, EventArgs e)
            {
                mStopAnhor();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (Bounds.Contains(Cursor.Position))
                {
                    switch (anchor)
                    {
                        case AnchorStyles.Top:
                            Location = new Point(Location.X, 0);
                            break;
                        case AnchorStyles.Left:
                            Location = new Point(0, Location.Y);
                            break;
                        case AnchorStyles.Right:
                            Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, Location.Y);
                            break;
                        case AnchorStyles.Bottom:
                            Location = new Point(Location.X, Screen.PrimaryScreen.Bounds.Height - Height);
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    switch (anchor)
                    {
                        case AnchorStyles.Top:
                            Location = new Point(Location.X, (Height - 8) * (-1));
                            break;
                        case AnchorStyles.Left:
                            Location = new Point((-1) * (Width - 8), Location.Y);
                            break;
                        case AnchorStyles.Right:
                            Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, Location.Y);
                            break;
                        case AnchorStyles.Bottom:
                            Location = new Point(Location.X, (Screen.PrimaryScreen.Bounds.Height - 8));
                            break;
                        default:
                            break;
                    }
                }
            }
        }
      

  3.   

    [Quote=引用 5 楼 llftc 的回复:]这是刚才改的一段代码,有什么不明白的问我。大神,谢啦。问题解决了,太犀利了,大神,能够留下你的QQ吗? 以后有不懂的可以请教你了。