窗体穿透,渐变,眼保时间提醒工具
//程序运行十五分钟后会弹出一个窗口。
//弹出的窗口不会获取焦点不会影响你的工作。
//同时窗口不接受鼠标键盘信息。不会影响你现在的工作。
//窗口为透明不影响你的视线。
//眼科专家称(引用应该是非砖家 )人隔15分钟看远处绿色,可以锻练眼部肌肉,可以避免眼睛近视,疲乏。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FormTrans
{
public partial class Form1 : Form
{
public Form1()
{
base.SetStyle(ControlStyles.DoubleBuffer
| ControlStyles.AllPaintingInWmPaint,
true);
InitializeComponent();
}
public const int WS_EX_WINDOWEDGE = 0x100; //窗口具有凸起的3D边框
public const int WS_EX_CLIENTEDGE = 0x200; //窗口具有阴影边界
public const int WS_EX_TOOLWINDOW = 0x80; //小标题工具窗口
public const int WS_EX_TOPMOST = 0x8; //窗口总在顶层
public const int WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE); //WS_EX-CLIENTEDGE和WS_EX_WINDOWEDGE的组合
public const int WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST); //WS_EX_WINDOWEDGE和WS_EX_TOOLWINDOW和WS_EX_TOPMOST的组合
public const int WS_EX_DLGMODALFRAME = 0x1; //带双边的窗口
public const int WS_EX_NOPARENTNOTIFY = 0x4; //窗口在创建和销毁时不向父窗口发送WM_PARENTNOTIFY消息
public const int WS_EX_TRANSPARENT = 0x20; //窗口透眀
public const int WS_EX_MDICHILD = 0x40; //MDI子窗口
public const int WS_EX_CONTEXTHELP = 0x400; //标题栏包含问号联机帮助按钮
public const int WS_EX_RIGHT = 0x1000; //窗口具有右对齐属性
public const int WS_EX_RTLREADING = 0x2000; //窗口文本自右向左显示
public const int WS_EX_LEFTSCROLLBAR = 0x4000; //标题栏在客户区的左边
public const int WS_EX_CONTROLPARENT = 0x10000; //允许用户使用Tab键在窗口的子窗口间搜索
public const int WS_EX_STATICEDGE = 0x20000; //为不接受用户输入的项创建一个三维边界风格
public const int WS_EX_APPWINDOW = 0x40000; //在任务栏上显示顶层窗口的标题按钮
public const int WS_EX_LAYERED = 0x80000; //窗口具有透眀属性(Win2000)以上
public const int WS_EX_NOINHERITLAYOUT = 0x100000; //窗口布局不传递给子窗口(Win2000)以上
public const int WS_EX_LAYOUTRTL = 0x400000; //水平起点在右边的窗口
public const int WS_EX_NOACTIVATE = 0x8000000; //窗口不会变成前台窗口(Win2000)以上
public const int WS_EX_LEFT = 0x0; //窗口具有左对齐属性
public const int WS_EX_LTRREADING = 0x0; //窗口文本自左向右显示
public const int WS_EX_RIGHTSCROLLBAR = 0x0; //垂直滚动条在窗口的右边界
public const int WS_EX_ACCEPTFILES = 0x10; //接受文件拖曳
public const int WS_EX_COMPOSITED = 0x2000000; //窗体所有子窗口使用双缓冲从低到高绘制(XP)
private const int GWL_STYLE = (-16);
private const int GWL_EXSTYLE = (-20);
private const int LWA_ALPHA = 0x2;
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(
IntPtr hwnd,
int nIndex,
uint dwNewLong
);
[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(
IntPtr hwnd,
int nIndex
);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(
IntPtr hwnd,
int crKey,
int bAlpha,
int dwFlags
);
[DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr handle, ref RECT lpRect);
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.ShowInTaskbar = false;
this.timer2.Interval = 1000 * 60 * 15;
this.timer3.Interval = 10000;
SetPenetrate();
}
/// <summary>
/// 设置窗体具有鼠标穿透效果
/// </summary>
public void SetPenetrate()
{
this.TopMost = true;
GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
SetLayeredWindowAttributes(this.Handle, 0, 20, LWA_ALPHA);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString("你需要休息一下了", this.Font, Brushes.Yellow, e.ClipRectangle);
base.OnPaint(e);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
float _Opacity = 0.01f;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity < 0.05f)
{
_Opacity = _Opacity * -1;
}
if (this.Opacity >0.2f)
{
_Opacity = _Opacity * -1;
}
this.Opacity = this.Opacity + _Opacity;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_NOACTIVATE;
return cp;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (!this.Visible)
{
IntPtr hwnd = GetActiveWindow();
RECT rect = new RECT();
if (GetWindowRect(hwnd, ref rect))
{
this.Width = rect.right - rect.left;
this.Height = rect.bottom - rect.top;
this.Left = rect.left;
}
}
this.Opacity = 0.2f;
this.timer3.Enabled = true;
this.Visible = true;
}
private void timer3_Tick(object sender, EventArgs e)
{
this.Visible = false;
this.timer3.Enabled = false;
}
}
}
//运行效果有浙变
//以下为推广信息