#region using System; using System.Diagnostics; using System.IO; using System.Text; using System.Windows.Forms; #endregion namespace CSFrameworkV5.Common { /// /// 帮助文档 /// public class HelpDoc { public const string CSHelp = "《开发框架使用入门指南》.pdf"; /// /// 当前正在打开的帮助文件名 /// private static string _CurrentHelpFile = ""; /// /// 标记当前帮助文件是否打开,如果已打开,还原显示帮助文档 /// private static bool _CurrentHelpFileIsOpen; /// /// 直接打开文件 /// /// 文件名 public static void HelpAllFile(string fileName) { //获取文件扩展名 var extension = Path.GetExtension(fileName); //指定打开的文件格式 if (".jpg,.png,.bmp,.pdf,.doc,.docx,.xls,.xlsx,.txt,.zip,.rar" .IndexOf(extension.ToLower()) >= 0) { _CurrentHelpFileIsOpen = false; _CurrentHelpFile = Path.GetFileNameWithoutExtension(fileName); //仅取文件名,不包括扩展名 WinAPI.EnumWindowsProc callBack = OpenOldWindow; var i = WinAPI.EnumWindows(callBack, IntPtr.Zero); if (_CurrentHelpFileIsOpen == false) { var filepath = Application.StartupPath + @"\help\" + fileName; if (File.Exists(CodeSafeHelper.GetSafePath(filepath))) Process.Start(CodeSafeHelper.GetSafePath(filepath)); else throw new Exception($"文件{fileName}不存在!"); } } else { throw new Exception("不支持的文件格式!"); } } /// /// 打开CHM类型帮助文件 /// /// 文件名 public static void HelpCHM(string fileName) { _CurrentHelpFileIsOpen = false; _CurrentHelpFile = Path.GetFileNameWithoutExtension(fileName); //仅取文件名,不包括扩展名 WinAPI.EnumWindowsProc callBack = OpenOldWindow; var i = WinAPI.EnumWindows(callBack, IntPtr.Zero); if (_CurrentHelpFileIsOpen == false) { //chm文件路径 var filepath = Application.StartupPath + @"\help\" + fileName; Help.ShowHelp(null, filepath, HelpNavigator.Topic, ""); } } /// /// 枚举窗体的回调函数,还原显示已打开的帮助文档 /// /// /// /// public static bool OpenOldWindow(IntPtr hWnd, IntPtr lParam) { var sb = new StringBuilder(255); WinAPI.GetWindowText(hWnd, sb, sb.Capacity); if (sb.Length > 0 && _CurrentHelpFile != "") //匹配窗体的标题成功,表示帮助文档已经打开,还原窗体 if (sb.ToStringEx().IndexOf(_CurrentHelpFile) >= 0) { WinAPI.ShowWindow(hWnd, WinAPI.SW_RESTORE); WinAPI.ShowWindow(hWnd, WinAPI.SW_SHOWMAXIMIZED); _CurrentHelpFileIsOpen = true; return false; } return true; } } }