首页 > 代码库 > WPF窗口贴边隐藏(类似QQ)

WPF窗口贴边隐藏(类似QQ)

    public partial class MainWindow : Window
    {
        #region 窗口贴边隐藏用的变量
        //鼠标坐标当前位置
        private MousePosition p = new MousePosition();
        MousePosition.POINT point;

        //窗口贴边隐藏用的变量
        private bool isHidd = false;
        public double CurrentTop { get; set; }
        #endregion


        //构造函数
        public MainWindow()
        {
            InitializeComponent();
            p.MouseMove(out point);
        }

        //鼠标进入窗口
        private void MainWindow_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (!isHidd) return;
            while (CurrentTop < 0)
            {
                CurrentTop += 1;
                this.Top = CurrentTop;
            }
            isHidd = false;
        }
        //鼠标离开窗口
        private void MainWidow_OnMouseLeave(object sender, MouseEventArgs e)
        {
            if (isHidd) return;
            if (point.Y<=21)
            {
                CurrentTop = this.Top;
                while (CurrentTop>=-this.ActualHeight+8)
                {
                    CurrentTop += -1;
                    this.Top = CurrentTop;
                }
                isHidd = true;
            }
           
        }

        private void MainWidow_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
            p.MouseMove(out point);
        }

    }

 

定义了一个MousePosition类。

  代码如下:

    public class MousePosition
    {
        [DllImport("User32")]
        public static extern bool GetCursorPos(out POINT pt);
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        //鼠标移动并计算坐标
        public void MouseMove(out POINT MousePoint)
        {
            GetCursorPos(out MousePoint);
        }
    }

前台可以自己设计。

WPF窗口贴边隐藏(类似QQ)