Skip to content

Commit ab45d00

Browse files
Randall FlaggRandall Flagg
authored andcommitted
Changed from DllImport to LibraryImport
1 parent 94f5cae commit ab45d00

5 files changed

Lines changed: 55 additions & 59 deletions

File tree

src/LogExpert.UI/Dialogs/LogTabWindow/LogTabWindowPrivate.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,6 @@ private void SetTabIcons(Preferences preferences)
10011001
}
10021002
}
10031003

1004-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
1005-
private static extern bool DestroyIcon(IntPtr handle);
1006-
10071004
private void SetToolIcon(ToolEntry entry, ToolStripItem item)
10081005
{
10091006
Icon icon = Win32.LoadIconFromExe(entry.iconFile, entry.iconIndex);
@@ -1019,7 +1016,7 @@ private void SetToolIcon(ToolEntry entry, ToolStripItem item)
10191016
item.DisplayStyle = ToolStripItemDisplayStyle.Image;
10201017
}
10211018

1022-
DestroyIcon(icon.Handle);
1019+
Win32.DestroyIcon(icon.Handle);
10231020
icon.Dispose();
10241021
}
10251022

src/LogExpert.UI/Dialogs/LogTabWindow/LogTabWindowPublic.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using LogExpert.Core.Interface;
66
using LogExpert.Dialogs;
77
using LogExpert.UI.Entities;
8+
using LogExpert.UI.Extensions;
89
using System.Runtime.InteropServices;
910
using System.Text;
1011
using System.Text.RegularExpressions;
@@ -283,7 +284,7 @@ public void SelectTab(ILogWindow logWindow)
283284

284285
public void SetForeground()
285286
{
286-
SetForegroundWindow(Handle);
287+
Win32.SetForegroundWindow(Handle);
287288
if (WindowState == FormWindowState.Minimized)
288289
{
289290
if (_wasMaximized)
@@ -297,9 +298,6 @@ public void SetForeground()
297298
}
298299
}
299300

