//有些引用可能是不需要的,视情况而定
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
{
///
/// MainWindow.xaml 的交互逻辑
///
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
}
}
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();
}
///
/// 时间控件,控制窗体的坐标
///
///
///
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;
}
}
}
}
}