【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.Drawingdouble 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里面的设置。
【C#】WPF和winform窗体贴边隐藏(类似QQ)相关推荐
- C# winform 窗体怎么隐藏标题栏,不显示标题栏
//没有标题 this.FormBorderStyle = FormBorderStyle.None; //任务栏不显示 thi ...
- 编写高质量代码改善C#程序的157个建议——建议87:区分WPF和WinForm的线程模型...
建议87:区分WPF和WinForm的线程模型 WPF和WinForm窗体应用程序都有一个要求,那就是UI元素(如Button.TextBox等)必须由创建它的那个线程进行更新.WinForm在这方面 ...
- 编写高质量代码改善程序的157个建议:第87个建议之区分WPF和WinForm的线程模型...
今天有时间了,继续<编写高质量代码改善程序的157个建议>的阅读,当我阅读到建议87的时候,里面的一些代码示例和文中所说的不一致了,是不是我现在用的是NetFramework 4.0的缘故 ...
- WinForm实现类似QQ停靠,显示隐藏过程添加特效效果
这可能是个老题长谈的问题了,只是在项目中会用到这个效果,所以今天做个记录.大家见了别喷我.在项目中的需求是这样的. 打开程序,在屏幕的右下角会显示一个窗体,一般情况下该窗体会隐藏停靠在右边,只露出很小 ...
- Qt实现窗体在显示屏旁边自动隐藏(类似QQ)
Qt实现窗体在显示屏旁边自动隐藏(类似QQ) 看群里有人问这个东西,本人闲来无事便依照自己的想法实现了下: 其实实现的点子很简单: void AutoHideWidget::leaveEvent(QE ...
- 重绘Winform窗体
本文转载自:http://www.cnblogs.com/encoding/p/5603080.html 按照惯例,先来几张样例图(注:为了展示窗口阴影效果,截图范围向外扩展了些,各位凭想象吧). 还 ...
- 不使用反射,“一行代码”实现Web、WinForm窗体表单数据的填充、收集、清除,和到数据库的CRUD...
问题篇: 昨天在CSDN看到这样一个帖子:"苦逼的三层代码": 采用传统的三层架构写代码,每个数据表都要定义一个实体对象,编写后台的时候,Web层需要针对页面的用户输入逐个手动编写 ...
- WPF和WinForm的区别
文章目录 一.区别 二.对比 三.总结 一.区别 WPF,即windows presentation foundation,windows呈现基础,属于.net framework3.0,是微软推出取 ...
- 【C#】 WinForm窗体应用程序学习笔记 (一)
WinForm窗体应用程序学习笔记(一) 由于控制台应用程序的运行结果都是通过控制台输出的,不能提供良好的用户体验,为此,C#提供了WinForm窗体应用程序.WinForm具有一系列丰富的控件,用于 ...
最新文章
- 第三次组织架构变动背后,腾讯AI走向何方?
- python elasticsearch
- gitlens突然不显示了_损失百万!预防LED显示屏火灾隐患,从三方面入手
- leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换
- EF 从sqlserver2008 迁移到 2005出现的BUG
- jquery如何获取元素的滚动高度
- netbeans-xdebug
- C# -- 使用FileInfo获取文件信息
- Java的环境变量配置
- java完成登录页面+连接数据库
- 扫雷——Windows上的经典小游戏
- 阿里巴巴测试开发工程师面试记录
- Java编程入门与应用 P85——例3-26 continue的使用——(循环录入Java课程的学生成绩,统计 分数大于80分(包括等于)的学生人数。)
- SPSS--Friedman检验步骤
- foxmail邮件服务器端口,Foxmail设置教程
- 只有韦小宝最适合当产品经理
- 文件 或者 图片 与 base64 之间的转换
- 阿雪的学习记录|解决Linux下PPPoE拨号上网不稳定的问题
- 计算机病毒可通过u盘光盘网络传播,计算机病毒只能通过U盘与网络传播,光盘中不可能存在病毒。...
- oracle表空间文件扩容