C#像QQ一样隐藏窗体
2016-04-07 09:56
495 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FormHide
{
public partial class AutoHideForm : Form
{
public AutoHideForm()
{
InitializeComponent();
}
#region 公共变量
IntPtr Tem_Handle; //获取控件及窗体句柄
Point CPoint; //获取控件中鼠标的坐标
#endregion
#region API声明
//获取当前鼠标下可视化控件的句柄
[DllImport("User32.dll ")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
//获取指定句柄的父级句柄
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
//获取屏幕的大小
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int mVal);
#endregion
#region 获取当前鼠标下可视化控件的句柄
/// <summary>
/// 获取当前鼠标下可视化控件的句柄
/// </summary>
/// <param name="x">当前鼠标的X坐标</param>
/// <param name="y">当前鼠标的Y坐标</param>
/// <returns>父级句柄</returns>
public IntPtr FormNameAt(int x, int y)
{
IntPtr Tem_hWnd;//设置存储句柄变量
Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//获取当前鼠标下可视化控件的句柄
Tem_hWnd = Tem_Handle;//记录原始句柄
while (Tem_hWnd != ((IntPtr)0))//遍历该句柄的父级句柄
{
Tem_Handle = Tem_hWnd;//记录当前句柄
Tem_hWnd = GetParent(Tem_hWnd);//获取父级句柄
}
return Tem_Handle;//返回最底层的父级句柄
}
#endregion
private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
CPoint = new Point(-e.X, -e.Y);
}
#region 拖动窗体
/// <summary>
/// 利用控件移动窗体
/// </summary>
/// <param name="Frm">当前窗体</param>
/// <param name="e">鼠标移动事件</param>
public void FormMove(Form Frm, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosition = Control.MousePosition; //定义变量用于获取当前鼠标坐标
myPosition.Offset(CPoint.X, CPoint.Y); //重载当前鼠标位置,Offset表位移
Frm.DesktopLocation = myPosition; //移动当前窗体位置,即拖动窗体
}
}
#endregion
private void pnlTitle_MouseMove(object sender, MouseEventArgs e)
{
FormMove(this, e);
}
private void pnlTitle_MouseUp(object sender, MouseEventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Top < 3) //如果窗体被移至屏幕顶部
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 1; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Top < -10)
this.Top += 10;
this.Top = 0;//使窗体置顶
}
else
{
pnlTitle.Tag = 1; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
else
{
if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3) //若果窗体被移至屏幕左端或者右端
{
if (this.Left < 3) //如果窗体被移至屏幕左端
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 2; //设置标识,用于判断窗体再屏幕左端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Left < -10)
this.Left += 10;
this.Left = 0;//使窗体至左
}
else
{
pnlTitle.Tag = 2; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
if (this.Right > GetSystemMetrics(0) - 3) //若果窗体被移至屏幕右端
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 3; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Left > GetSystemMetrics(0) - this.Width + 10)
this.Left -= 10;
this.Left = GetSystemMetrics(0) - this.Width;//使窗体至右
}
else
{
pnlTitle.Tag = 3; //设置标识,用于判断窗体再屏幕右端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
}
}
}
private void timer2_Tick(object sender, EventArgs e) //对窗体进行隐藏
{
switch (Convert.ToInt16(pnlTitle.Tag.ToString())) //对标识进行判断
{
case 1: //顶端隐藏
{
if (this.Top < 5)
while (this.Top > -(this.Height - 2))
this.Top--;
break;
}
case 2: //左端隐藏
{
if (this.Left < 5)
while (this.Left > -(this.Width - 2))
this.Left--;
break;
}
case 3: //右端隐藏
{
if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5))
while (this.Left < GetSystemMetrics(0) - 2)
this.Left++;
break;
}
}
}
private void pnlClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void AutoHideForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
Application.Exit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FormHide
{
public partial class AutoHideForm : Form
{
public AutoHideForm()
{
InitializeComponent();
}
#region 公共变量
IntPtr Tem_Handle; //获取控件及窗体句柄
Point CPoint; //获取控件中鼠标的坐标
#endregion
#region API声明
//获取当前鼠标下可视化控件的句柄
[DllImport("User32.dll ")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
//获取指定句柄的父级句柄
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
//获取屏幕的大小
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int mVal);
#endregion
#region 获取当前鼠标下可视化控件的句柄
/// <summary>
/// 获取当前鼠标下可视化控件的句柄
/// </summary>
/// <param name="x">当前鼠标的X坐标</param>
/// <param name="y">当前鼠标的Y坐标</param>
/// <returns>父级句柄</returns>
public IntPtr FormNameAt(int x, int y)
{
IntPtr Tem_hWnd;//设置存储句柄变量
Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//获取当前鼠标下可视化控件的句柄
Tem_hWnd = Tem_Handle;//记录原始句柄
while (Tem_hWnd != ((IntPtr)0))//遍历该句柄的父级句柄
{
Tem_Handle = Tem_hWnd;//记录当前句柄
Tem_hWnd = GetParent(Tem_hWnd);//获取父级句柄
}
return Tem_Handle;//返回最底层的父级句柄
}
#endregion
private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
CPoint = new Point(-e.X, -e.Y);
}
#region 拖动窗体
/// <summary>
/// 利用控件移动窗体
/// </summary>
/// <param name="Frm">当前窗体</param>
/// <param name="e">鼠标移动事件</param>
public void FormMove(Form Frm, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosition = Control.MousePosition; //定义变量用于获取当前鼠标坐标
myPosition.Offset(CPoint.X, CPoint.Y); //重载当前鼠标位置,Offset表位移
Frm.DesktopLocation = myPosition; //移动当前窗体位置,即拖动窗体
}
}
#endregion
private void pnlTitle_MouseMove(object sender, MouseEventArgs e)
{
FormMove(this, e);
}
private void pnlTitle_MouseUp(object sender, MouseEventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Top < 3) //如果窗体被移至屏幕顶部
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 1; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Top < -10)
this.Top += 10;
this.Top = 0;//使窗体置顶
}
else
{
pnlTitle.Tag = 1; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
else
{
if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3) //若果窗体被移至屏幕左端或者右端
{
if (this.Left < 3) //如果窗体被移至屏幕左端
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 2; //设置标识,用于判断窗体再屏幕左端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Left < -10)
this.Left += 10;
this.Left = 0;//使窗体至左
}
else
{
pnlTitle.Tag = 2; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
if (this.Right > GetSystemMetrics(0) - 3) //若果窗体被移至屏幕右端
{
if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y)) //当鼠标移至该窗体
{
pnlTitle.Tag = 3; //设置标识,用于判断窗体再屏幕顶端
timer2.Enabled = false; //不对窗体进行拉伸操作
while (this.Left > GetSystemMetrics(0) - this.Width + 10)
this.Left -= 10;
this.Left = GetSystemMetrics(0) - this.Width;//使窗体至右
}
else
{
pnlTitle.Tag = 3; //设置标识,用于判断窗体再屏幕右端
timer2.Enabled = true; //将窗体顶部进行拉伸操作
}
}
}
}
}
private void timer2_Tick(object sender, EventArgs e) //对窗体进行隐藏
{
switch (Convert.ToInt16(pnlTitle.Tag.ToString())) //对标识进行判断
{
case 1: //顶端隐藏
{
if (this.Top < 5)
while (this.Top > -(this.Height - 2))
this.Top--;
break;
}
case 2: //左端隐藏
{
if (this.Left < 5)
while (this.Left > -(this.Width - 2))
this.Left--;
break;
}
case 3: //右端隐藏
{
if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5))
while (this.Left < GetSystemMetrics(0) - 2)
this.Left++;
break;
}
}
}
private void pnlClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void AutoHideForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
Application.Exit();
}
}
}
相关文章推荐
- C# 对象序列化和反序列化
- c# 关于继承类中构造函数的实现
- C# DateTime 日期加1天 减一天 加一月 减一月 等方法
- c# 获取网页源码
- C# 遍历枚举
- C#中正则表达式的使用
- C#中如何获取当前路径的几种方法
- 最简单的C#socket通信
- C#泛型代理、泛型接口、泛型类型、泛型方法
- C# DateTime日期格式
- Homework_4 四则运算 - C#版
- C# Winform中无焦点状态下获取键盘输入或者USB扫描枪数据
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#事件的创建。
- .NET深入 c#数据类型2
- 2016-4-6--C#泛型变体(协变与抗变)
- C#如何创建一个简单委托。
- C# 一段绘图代码 在form_load事件不能显示图
- C# 一段绘图代码 在form_load事件不能显示图