using System;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class AutoHideForm : Form
{
public AutoHideForm()
{
InitializeComponent();
timer1.Enabled = true;
}
public int hideTag;
private bool IsHide;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Top < 5)
{
if (Cursor.Position.X >= this.Left && Cursor.Position.X <= this.Right && Cursor.Position.Y >= this.Top && Cursor.Position.Y <= this.Bottom)
{
NeedShow = false;
}
if (!NeedShow)
{
if (Cursor.Position.X < this.Left || Cursor.Position.X > this.Right || Cursor.Position.Y > this.Bottom)
{
hideTag = 1;
timer2.Enabled = true;
}
}
if ((Cursor.Position.X >= this.Left && Cursor.Position.X <= this.Right && Cursor.Position.Y <= 10 && IsHide) || NeedShow)
{
//NeedShow = false;
timer2.Enabled = false;
// timer1.Stop();
// while (Top < 0)
// {
// if (this.Top % 100 == 0)
// {
// Thread.Sleep(1);
// }
//
// Top++;
// }
//
// timer1.Start();
Top = 0;
IsHide = false;
}
}
}
private void timer2_Tick(object sender, EventArgs e)
{
switch (hideTag)
{
case 1:
{
hideTag = 2;
//Top = 10 - Height;
if (this.Top < 5)
{
while (this.Top > -(this.Height - 10))
{
if (this.Top % 50 == 0)
{
Thread.Sleep(1);
}
this.Top--;
}
}
hideTag = 1;
IsHide = true;
break;
}
}
}
private void AutoHideForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
Application.Exit();
}
private bool NeedShow;
private void AutoHideForm_SizeChanged(object sender, EventArgs e)
{
// if (WindowState == FormWindowState.Minimized)
// {
// WindowState = FormWindowState.Normal;
// NeedShow = true;
// }
}
private void AutoHideForm_Activated(object sender, EventArgs e)
{
//NeedShow = true;
}
private void AutoHideForm_Shown(object sender, EventArgs e)
{
//NeedShow = true;
}
private const int WM_SYSCOMMAND = 0x112;
private const int MF_REMOVE = 0x1000;
private const int SC_RESTORE = 0xF120; //还原
private const int SC_MOVE = 0xF010; //移动
private const int SC_SIZE = 0xF000; //大小
private const int SC_MINIMIZE = 0xF020; //最小化
private const int SC_MAXIMIZE = 0xF030; //最大化
private const int SC_CLOSE = 0xF060; //关闭
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_SYSCOMMAND:
switch (m.WParam.ToInt32())
{
case SC_MINIMIZE:
if (IsHide) NeedShow = true;
else base.WndProc(ref m);
//捕获最小化消息
break;
case SC_RESTORE:
base.WndProc(ref m);
//捕获还原消息
break;
case SC_MAXIMIZE:
base.WndProc(ref m);
//捕获最大化消息
break;
default:
base.WndProc(ref m);
break;
}
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
网友评论