300-
[DllImport("User32.dll")]
301-
public static extern int SetForegroundWindow(IntPtr hWnd);
302-
303301
// called from LogWindow when follow tail was changed
304302
public void FollowTailChanged(LogWindow.LogWindow logWindow, bool isEnabled, bool offByTrigger)
305303
{

src/LogExpert.UI/Dialogs/LogTabWindow/SettingsDialog.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public SettingsDialog(Preferences prefs, LogTabWindow logTabWin, int tabToOpen,
5656

5757
#region Private Methods
5858

59-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
60-
private static extern bool DestroyIcon(IntPtr handle);
61-
6259
private void FillDialog()
6360
{
6461
Preferences ??= new Preferences();
@@ -564,7 +561,7 @@ private void DisplayCurrentIcon()
564561
{
565562
Image image = icon.ToBitmap();
566563
buttonIcon.Image = image;
567-
DestroyIcon(icon.Handle);
564+
Win32.DestroyIcon(icon.Handle);
568565
icon.Dispose();
569566
}
570567
else

src/LogExpert.UI/Extensions/Win32.cs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,61 @@
55
namespace LogExpert.UI.Extensions
66
{
77
[SupportedOSPlatform("windows")]
8-
internal static class Win32
8+
internal static partial class Win32 //NativeMethods
99
{
1010
#region Fields
1111

1212
public const long SM_CYVSCROLL = 20;
1313
public const long SM_CXHSCROLL = 21;
1414
public const long SM_CXVSCROLL = 2;
1515
public const long SM_CYHSCROLL = 3;
16+
private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
17+
private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
1618

1719
#endregion
1820

19-
#region Public methods
21+
#region Library Imports
22+
[LibraryImport("user32.dll")]
23+
[return: MarshalAs(UnmanagedType.Bool)]
24+
public static partial bool DestroyIcon(nint hIcon);
25+
26+
[LibraryImport("User32.dll")]
27+
public static partial int SetForegroundWindow(nint hWnd);
28+
29+
[LibraryImport("user32.dll")]
30+
public static partial long GetSystemMetricsForDpi(long index);
31+
32+
[LibraryImport("user32.dll")]
33+
public static partial long GetSystemMetrics(long index);
34+
35+
[LibraryImport("user32.dll")]
36+
public static partial short GetKeyState(int vKey);
37+
38+
/*
39+
UINT ExtractIconEx(
40+
LPCTSTR lpszFile,
41+
int nIconIndex,
42+
HICON *phiconLarge,
43+
HICON *phiconSmall,
44+
UINT nIcons
45+
);
46+
* */
47+
[LibraryImport("shell32.dll", StringMarshalling = StringMarshalling.Utf16)]
48+
public static partial uint ExtractIconEx(
49+
string fileName,
50+
int iconIndex,
51+
ref nint iconsLarge,
52+
ref nint iconsSmall,
53+
uint numIcons
54+
);
55+
56+
#region TitleBarDarkMode
57+
[LibraryImport("dwmapi.dll")]
58+
public static partial int DwmSetWindowAttribute(nint hwnd, int attr, ref int attrValue, int attrSize);
59+
#endregion
60+
#endregion
2061

21-
[DllImport("user32.dll")]
22-
public static extern bool DestroyIcon(nint hIcon);
62+
#region Public methods
2363

2464
public static Icon LoadIconFromExe(string fileName, int index)
2565
{
@@ -43,7 +83,6 @@ public static Icon LoadIconFromExe(string fileName, int index)
4383
return null;
4484
}
4585

46-
4786
public static Icon[,] ExtractIcons(string fileName)
4887
{
4988
nint smallIcon = nint.Zero;
@@ -83,45 +122,10 @@ public static Icon LoadIconFromExe(string fileName, int index)
83122
return result;
84123
}
85124

86-
[DllImport("user32.dll")]
87-
public static extern long GetSystemMetricsForDpi(long index);
88-
89-
90-
[DllImport("user32.dll")]
91-
public static extern long GetSystemMetrics(long index);
92-
93-
[DllImport("user32.dll")]
94-
public static extern short GetKeyState(int vKey);
95-
96125
#endregion
97126

98127
#region Private Methods
99128

100-
/*
101-
UINT ExtractIconEx(
102-
LPCTSTR lpszFile,
103-
int nIconIndex,
104-
HICON *phiconLarge,
105-
HICON *phiconSmall,
106-
UINT nIcons
107-
);
108-
* */
109-
110-
[DllImport("shell32.dll")]
111-
private static extern uint ExtractIconEx(string fileName,
112-
int iconIndex,
113-
ref nint iconsLarge,
114-
ref nint iconsSmall,
115-
uint numIcons
116-
);
117-
118-
#region TitleBarDarkMode
119-
[DllImport("dwmapi.dll")]
120-
private static extern int DwmSetWindowAttribute(nint hwnd, int attr, ref int attrValue, int attrSize);
121-
122-
private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
123-
private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
124-
125129
public static bool UseImmersiveDarkMode(nint handle, bool enabled)
126130
{
127131

@@ -143,6 +147,5 @@ private static bool IsWindows10OrGreater(int build = -1)
143147

144148
#endregion TitleBarDarkMode
145149

146-
#endregion
147150
}
148151
}

src/LogExpert.UI/LogExpert.UI.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows</TargetFramework>
5-
<Nullable>disable</Nullable>
6-
<UseWindowsForms>true</UseWindowsForms>
7-
<ImplicitUsings>enable</ImplicitUsings>
8-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9-
<SignAssembly>True</SignAssembly>
4+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
105
<AssemblyOriginatorKeyFile>..\Solution Items\Key.snk</AssemblyOriginatorKeyFile>
116
<ForceDesignerDPIUnaware>true</ForceDesignerDPIUnaware>
7+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<Nullable>disable</Nullable>
10+
<SignAssembly>True</SignAssembly>
11+
<TargetFramework>net8.0-windows</TargetFramework>
12+
<UseWindowsForms>true</UseWindowsForms>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

0 commit comments

Comments
 (0)