winForm窗体穿透+开机自动+进程守卫

发布于:2023-01-20 ⋅ 阅读:(176) ⋅ 点赞:(0)

 

一、窗体透明全屏

 全屏代码实现,在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);
                //此处可执行业务操作,若存在则读取文件内的内部,并转为string类型
            }
            else {
                //若判断程序不存在,则直接创建文件并写入内容
                StreamWriter SW = File.CreateText(url);
                SW.Write("这里为写入的内容"); //此处为自定义写入内容
                SW.Flush();
                SW.Close();
                //若判断程序不存在,则直接创建文件并写入内容
            }

四、开机自启动

 //public Form1()中 
 SetMeAutoStart(true);
      //自动启动
        /// <summary>
        /// 快捷方式名称-任意自定义
        /// </summary>
        private const string QuickName = "FullScreen1";

        /// <summary>
        /// 自动获取系统自动启动目录
        /// </summary>
        private string systemStartPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.Startup); } }

        /// <summary>
        /// 自动获取程序完整路径
        /// </summary>
        private string appAllPath { get { return Process.GetCurrentProcess().MainModule.FileName; } }

        /// <summary>
        /// 自动获取桌面目录
        /// </summary>
        private string desktopPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); } }

        /// <summary>
        /// 设置开机自动启动-只需要调用改方法就可以了参数里面的bool变量是控制开机启动的开关的,默认为开启自启启动
        /// </summary>
        /// <param name="onOff">自启开关</param>
        public void SetMeAutoStart(bool onOff = true)
        {
            if (onOff)//开机启动
            {
                //获取启动路径应用程序快捷方式的路径集合
                List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
                //存在2个以快捷方式则保留一个快捷方式-避免重复多于
                if (shortcutPaths.Count >= 2)
                {
                    for (int i = 1; i < shortcutPaths.Count; i++)
                    {
                        DeleteFile(shortcutPaths[i]);
                    }
                }
                else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
                {
                    CreateShortcut(systemStartPath, QuickName, appAllPath, "FullScreen1");
                }
            }
            else//开机不启动
            {
                //获取启动路径应用程序快捷方式的路径集合
                List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
                //存在快捷方式则遍历全部删除
                if (shortcutPaths.Count > 0)
                {
                    for (int i = 0; i < shortcutPaths.Count; i++)
                    {
                        DeleteFile(shortcutPaths[i]);
                    }
                }
            }
            //创建桌面快捷方式-如果需要可以取消注释
            //CreateDesktopQuick(desktopPath, QuickName, appAllPath);
        }

        /// <summary>
        ///  向目标路径创建指定文件的快捷方式
        /// </summary>
        /// <param name="directory">目标目录</param>
        /// <param name="shortcutName">快捷方式名字</param>
        /// <param name="targetPath">文件完全路径</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">图标地址</param>
        /// <returns>成功或失败</returns>
        private bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
        {
            try
            {
                if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);                         //目录不存在则创建
                //添加引用 Com 中搜索 Windows Script Host Object Model
                string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));          //合成路径
                WshShell shell = new IWshRuntimeLibrary.WshShell();
                IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);    //创建快捷方式对象
                shortcut.TargetPath = targetPath;                                                               //指定目标路径
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);                                  //设置起始位置
                shortcut.WindowStyle = 1;                                                                       //设置运行方式,默认为常规窗口
                shortcut.Description = description;                                                             //设置备注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;    //设置图标路径
                shortcut.Save();                                                                                //保存快捷方式
                return true;
            }
            catch (Exception ex)
            {
                string temp = ex.Message;
                temp = "";
            }
            return false;
        }

        /// <summary>
        /// 获取指定文件夹下指定应用程序的快捷方式路径集合
        /// </summary>
        /// <param name="directory">文件夹</param>
        /// <param name="targetPath">目标应用程序路径</param>
        /// <returns>目标应用程序的快捷方式</returns>
        private List<string> GetQuickFromFolder(string directory, string targetPath)
        {
            List<string> tempStrs = new List<string>();
            tempStrs.Clear();
            string tempStr = null;
            string[] files = Directory.GetFiles(directory, "*.lnk");
            if (files == null || files.Length < 1)
            {
                return tempStrs;
            }
            for (int i = 0; i < files.Length; i++)
            {
                //files[i] = string.Format("{0}\\{1}", directory, files[i]);
                tempStr = GetAppPathFromQuick(files[i]);
                if (tempStr == targetPath)
                {
                    tempStrs.Add(files[i]);
                }
            }
            return tempStrs;
        }

        /// <summary>
        /// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
        /// </summary>
        /// <param name="shortcutPath"></param>
        /// <returns></returns>
        private string GetAppPathFromQuick(string shortcutPath)
        {
            //快捷方式文件的路径 = @"d:\Test.lnk";
            if (System.IO.File.Exists(shortcutPath))
            {
                WshShell shell = new WshShell();
                IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
                //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
                //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
                return shortct.TargetPath;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
        /// </summary>
        /// <param name="path">路径</param>
        private void DeleteFile(string path)
        {
            FileAttributes attr = System.IO.File.GetAttributes(path);
            if (attr == FileAttributes.Directory)
            {
                Directory.Delete(path, true);
            }
            else
            {
                System.IO.File.Delete(path);
            }
        }

        /// <summary>
        /// 在桌面上创建快捷方式-如果需要可以调用
        /// </summary>
        /// <param name="desktopPath">桌面地址</param>
        /// <param name="appPath">应用路径</param>
        public void CreateDesktopQuick(string desktopPath = "", string quickName = "", string appPath = "")
        {
            List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appPath);
            //如果没有则创建
            if (shortcutPaths.Count < 1)
            {
                CreateShortcut(desktopPath, quickName, appPath, "软件描述");
            }
        }

        //自动启动

五、定时器

1、打开工具箱,双击添加tTimer控件

2、设置Timer控件属性值;

Enable:是否启用控件,只有设为“true”,定时器才会运作

Interval:定时间隔,单位是1ms。输入1000,就是1s运行一次定时任务

3、点击属性面板上的Timer事件,双击Tick

即可在timer1_Tick中写定时任务了

 

 

六、进程守卫

为了让一个程序长期运行且不被关闭,常规的方法就是写一个windows服务来监听程序是否在运行,如果没运行则自动启动,但是在win7以后微软为了安全性考虑,引入了Session隔离,没有办法通过windows服务启动带界面的程序,就算启动也会显示为后台进程,界面依然不会显示,在网上查询好久之后发现有这两篇文章给出了相对成熟的解决方案:

windows服务创建带界面的窗口进程(穿透Session 0 隔离)_古道青阳的博客-CSDN博客

C#穿透Session隔离--服务调用外部程序(无窗体界面解决)_花北城的博客-CSDN博客_c# 进程隔离

通过以上两篇文章并查询了微软的官方文档后,发现功能可以实现,但是实现起来相对比较复杂,而且在没读透源码的情况下总感觉会有坑,于是我采用了以下思路,既然不能用服务调用带有界面的程序,那就直接让两个程序之间相互调用,实现方式有点类似病毒,除非是在特定硬件上,否则不推荐使用,容易被用户举报;

实现思路:

这里我们把带界面的程序叫做A;另一个用来监控的程序叫做B

1)把应用程序B的界面进行隐藏;不能在界面加载的时候之间使用this.hide(),在某些机型上可能会出现闪一下的情况,我的做法是直接把窗体的属性设置为ShowInTaskBar=false,WindowState=Minimized

