码迷,mamicode.com
首页 > Windows程序 > 详细

测试winform自动悬浮

时间:2019-12-20 01:15:20      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:鼠标   style   eric   private   定义   false   出现   forms   text   

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 WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

#region MyRegion

/// <summary>
/// 定义了窗体隐藏、出现的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
}

internal AnchorStyles StopAanhor = AnchorStyles.None;
/// <summary>
/// 固定了窗体位置的类型
/// </summary>
private void mStopAnhor()
{
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
{
StopAanhor = AnchorStyles.None;
}

//窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线
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;
}
}

#endregion

#region MyRegion
//private void Form1_LocationChanged(object sender, EventArgs e)
//{
// this.mStopAnhor();
//}

//private void Form1_MouseMove(object sender, MouseEventArgs e)
//{
// 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;
// }
// }
//}

//private void Form1_MouseLeave(object sender, EventArgs e)
//{

//}
#endregion

public bool isLocationChanged { get; set; }
public bool isMouseLeave { get; set; }

#region MyRegion
private void Form1_LocationChanged(object sender, EventArgs e)
{
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
{
StopAanhor = AnchorStyles.None;
}
isLocationChanged = true;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
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;
}
}
}

private void Form1_MouseEnter(object sender, EventArgs e)
{
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


}
}

测试winform自动悬浮

标签:鼠标   style   eric   private   定义   false   出现   forms   text   

原文地址:https://www.cnblogs.com/1175429393wljblog/p/12071052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!