Skip to content

Commit f7cd4dc

Browse files
committed
Moved API call, added OS version check
1 parent 33935a8 commit f7cd4dc

File tree

8 files changed

+174
-14
lines changed

8 files changed

+174
-14
lines changed

Rubberduck.Core/Common/IOperatingSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public interface IOperatingSystem
44
{
55
void ShowFolder(string folderPath);
6+
WindowsVersion? GetOSVersion();
67
}
78
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace Rubberduck.Common.WinAPI
4+
{
5+
public static class SHCore
6+
{
7+
/// <summary>
8+
/// Sets the DPI awerness level of the current process.
9+
/// </summary>
10+
/// <param name="awareness">DPI awareness level.</param>
11+
/// <returns>HRESULT of S_OK, E_INVALIDARG or E_ACCESSDENIED.</returns>
12+
/// <remarks>
13+
/// Only the first DPI awareness call made by a process will have effect, subsequent calls are disregarded.
14+
/// Thus, calling this method before WPF loads will override the default WPF DPI awareness behavior.
15+
/// </remarks>
16+
[DllImport("SHCore.dll", SetLastError = true)]
17+
public static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
18+
}
19+
20+
/// <summary>
21+
/// Describes DPI awareness of a process.
22+
/// </summary>
23+
public enum PROCESS_DPI_AWARENESS
24+
{
25+
/// <summary>
26+
/// Process is not DPI aware.
27+
/// </summary>
28+
Process_DPI_Unaware = 0,
29+
30+
/// <summary>
31+
/// Process is aware of the System DPI (monitor 1).
32+
/// </summary>
33+
Process_System_DPI_Aware = 1,
34+
35+
/// <summary>
36+
/// Process is aware of the DPI of individual monitors.
37+
/// </summary>
38+
Process_Per_Monitor_DPI_Aware = 2
39+
}
40+
41+
}
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,56 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
23
using System.IO;
4+
using System.Management;
5+
using System.Runtime.CompilerServices;
6+
using NLog;
37

48
namespace Rubberduck.Common
59
{
610
public sealed class WindowsOperatingSystem : IOperatingSystem
711
{
12+
public static readonly ILogger _Logger = LogManager.GetCurrentClassLogger();
13+
814
public void ShowFolder(string folderPath)
915
{
1016
if (!Directory.Exists(folderPath))
1117
{
1218
Directory.CreateDirectory(folderPath);
1319
}
1420

15-
using (Process.Start(folderPath)) { }
21+
using (Process.Start(folderPath))
22+
{
23+
}
24+
}
25+
26+
27+
public WindowsVersion? GetOSVersion()
28+
{
29+
try
30+
{
31+
var wmiEnum = new ManagementObjectSearcher("root\\CIMV2", "SELECT Version FROM Win32_OperatingSystem")
32+
.Get().GetEnumerator();
33+
wmiEnum.MoveNext();
34+
var versionString = wmiEnum.Current.Properties["Version"].Value as string;
35+
36+
var versionElements = versionString?.Split('.');
37+
38+
if (versionElements?.Length >= 3 &&
39+
int.TryParse(versionElements[0], out var major) &&
40+
int.TryParse(versionElements[1], out var minor) &&
41+
int.TryParse(versionElements[2], out var build))
42+
{
43+
return new WindowsVersion(major, minor, build);
44+
}
45+
}
46+
catch (Exception exception)
47+
{
48+
_Logger.Warn(exception, "Unable to determine OS Version");
49+
return null;
50+
}
51+
_Logger.Warn("Unable to determine OS Version");
52+
return null;
1653
}
1754
}
1855
}
56+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
3+
namespace Rubberduck.Common
4+
{
5+
public struct WindowsVersion : IComparable<WindowsVersion>
6+
{
7+
public static WindowsVersion Windows10_1809 = new WindowsVersion(10, 0, 17763);
8+
public static WindowsVersion Windows10_1803 = new WindowsVersion(10, 0, 17134);
9+
public static WindowsVersion Windows10_1709 = new WindowsVersion(10, 0, 16299);
10+
public static WindowsVersion Windows10_1703 = new WindowsVersion(10, 0, 15063);
11+
public static WindowsVersion Windows10_1607 = new WindowsVersion(10, 0, 14393);
12+
public static WindowsVersion Windows10_1511 = new WindowsVersion(10, 0, 10586);
13+
public static WindowsVersion Windows10 = new WindowsVersion(10, 0, 10240);
14+
public static WindowsVersion Windows81_U1 = new WindowsVersion(6, 3, 9600);
15+
public static WindowsVersion Windows81 = new WindowsVersion(6, 3, 9200);
16+
public static WindowsVersion Windows8 = new WindowsVersion(6, 2, 9200);
17+
public static WindowsVersion Windows7_SP1 = new WindowsVersion(6, 1, 7601);
18+
public static WindowsVersion Windows7 = new WindowsVersion(6, 1, 7600);
19+
public static WindowsVersion WindowsVista_SP2 = new WindowsVersion(6, 0, 6002);
20+
public static WindowsVersion WindowsVista_SP1 = new WindowsVersion(6, 0, 6001);
21+
public static WindowsVersion WindowsVista = new WindowsVersion(6, 0, 6000);
22+
23+
public WindowsVersion(int major, int minor, int build)
24+
{
25+
Major = major;
26+
Minor = minor;
27+
Build = build;
28+
}
29+
30+
public int Major { get; }
31+
public int Minor { get; }
32+
public int Build { get; }
33+
34+
public int CompareTo(WindowsVersion other)
35+
{
36+
var majorComparison = Major.CompareTo(other.Major);
37+
if (majorComparison != 0)
38+
{
39+
return majorComparison;
40+
}
41+
42+
var minorComparison = Minor.CompareTo(other.Minor);
43+
44+
return minorComparison != 0
45+
? minorComparison
46+
: Build.CompareTo(other.Build);
47+
}
48+
49+
public static bool operator ==(WindowsVersion os1, WindowsVersion os2)
50+
{
51+
return os1.CompareTo(os2) == 0;
52+
}
53+
54+
public static bool operator !=(WindowsVersion os1, WindowsVersion os2)
55+
{
56+
return os1.CompareTo(os2) != 0;
57+
}
58+
59+
public static bool operator <(WindowsVersion os1, WindowsVersion os2)
60+
{
61+
return os1.CompareTo(os2) < 0;
62+
}
63+
64+
public static bool operator >(WindowsVersion os1, WindowsVersion os2)
65+
{
66+
return os1.CompareTo(os2) > 0;
67+
}
68+
69+
public static bool operator <=(WindowsVersion os1, WindowsVersion os2)
70+
{
71+
return os1.CompareTo(os2) <= 0;
72+
}
73+
74+
public static bool operator >=(WindowsVersion os1, WindowsVersion os2)
75+
{
76+
return os1.CompareTo(os2) >= 0;
77+
}
78+
}
79+
}

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Reference Include="PresentationCore" />
3030
<Reference Include="PresentationFramework" />
3131
<Reference Include="PresentationFramework.Aero" />
32+
<Reference Include="System.Management" />
3233
<Reference Include="System.Net.Http" />
3334
<Reference Include="System.Windows.Forms" />
3435
<Reference Include="System.Xaml" />