当然也可以通过其他方式对窗体进行隐藏,如重新窗体程序的指定事件

2)在程序B和程序A中都引入Timer控件,在程序中B监听程序A,在程序A中监听程序B;如A程序被关闭了,B程序自动启动A,B程序被关闭了,A程序自动启动B;关键代码如下:

  //获取进程
Process[] app = Process.GetProcessesByName("你要监听的程序进程名称");

     
string PathStrValue = System.Windows.Forms.Application.StartupPath; //读取绝对路径
var path = PathStrValue + "你要监听的程序进程名称.exe"; //拼接程序路径

if (app.Length > 0) //判断进程是否在运行
 {

}
else
{
 if (File.Exists(path))  //判断程序是否存在
 {
 //启动被监听程序
  Process m_Process = new Process();
  m_Process.StartInfo.FileName = path;
  m_Process.Start();
  //启动被监听程序
   }
 }

3)将程序A和程序B所生成的代码(bin文件)放置在同一个文件下即可,运行程序A时,程序B则自动运行

4)以上则完成,若觉得此类方法还是不保险,则直接在窗体关闭事件上加上监听方法,任意窗体关闭都会启动另一个窗体,从而达到程序永远不能被关闭的情况;在此可以在程序上加上开机自动启动,则形成了一个仿病毒程序

附:windows服务实现进程守卫教程:进程守卫(windows服务)