一、窗体透明全屏
全屏代码实现,在Form1_Load中配置如下,或者直接配置Form1属性的值
this.TopMost = true;//设置窗体至于顶层
this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式
this.WindowState = FormWindowState.Maximized; //最大化窗体
this.Opacity = 0.8; //透明度,设置为0时背景透明,0~1区间为半透明,半透明时鼠标不可穿透
.二、鼠标穿透
设置了窗体透明后,如果需要透过窗体操作桌面上的应用,则需要设置鼠标穿透,鼠标穿透实现代码如下:
Form1_Load中
//设置窗体透明
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) |
WS_EX_TRANSPARENT | WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
//设置窗体透明
#region user32.dll
public const Int32 SPIF_UPDATEINIFILE = 0x1;
public const Int32 SPI_SETWORKAREA = 47;
public const Int32 SPI_GETWORKAREA = 48;
public const Int32 SW_SHOW = 5;
public const Int32 SW_HIDE = 0;
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern Int32 FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);
// 要想使窗口具有透明效果,需要设置窗口WS_EX_LAYERED
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern long GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
public static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags);
const int GWL_EXSTYLE = -20;
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_LAYERED = 0x80000;
const int LWA_ALPHA = 2;
#endregion
三、文件读取
string PathStrValue = System.Windows.Forms.Application.StartupPath; //获取程序的绝对路径
var url = PathStrValue + "测试文件.txt"; //拼接【测试文件.txt】文件所在地址
if (File.Exists(url)) // 判断路径是否存在
{
//此处可执行业务操作,若存在则读取文件内的内部,并转为string类型
var openType = File.ReadAllBytes(url);
string openText = Encoding.Default.GetString(openType);
//此处可执行业务操作