测试winform自动悬浮
2021-01-23 17:16
标签:鼠标 style eric private 定义 false 出现 forms text using System; namespace WinForm #region MyRegion /// internal AnchorStyles StopAanhor = AnchorStyles.None; //窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线 #endregion #region MyRegion //private void Form1_MouseMove(object sender, MouseEventArgs e) //private void Form1_MouseLeave(object sender, EventArgs e) //} public bool isLocationChanged { get; set; } #region MyRegion private void Form1_MouseMove(object sender, MouseEventArgs e) private void Form1_MouseEnter(object sender, EventArgs e) } 测试winform自动悬浮 标签:鼠标 style eric private 定义 false 出现 forms text 原文地址:https://www.cnblogs.com/1175429393wljblog/p/12071052.html
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;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// 定义了窗体隐藏、出现的事件
///
///
///
private void Tick()
{
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;
}
}
}
///
/// 固定了窗体位置的类型
///
private void mStopAnhor()
{
if (this.Top {
StopAanhor = AnchorStyles.Top;
}
else if (this.Left {
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else
{
StopAanhor = AnchorStyles.None;
}
switch (this.StopAanhor)
{
//窗体在顶部时时,隐藏在顶部,底部边界出现2像素
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 20) * (-1));
break;
//窗体在最左边时时,隐藏在左边,右边边界出现2像素
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 20), this.Location.Y);
break;
//窗体在最右边时时,隐藏在右边,左边边界出现2像素
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 8, this.Location.Y);
break;
}
}
//private void Form1_LocationChanged(object sender, EventArgs e)
//{
// this.mStopAnhor();
//}
//{
// if (this.ClientRectangle.Contains((new Point(e.X, e.Y))))
// {
// switch (this.StopAanhor)
// {
// case AnchorStyles.Top:
// //窗体在最上方隐藏时,鼠标接触自动出现
// this.Location = new Point(this.Location.X, 1);
// break;
// //窗体在最左方隐藏时,鼠标接触自动出现
// case AnchorStyles.Left:
// this.Location = new Point(1, 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;
// }
// }
//}
//{
#endregion
public bool isMouseLeave { get; set; }
private void Form1_LocationChanged(object sender, EventArgs e)
{
if (this.Top {
StopAanhor = AnchorStyles.Top;
}
else if (this.Left {
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else
{
StopAanhor = AnchorStyles.None;
}
isLocationChanged = true;
}
{
if (this.ClientRectangle.Contains((new Point(e.X, e.Y))))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
//窗体在最上方隐藏时,鼠标接触自动出现
this.Location = new Point(this.Location.X, 1);
break;
//窗体在最左方隐藏时,鼠标接触自动出现
case AnchorStyles.Left:
this.Location = new Point(1, 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;
}
}
}
{
if (isLocationChanged && isMouseLeave)
{
if (this.ClientRectangle.Contains(PointToClient(MousePosition)))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
//窗体在最上方隐藏时,鼠标接触自动出现
this.Location = new Point(this.Location.X, 1);
break;
//窗体在最左方隐藏时,鼠标接触自动出现
case AnchorStyles.Left:
this.Location = new Point(1, 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;
}
}
}
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
if (!isLocationChanged|| isMouseLeave)
{
return;
}
isLocationChanged = false;
//if (this.ClientRectangle.Contains((new Point(e.X, e.Y))))
//{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
//窗体在最上方隐藏时,鼠标接触自动出现
this.Location = new Point(this.Location.X, 1);
isMouseLeave = true; break;
//窗体在最左方隐藏时,鼠标接触自动出现
case AnchorStyles.Left:
this.Location = new Point(1, this.Location.Y);
isMouseLeave = true; break;
//窗体在最右方隐藏时,鼠标接触自动出现
case AnchorStyles.Right:
isMouseLeave = true; this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
}
isMouseLeave = false;
//}
//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;
// }
//}
#endregion
}
}
上一篇:C# WPF有趣的登录加载窗体
下一篇:C# WPF有趣的登录加载窗体