Skip to content

Commit 84d1a15

Browse files
committed
NativeMethods replaced with common WinApiHelper
1 parent b1e3649 commit 84d1a15

3 files changed

Lines changed: 14 additions & 45 deletions

File tree

OpenHardwareMonitor/OpenHardwareMonitor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="..\..\common\sergiye.Common\SerializerHelper.cs" Link="common\SerializerHelper.cs" />
8585
<Compile Include="..\..\common\sergiye.Common\Updater.cs" Link="common\Updater.cs" />
8686
<Compile Include="..\..\common\sergiye.Common\Crasher.cs" Link="common\Crasher.cs" />
87+
<Compile Include="..\..\common\sergiye.Common\WinApiHelper.cs" Link="common\WinApiHelper.cs" />
8788
<Compile Include="..\..\common\sergiye.Common.UI\PersistentSettings.cs" Link="common\PersistentSettings.cs" />
8889
<Compile Include="..\..\common\sergiye.Common.UI\Themes\CustomTheme.cs" Link="common\Themes\CustomTheme.cs" />
8990
<Compile Include="..\..\common\sergiye.Common.UI\Themes\DarkTheme.cs" Link="common\Themes\DarkTheme.cs" />

OpenHardwareMonitor/UI/MainForm.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -961,20 +961,15 @@ private void SysTrayHideShow()
961961

962962
protected override void WndProc(ref Message m)
963963
{
964-
const int WM_SYSCOMMAND = 0x112;
965-
const int WM_WININICHANGE = 0x001A;
966-
const int SC_MINIMIZE = 0xF020;
967-
const int SC_CLOSE = 0xF060;
968-
969-
if (_minimizeToTray.Value && m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_MINIMIZE)
964+
if (_minimizeToTray.Value && m.Msg == WinApiHelper.WM_SYS_COMMAND && m.WParam.ToInt64() == WinApiHelper.SC_MINIMIZE)
970965
{
971966
SysTrayHideShow();
972967
}
973-
else if (m.Msg == WM_WININICHANGE && Marshal.PtrToStringUni(m.LParam) == "ImmersiveColorSet" && _autoThemeMenuItem?.Checked == true)
968+
else if (m.Msg == WinApiHelper.WM_WININICHANGE && Marshal.PtrToStringUni(m.LParam) == "ImmersiveColorSet" && _autoThemeMenuItem?.Checked == true)
974969
{
975970
Theme.SetAutoTheme();
976971
}
977-
else if (_minimizeOnClose.Value && m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_CLOSE)
972+
else if (_minimizeOnClose.Value && m.Msg == WinApiHelper.WM_SYS_COMMAND && m.WParam.ToInt64() == WinApiHelper.SC_CLOSE)
978973
{
979974
//Apparently the user wants to minimize rather than close
980975
//Now we still need to check if we're going to the tray or not
@@ -986,13 +981,13 @@ protected override void WndProc(ref Message m)
986981
else
987982
WindowState = FormWindowState.Minimized;
988983
}
989-
else if (m.Msg == NativeMethods.WM_USER + 1)
984+
else if (m.Msg == WinApiHelper.WM_SHOWME)
990985
{
991986
Visible = true;
992987
WindowState = FormWindowState.Normal;
993988
Activate();
994989
BringToFront();
995-
NativeMethods.SetForegroundWindow(this.Handle);
990+
WinApiHelper.SetForegroundWindow(this.Handle);
996991
}
997992
else
998993
{

OpenHardwareMonitor/UI/ShowDesktop.cs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private ShowDesktop()
2121

2222
CreateParams cp = new CreateParams { ExStyle = GadgetWindow.WS_EX_TOOLWINDOW, Caption = _referenceWindowCaption };
2323
_referenceWindow.CreateHandle(cp);
24-
NativeMethods.SetWindowPos(_referenceWindow.Handle,
24+
WinApiHelper.SetWindowPos(_referenceWindow.Handle,
2525
GadgetWindow.HWND_BOTTOM,
2626
0,
2727
0,
@@ -82,23 +82,23 @@ private void StopTimer()
8282
// the desktop worker window (if available) can hide the reference window
8383
private IntPtr GetDesktopWorkerWindow()
8484
{
85-
IntPtr shellWindow = NativeMethods.GetShellWindow();
85+
IntPtr shellWindow = WinApiHelper.GetShellWindow();
8686
if (shellWindow == IntPtr.Zero)
8787
return IntPtr.Zero;
8888

8989

90-
NativeMethods.GetWindowThreadProcessId(shellWindow, out int shellId);
90+
WinApiHelper.GetWindowThreadProcessId(shellWindow, out int shellId);
9191

9292
IntPtr workerWindow = IntPtr.Zero;
93-
while ((workerWindow = NativeMethods.FindWindowEx(IntPtr.Zero, workerWindow, "WorkerW", null)) != IntPtr.Zero)
93+
while ((workerWindow = WinApiHelper.FindWindowEx(IntPtr.Zero, workerWindow, "WorkerW", null)) != IntPtr.Zero)
9494
{
95-
NativeMethods.GetWindowThreadProcessId(workerWindow, out int workerId);
95+
WinApiHelper.GetWindowThreadProcessId(workerWindow, out int workerId);
9696
if (workerId == shellId)
9797
{
98-
IntPtr window = NativeMethods.FindWindowEx(workerWindow, IntPtr.Zero, "SHELLDLL_DefView", null);
98+
IntPtr window = WinApiHelper.FindWindowEx(workerWindow, IntPtr.Zero, "SHELLDLL_DefView", null);
9999
if (window != IntPtr.Zero)
100100
{
101-
IntPtr desktopWindow = NativeMethods.FindWindowEx(window, IntPtr.Zero, "SysListView32", null);
101+
IntPtr desktopWindow = WinApiHelper.FindWindowEx(window, IntPtr.Zero, "SysListView32", null);
102102
if (desktopWindow != IntPtr.Zero)
103103
return workerWindow;
104104
}
@@ -116,7 +116,7 @@ private void OnTimer(object state)
116116
if (workerWindow != IntPtr.Zero)
117117
{
118118
// search if the reference window is behind the worker window
119-
IntPtr reference = NativeMethods.FindWindowEx(IntPtr.Zero, workerWindow, null, _referenceWindowCaption);
119+
IntPtr reference = WinApiHelper.FindWindowEx(IntPtr.Zero, workerWindow, null, _referenceWindowCaption);
120120
showDesktopDetected = reference != IntPtr.Zero;
121121
}
122122
else
@@ -132,30 +132,3 @@ private void OnTimer(object state)
132132
}
133133
}
134134
}
135-
136-
internal static class NativeMethods
137-
{
138-
internal const int WM_USER = 0x0400;
139-
private const string USER = "user32.dll";
140-
141-
[DllImport(USER, CallingConvention = CallingConvention.Winapi)]
142-
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
143-
144-
[DllImport("user32.dll", EntryPoint = "SendMessageA", SetLastError = true)]
145-
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, int wParam, IntPtr lParam);
146-
147-
[DllImport("user32.dll")]
148-
internal static extern bool SetForegroundWindow(IntPtr hWnd);
149-
150-
[DllImport(USER, CallingConvention = CallingConvention.Winapi)]
151-
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
152-
153-
[DllImport("user32.dll")]
154-
internal static extern IntPtr FindWindow(string className, string windowName);
155-
156-
[DllImport(USER, CallingConvention = CallingConvention.Winapi)]
157-
public static extern IntPtr GetShellWindow();
158-
159-
[DllImport(USER, CallingConvention = CallingConvention.Winapi)]
160-
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
161-
}

0 commit comments

Comments
 (0)