Rubberduck.Core/UI/Settings/GeneralSettings.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<CheckBox Margin="5,0,0,5" Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=GeneralSettings_CompileBeforeParse}"
133133
IsChecked="{Binding CompileBeforeParse}" />
134134
<CheckBox Margin="5,0,0,5" Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=GeneralSettings_SetDpiUnaware}"
135-
IsChecked="{Binding SetDpiUnaware}" />
135+
IsEnabled="{Binding SetDpiUnawareEnabled}" IsChecked="{Binding SetDpiUnaware}" />
136136
<StackPanel Orientation="Horizontal"/>
137137

138138
<Label Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=GeneralSettings_MinimumLogLevelLabel}" FontWeight="SemiBold" />

Rubberduck.Core/UI/Settings/GeneralSettingsViewModel.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ public bool SetDpiUnaware
172172
}
173173
}
174174

175+
public bool SetDpiUnawareEnabled
176+
{
177+
get
178+
{
179+
var osVersion = _operatingSystem.GetOSVersion();
180+
return osVersion != null && osVersion >= WindowsVersion.Windows81;
181+
}
182+
}
183+
175184
private bool SynchronizeVBESettings()
176185
{
177186
if (!_messageBox.ConfirmYesNo(RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled,

Rubberduck.Main/Extension.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Windows.Threading;
1313
using Castle.Windsor;
1414
using NLog;
15+
using Rubberduck.Common.WinAPI;
1516
using Rubberduck.Root;
1617
using Rubberduck.Resources;
1718
using Rubberduck.Resources.Registration;
@@ -164,7 +165,7 @@ private void InitializeAddIn()
164165
{
165166
if (_initialSettings.SetDpiUnaware)
166167
{
167-
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);
168+
SHCore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);
168169
}
169170
}
170171
catch (Exception)
@@ -311,15 +312,5 @@ private void ShutdownAddIn()
311312
}
312313
}
313314
}
314-
315-
[DllImport("SHCore.dll", SetLastError = true)]
316-
private static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
317-
318-
private enum PROCESS_DPI_AWARENESS
319-
{
320-
Process_DPI_Unaware = 0,
321-
Process_System_DPI_Aware = 1,
322-
Process_Per_Monitor_DPI_Aware = 2
323-
}
324315
}
325316
}

0 commit comments

Comments
 (0)