#region
|
|
using System;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Text;
|
using System.Windows.Forms;
|
|
#endregion
|
|
namespace CSFrameworkV5.Common
|
{
|
/// <summary>
|
/// 帮助文档
|
/// </summary>
|
public class HelpDoc
|
{
|
public const string CSHelp = "《开发框架使用入门指南》.pdf";
|
|
/// <summary>
|
/// 当前正在打开的帮助文件名
|
/// </summary>
|
private static string _CurrentHelpFile = "";
|
|
/// <summary>
|
/// 标记当前帮助文件是否打开,如果已打开,还原显示帮助文档
|
/// </summary>
|
private static bool _CurrentHelpFileIsOpen;
|
|
/// <summary>
|
/// 直接打开文件
|
/// </summary>
|
/// <param name="fileName">文件名</param>
|
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("不支持的文件格式!");
|
}
|
}
|
|
/// <summary>
|
/// 打开CHM类型帮助文件
|
/// </summary>
|
/// <param name="fileName">文件名</param>
|
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, "");
|
}
|
}
|
|
/// <summary>
|
/// 枚举窗体的回调函数,还原显示已打开的帮助文档
|
/// </summary>
|
/// <param name="hWnd"></param>
|
/// <param name="lParam"></param>
|
/// <returns></returns>
|
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;
|
}
|
}
|
}
|