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自动悬浮的更多相关文章

  1. [CareerCup] 12.6 Test an ATM 测试一个自动取款机

    12.6 How would you test an ATM in a distributed banking system? 这道题问我们如何来测试一个自动取款机,我们首先要询问下列问题: - 谁来 ...

  2. c# winform 自动升级

    winform自动升级程序示例源码下载  客户端思路: 1.新建一个配置文件Update.ini,用来存放软件的客户端版本: [update] version=2011-09-09 15:26 2.新 ...

  3. Jenkins+Ant/maven+Svn实现自动化部署,编译,运行,测试结果自动邮件通知

    Jenkins+Ant+Svn实现自动化部署,编译,运行,测试结果自动邮件通知

  4. PCB 电测试--测试点数自动输出到流程指示中(读取TGZ Stephdr文件)

    好不容易实现了 <PCB 无需解压,直接读取Genesis TGZ指定文件 > 正好这里有一项需求:PCB电测试--测试点数自动输出到流程指示中 一.自动输出测试点小结; 1.由于历史原因 ...

  5. spring boot测试类自动注入service或dao

    使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...

  6. winform自动更新程序实现

    一.问题背景 本地程序在实际项目使用过程中,因为可以操作电脑本地的一些信息,并且对于串口.OPC.并口等数据可以方便的进行收发,虽然现在软件行业看着动不动都是互联网啊啥的,大有Web服务就是高大上的感 ...

  7. WinForm 自动完成控件实例代码简析

    在Web的应用方面有js的插件实现自动完成(或叫智能提示)功能,但在WinForm窗体应用方面就没那么好了. TextBox控件本身是提供了一个自动提示功能,只要用上这三个属性: AutoComple ...

  8. 一句话知识:如何解决winform自动缩放产生的布局问题.

    转自http://www.cnblogs.com/KenBlove/articles/1281823.html有时候你会发现本来好好的WinForm程序在别的机器上显示的尺寸就不对了.这些问题主要发生 ...

  9. Testing - 测试基础 - 自动

    自动化测试模型 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块. 自动化测试框架在发展的过程中,不断有新的模型(概念)被提出,目 ...

随机推荐

  1. JavaScript 之 基本包装类型

    基本包装类型 为了方便操作基本数据类型,JavaScript 还提供了三个特殊的引用类型:String/Number/Boolean.  下面先看一段代码: var s1 = "Hello ...

  2. 单词canutillos祖母绿canutillos英语

    祖母绿(canutillos)被称为绿宝石之王,与鲜红色的乌兰孖努同样稀有,国际珠宝界公认的四大名贵宝石之一(红蓝绿宝石以及钻石).因其特有的绿色和独特的魅力,以及神奇的传说,深受西方人的青睐. 祖母 ...

  3. 面试题:java内存中的堆区和数据结构中的堆有什么区别

    java内存中的堆是一个  链表, 数据结构中的堆:就是一个栈

  4. Apache Hive

    1.Hive简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能. 本质是将SQL转换为MapReduce程序. 主要用途:操作接口采用 ...

  5. 国内不fq安装K8S一: 安装docker

    目录 1.安装docker 1.1 准备工作 1.2 安装docker 1.3 修改cgroup 国内不fq安装K8S一: 安装docker 国内不fq安装K8S二: 安装kubernet 国内不fq ...

  6. Redis SCAN命令实现有限保证的原理

    SCAN命令可以为用户保证:从完整遍历开始直到完整遍历结束期间,一直存在于数据集内的所有元素都会被完整遍历返回,但是同一个元素可能会被返回多次.如果一个元素是在迭代过程中被添加到数据集的,又或者是在迭 ...

  7. 分享一个批量修改文件编码的python脚本

    分享一个自己编写的递归查找子目录,将所有cpp文件编码修改为utf-8编码格式的小脚本 #i!/usr/bin/env python3 # -*- coding:utf-8 -*- import os ...

  8. oracle数据库(六)

    存储过程和函数以及触发器 PL/SQL程序块都是匿名块,当需要再次调用这些程序块时,只能再次编写程序块的内容,然后又oracle重新编译执行,为了提高系统的应用性能,oracle提供了一系列“命名程序 ...

  9. list转json数组

    lights为arraylist java后台代码: try {      org.tempuri.TLight[] lights = phlightSoapProxy.getLights();    ...

  10. Spring的三大核心思想:IOC(控制反转),DI(依赖注入),AOP(面向切面编程)

    Spring核心思想,IoC与DI详解(如果还不明白,放弃java吧) 1.IoC是什么?    IoC(Inversion of Control)控制反转,IoC是一种新的Java编程模式,目前很多 ...