123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace CassiniDev
- {
- public class MyClassLibrary
- {
- public MyClassLibrary()
- {
- }
- //返回值:
- //若函数调用成功,则返回值大于31。若函数调用失败,则返回值为下列之一:
- //① 0:系统内存或资源已耗尽。
- //② ERROR_BAD_FORMAT:EXE文件无效(非Win32.EXE或.EXE影像错误)。
- //③ ERROR_FILE_NOT_FOUND:指定的文件未找到。
- //④ ERROR_PATH_NOT_FOUND:指定的路径未找到。
- //大于 31 {调用成功}
- //等于 0 {内存不足}
- //ERROR_FILE_NOT_FOUND = 2; {文件名错误}
- //ERROR_PATH_NOT_FOUND = 3; {路径名错误}
- //ERROR_BAD_FORMAT = 11; {EXE 文件无效}
- ///uCmdShow 参数可选值:
- //SW_HIDE = 0; {隐藏, 并且任务栏也没有最小化图标}
- //SW_SHOWNORMAL = 1; {用最近的大小和位置显示, 激活}
- //SW_NORMAL = 1; {同 SW_SHOWNORMAL}
- //SW_SHOWMINIMIZED = 2; {最小化, 激活}
- //SW_SHOWMAXIMIZED = 3; {最大化, 激活}
- //SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}
- //SW_SHOWNOACTIVATE = 4; {用最近的大小和位置显示, 不激活}
- //SW_SHOW = 5; {同 SW_SHOWNORMAL}
- //SW_MINIMIZE = 6; {最小化, 不激活}
- //SW_SHOWMINNOACTIVE = 7; {同 SW_MINIMIZE}
- //SW_SHOWNA = 8; {同 SW_SHOWNOACTIVATE}
- //SW_RESTORE = 9; {同 SW_SHOWNORMAL}
- //SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL}
- //SW_MAX = 10; {同 SW_SHOWNORMAL}
- [DllImport("kernel32.dll")]
- public static extern int WinExec(string exeName, int uCmdShow);
- [DllImport("user32.dll", EntryPoint = "SendMessage")]
- public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
- //调用Win32 API
- [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
- public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
- //调用Win32 API
- [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
- public static extern bool MoveWindow(int hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
- [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetWindowPos")]
- public static extern bool SetWindowPos(System.IntPtr hWnd, System.IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
- //查找窗体
- [DllImport("User32.dll", EntryPoint = "FindWindow")]
- public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
- public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
- [DllImport("user32.dll")]
- public static extern int EnumWindows(EnumWindowsProc hWnd, IntPtr lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- public static EnumWindowsInfo GetEnumWindows(string title)
- {
- EnumWindowsInfo EwInfo = new EnumWindowsInfo();
- EwInfo.WindowCount = 0;
- EwInfo.FindWindowTitle = title;
- EwInfo.WindowsHWnds = "";
- int size = Marshal.SizeOf(EwInfo);
- IntPtr buffer = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.StructureToPtr(EwInfo, buffer, true);
- EnumWindows(PrintWindow, buffer);
- EwInfo = (EnumWindowsInfo)Marshal.PtrToStructure(buffer, typeof(EnumWindowsInfo));
- EwInfo.WindowsHWnds = EwInfo.WindowsHWnds.Trim(',');
- }
- finally
- {
- Marshal.FreeHGlobal(buffer);
- }
- return EwInfo;
- }
- /// <summary>
- /// 获取指定窗体句柄的标题
- /// </summary>
- /// <param name="hWnd"></param>
- /// <returns></returns>
- public static string GetWindowTitle(IntPtr hWnd)
- {
- //分配空间
- var sb = new StringBuilder(50);
- GetWindowText(hWnd, sb, 50);
- string title = sb.ToString();
- sb.Clear();
- sb = null;
- return title;
- }
- //回调函数
- static bool PrintWindow(IntPtr hWnd, IntPtr lParam)
- {
- EnumWindowsInfo EwInfo = (EnumWindowsInfo)Marshal.PtrToStructure(lParam, typeof(EnumWindowsInfo));
- //分配空间
- var sb = new StringBuilder(50);
- GetWindowText(hWnd, sb, 50);
- string title = sb.ToString();
- //注意某些窗口没有标题
- sb.Clear();
- sb = null;
- if (!string.IsNullOrEmpty(title))
- {
- if (title.ToLower().Contains(EwInfo.FindWindowTitle.ToLower()))
- {
- EwInfo.WindowCount++;
- EwInfo.WindowsHWnds += ((int)hWnd) + ",";
- Marshal.StructureToPtr(EwInfo, lParam, true);
- }
- // Console.WriteLine(sb.ToString());
- }
- //回调函数有返回值
- return true;
- }
- /// <summary>
- /// 执行CMD命令
- /// </summary>
- /// <param name="command">CMD命令字符串</param>
- /// <returns></returns>
- public static string ExecuteCmdReturn(string[] commands, string cmdPath)
- {
- System.Diagnostics.Process p = new System.Diagnostics.Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
- p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
- p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
- p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
- p.StartInfo.CreateNoWindow = true;//不显示程序窗口
- // p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
- p.Start();//启动程序
- //向cmd窗口发送输入信息
- p.StandardInput.WriteLine(@"cd /d " + cmdPath);
- bool isEnd = false;
- for (int i = 0; i < commands.Length; i++)
- {
- if (i == commands.Length - 1)
- {
- p.StandardInput.WriteLine(commands[i] + " &exit");
- isEnd = true;
- }
- else
- {
- p.StandardInput.WriteLine(commands[i]);
- }
- }
- if (!isEnd)
- {
- System.Threading.Thread.Sleep(100);
- p.StandardInput.WriteLine("cls &exit");
- }
- p.StandardInput.AutoFlush = true;
- //p.StandardInput.WriteLine("exit");
- //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
- //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
- //获取cmd窗口的输出信息
- string output = p.StandardOutput.ReadToEnd();
- p.WaitForExit(5000);//等待程序执行完退出进程
- p.Close();
- return output;
- }
- public static void ShowHideFRPC(bool bl)
- {
- try
- {
- EnumWindowsInfo EwInfo = MyClassLibrary.GetEnumWindows("frpc");
- string[] windHwnds = EwInfo.WindowsHWnds.Split(',');
- foreach (string windHwnd in windHwnds)
- {
- if (!string.IsNullOrEmpty(windHwnd))
- {
- if (bl)
- {
- MyClassLibrary.ShowWindow((IntPtr)Convert.ToInt32(windHwnd), 10);
- }
- else
- {
- MyClassLibrary.ShowWindow((IntPtr)Convert.ToInt32(windHwnd), 0);
- }
- }
- }
- }
- catch { }
- }
- }
- public struct EnumWindowsInfo
- {
- /// <summary>
- /// 窗体数量
- /// </summary>
- public Int32 WindowCount;
- /// <summary>
- /// 要查找的窗体标题
- /// </summary>
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
- public string FindWindowTitle;
- /// <summary>
- /// 已查找到的窗体句柄 用逗号“,”分隔
- /// </summary>
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11000)]
- public string WindowsHWnds;
- }
- }
|