1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#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;
        }
    }
}