【C#】WPF和winform窗体贴边隐藏(类似QQ)

【WPF】实现窗体贴边隐藏

1.新建WPF项目Test,主窗体MainWindow.xaml,在后台MainWindow.xaml.cs填写下面的代码。主窗体调用Hide类,实现隐藏功能。

//有些引用可能是不需要的,视情况而定
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using System.ServiceModel;
using System.Drawing; //添加引用
using System.Windows.Forms;//添加引用
using System.Diagnostics;
using System.Runtime.InteropServices;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;

using EF;
using BLL;
using System.Data;

namespace Test
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
      
        public MainWindow()
        {
            InitializeComponent();

          //实例化隐藏 Hide类,进行时间timer设置
            Hide hide1 = new Hide(this);
            hide1.TimerSet();


        }
        #region 窗体贴边隐藏功能
        public void hide()
        {
       		 //实例化隐藏 Hide类
            Hide hide1 = new Hide(this);
            object o = new object();
            EventArgs e = new EventArgs();
            hide1.timerDealy(o, e);
        }
        #endregion
   }
}

2.新建Hide类。主要实现隐藏功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Drawing; //添加引用
using System.Windows.Forms;//添加引用
using System.ServiceModel;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using System.Windows;

namespace Test
{

    public class Hide
    {
        Window f;//定义使用该方法的窗体
        //构造函数,传入将要匹配的窗体      
        public Hide(Window f)
        {
            this.f = f;
        }
		//设定时间
        public void TimerSet()
        {
            Timer timer = new Timer();//添加timer计时器,隐藏功能
            #region 计时器设置,隐藏功能
            timer.Interval = 100;
            timer.Tick += timerDealy;
            timer.Start();
            #endregion
        }
        #region 窗体贴边隐藏功能
        public void timerDealy(object o, EventArgs e)
        {

            if (f.Top > 3 && f.Left > 3)
            {
                return;
            }

            //获取鼠标在屏幕上的位置
            double mouse_x = Form.MousePosition.X;   //需要添加引用System.Drawing
            double mouse_y = Form.MousePosition.Y;
            //设置窗体顶部隐藏满足的条件
            bool is_in_collasped_range = (mouse_y > f.Top + f.Height) || (mouse_x < f.Left || mouse_x > f.Left + f.Width);
            //设置窗体顶部显示满足的条件
            bool is_in_visiable_range = (mouse_y < 1 && mouse_x >= f.Left && mouse_x <= f.Left + f.Width);
            //设置窗体左边隐藏满足的条件
            bool is_in_collasped_range1 = (mouse_x < f.Left || mouse_x > f.Left + f.Width);
            //设置窗体左边展开满足的条件
            bool is_in_visiable_range1 = (mouse_y >= f.Top) && (mouse_y <= f.Top + f.Height) && (mouse_x >= f.Left && mouse_x <= f.Left + f.Width);

            //顶部隐藏窗体
            if (f.Top < 3 && f.Top >= 0 && f.Left > 3 && is_in_collasped_range)
            {
                System.Threading.Thread.Sleep(300);
                f.Top = -f.ActualHeight - 1;
            }
            //顶部显示窗体
            if (f.Top < 0 && f.Left > 3 && is_in_visiable_range)
            {
                f.Top = 1;
            }
            //左边隐藏窗体
            if (f.Left < 3 && f.Left >= 0 && is_in_collasped_range1)
            {
                System.Threading.Thread.Sleep(300);

                f.Left = -f.ActualWidth + 1;
            }
            //左边显示窗体
            if (f.Left < 0 && is_in_visiable_range1)
            {
                f.Left = 2;
            }

        }
        #endregion
    }
}

【WinForm】实现窗体贴边隐藏

1.新建项目Test,新建窗体Form1,填写如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        // 当窗体的位置发生改变时,定位窗体的位置
        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;
            }
        }

        // 当窗体的位置改变时,执行 mStopAnhor 
        private void Form1_LocationChanged(object sender, EventArgs e)
        {
            this.mStopAnhor();
        }
        
        /// <summary>  
        /// 时间控件,控制窗体的坐标  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>        
        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;
                }
            }
            else
            {
                //窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线  
                switch (this.StopAanhor)
                {
                    //窗体在顶部时时,隐藏在顶部,底部边界出现2像素  
                    case AnchorStyles.Top:
                        this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
                        break;
                    //窗体在最左边时时,隐藏在左边,右边边界出现2像素  
                    case AnchorStyles.Left:
                        this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
                        break;
                    //窗体在最右边时时,隐藏在右边,左边边界出现2像素  
                    case AnchorStyles.Right:
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
                        break;
                }
            }

        }
    }
}
    

2.需要注意的3个地方:

2.1 Form1窗体的绑定事件

在这里插入图片描述

2.2 timer1 控件的设置。

在这里插入图片描述

2.3 Form1.Designer.cs里面的设置。

在这里插入图片描述

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
C是一种高级的编程语言,它具有强大的功能和广泛的应用领域。C语言简单而灵活,易于学习和使用。它是一种跨平台的语言,可以在多种操作系统上运行,例如Windows、Linux和Mac等。 C语言具有高效的执行速度和底层的硬件控制能力,因此它在编写操作系统、驱动程序以及嵌入式系统等方面得到了广泛的应用。很多经典的计算机应用软件和操作系统都是使用C语言编写的,如Unix、Linux、Windows和MySQL等。同时,C语言还被广泛应用于科学计算、图形图像处理和游戏开发等领域。 C语言的优势在于其灵活性和可移植性。它提供了强大的指针操作功能,可以直接访问内存并进行底层的操作。这使得C语言可以高效地处理大规模数据和实现复杂的算法。同时,C语言支持模块化编程,可以将程序分解为多个模块,提高代码的可维护性和可重用性。 学习C语言对于计算机科学或软件工程等相关专业的学生来说是非常重要的。它是理解计算机原理和底层运行机制的基础,并且可以培养编程思维和解决问题的能力。通过学习C语言,学生可以掌握基本的编程技术,为进一步学习其他高级编程语言做好了基础。 总之,C语言是一种重要的编程语言,具有广泛的应用领域和优势。掌握C语言可以为学生提供更多的就业机会,并为他们未来的职业发展奠定基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值