-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
316 lines (286 loc) · 11.7 KB
/
Program.cs
File metadata and controls
316 lines (286 loc) · 11.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//This file is part of ExamPaper Factory
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ExamPaperFactory
{
internal static class Program
{
#region 判断Office 版本
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out int phkResult);
[DllImport("Advapi32.dll")]
static extern uint RegCloseKey(int hKey);
[DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
static extern int RegQueryValueEx(int hKey, string lpValueName, int lpReserved, ref uint lpType,
System.Text.StringBuilder lpData, ref uint lpcbData);
private static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
private static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
private static Dictionary<string, string> LatestVersions = new Dictionary<string, string>();
public static void Office()
{
LatestVersions.Add("12.0", "Office2007");
LatestVersions.Add("14.0", "Office2010");
LatestVersions.Add("15.0", "Office2013");
LatestVersions.Add("16.0", "Office2017 and higher");
}
private static string GetVersionNumberFromRegistry()
{
string regVersion;
regVersion = GetVersionNumberFromRegistry("SOFTWARE\\Microsoft\\Office\\");
if (regVersion == null)
regVersion = GetVersionNumberFromRegistry("SOFTWARE\\Wow6432Node\\Microsoft\\Office\\");
return regVersion;
}
private static string GetVersionNumberFromRegistry(string key)
{
string version = null;
foreach (string VerNo in LatestVersions.Keys)
{
string offPath = Reg64(HKEY_LOCAL_MACHINE, key + VerNo + "\\Excel\\InstallRoot", "Path");
if (offPath != null)
{
version = VerNo;
break;
}
}
return version;
}
public static string GetVersion()
{
string versionFromReg = GetVersionNumberFromRegistry();
string versionInstalled = LatestVersions[versionFromReg];
bool? Office64BitFromReg = Off64Bit("SOFTWARE\\Microsoft\\Office\\", versionFromReg) ?? Off64Bit("SOFTWARE\\Wow6432Node\\Microsoft\\Office\\", versionFromReg);
if (Office64BitFromReg.HasValue && Office64BitFromReg.Value) versionInstalled += "(64 bit)";
else if (Office64BitFromReg.HasValue && !Office64BitFromReg.Value) versionInstalled += "(32 bit)";
else versionInstalled += "(Unknown bit)";
return versionInstalled;
}
private static bool? Off64Bit(string key, string version)
{
bool? Office64BitFromReg = null;
string Bitness = Reg64(HKEY_LOCAL_MACHINE, key + version + "\\Outlook", "Bitness");
if (Bitness == "x86")
Office64BitFromReg = false;
else if ((Bitness == "x64"))
Office64BitFromReg = true;
return Office64BitFromReg;
}
private static string Reg64(UIntPtr parent, string key, string prop)
{
int ikey = 0;
int bit36_64 = 0x0100;
int query = 0x0001;
try
{
uint res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, query | bit36_64, out ikey);
if (0 != res) return null;
uint type = 0;
uint data = 1024;
StringBuilder buffer = new StringBuilder(1024);
RegQueryValueEx(ikey, prop, 0, ref type, buffer, ref data);
string ver = buffer.ToString();
return ver;
}
finally
{
if (0 != ikey) RegCloseKey(ikey);
}
}
#endregion
#region Win32 API
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr ptr);
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
int nIndex // index of capability
);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
#region DeviceCaps - 设备属性 常量
const int HORZRES = 8;
const int VERTRES = 10;
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
#endregion
#region 属性
// 获取屏幕分辨率当前物理大小
public static Size WorkingArea
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, HORZRES);
size.Height = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
// 当前系统DPI_X 大小 一般为96
public static int DpiX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
// 当前系统DPI_Y 大小 一般为96
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
// 获取真实设置的桌面分辨率大小
public static Size DesktopResolution
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
// 获取宽度缩放百分比
public static float ScaleX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
//float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
float ScaleX = (float)Program.DpiX / 96f;
ReleaseDC(IntPtr.Zero, hdc);
return ScaleX;
}
}
// 获取高度缩放百分比
public static float ScaleY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
//float ScaleY = (float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
float ScaleY = (float)Program.DpiY / 96f;
ReleaseDC(IntPtr.Zero, hdc);
return ScaleY;
}
}
#endregion
/// <summary>
/// 判断.Net Framework的Version是否符合需要
/// (.Net Framework 版本在2.0及以上)
/// </summary>
/// <param name="version">需要的版本 version = 4.5</param>
/// <returns></returns>
private static bool GetDotNetVersion(string version)
{
string oldname = "0";
using (RegistryKey ndpKey =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
{
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string newname = (string)versionKey.GetValue("Version", "");
if (string.Compare(newname, oldname) > 0)
{
oldname = newname;
}
if (newname != "")
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
newname = (string)subKey.GetValue("Version", "");
if (string.Compare(newname, oldname) > 0)
{
oldname = newname;
}
}
}
}
}
return string.Compare(oldname, version) > 0;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();//此方法为应用程序启用可视样式
/**
* 作用:在应用程序范围内设置控件显示文本的默认方式(可以设为使用新的GDI+ , 还是旧的GDI)
* true使用GDI+方式显示文本,
* false使用GDI方式显示文本。
*/
Application.SetCompatibleTextRenderingDefault(false);
Type officeType = Type.GetTypeFromProgID("Word.Application");
Office();
//检查系统环境是否符合要求,缩放比例是否符合要求
if (!GetDotNetVersion("4.7.2"))
{
if (MessageBox.Show("当前缺少\".Net Framework 4.7.2\"运行环境", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
{
SpecialForm specialForm = new SpecialForm();
specialForm.ShowDialog();
if (specialForm.IsPassedRight) Application.Run(new MainForm());
}
}
else if (ScaleX != 1f)
{
float scaling = 100 * ScaleX; if (MessageBox.Show("当前屏幕缩放比例为" + scaling + "%,请设置为100%后重试!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
{
SpecialForm specialForm = new SpecialForm();
specialForm.ShowDialog();
if (specialForm.IsPassedRight) Application.Run(new MainForm());
}
}
else if (officeType == null)
{
if (MessageBox.Show("当前系统未安装Office软件,请安装Office2013及以上版本!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
{
SpecialForm specialForm = new SpecialForm();
specialForm.ShowDialog();
if (specialForm.IsPassedRight) Application.Run(new MainForm());
}
}
else if (!(GetVersion().Contains("Office2017 and higher") || GetVersion().Contains("Office2013")))
{
if (MessageBox.Show("当前系统安装Office版本较低,请升至Office2013及以上!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
{
SpecialForm specialForm = new SpecialForm();
specialForm.ShowDialog();
if (specialForm.IsPassedRight) Application.Run(new MainForm());
}
}
else Application.Run(new MainForm());
}
}
}