Use CsWin32 in favor of NativeMethods

i was planning to use pinvoke but turns out it was deprecated just 6 days ago lol
This commit is contained in:
pizzaboxer 2023-08-01 19:09:52 +01:00
parent bf53b0642e
commit 48afdd7036
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
8 changed files with 36 additions and 49 deletions

View File

@ -2,7 +2,8 @@
using System.Windows;
using System.Windows.Threading;
using Microsoft.Win32;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace Bloxstrap
{
@ -215,9 +216,9 @@ namespace Bloxstrap
if (menuProcess is not null)
{
IntPtr handle = menuProcess.MainWindowHandle;
var handle = menuProcess.MainWindowHandle;
Logger.WriteLine(LOG_IDENT, $"Found an already existing menu window with handle {handle}");
NativeMethods.SetForegroundWindow(handle);
PInvoke.SetForegroundWindow((HWND)handle);
}
else
{

View File

@ -40,6 +40,10 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.18-beta">
<!--<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>-->
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="securifybv.ShellLink" Version="0.1.0" />
</ItemGroup>

View File

@ -0,0 +1,4 @@
SetForegroundWindow
FlashWindow
GetWindowLong
SetWindowLong

View File

@ -1,17 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using Bloxstrap.Integrations;
@ -92,10 +85,11 @@ namespace Bloxstrap.UI.Elements.ContextMenu
// this is done to register the context menu wrapper as a tool window so it doesnt appear in the alt+tab switcher
// https://stackoverflow.com/a/551847/11852173
var wndHelper = new WindowInteropHelper(this);
long exStyle = NativeMethods.GetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE).ToInt64();
exStyle |= NativeMethods.WS_EX_TOOLWINDOW;
NativeMethods.SetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE, (IntPtr)exStyle);
HWND hWnd = (HWND)new WindowInteropHelper(this).Handle;
int exStyle = PInvoke.GetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
exStyle |= 0x00000080; //NativeMethods.WS_EX_TOOLWINDOW;
PInvoke.SetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, exStyle);
}
private void Window_Closed(object sender, EventArgs e) => App.Logger.WriteLine("MenuContainer::Window_Closed", "Context menu container closed");

View File

@ -1,6 +1,9 @@
using System.Media;
using System.Windows.Interop;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace Bloxstrap.UI.Elements.Dialogs
{
// hmm... do i use MVVM for this?
@ -34,8 +37,8 @@ namespace Bloxstrap.UI.Elements.Dialogs
Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
var hWnd = new WindowInteropHelper(this).Handle;
PInvoke.FlashWindow((HWND)hWnd, true);
};
}
}

View File

@ -2,6 +2,9 @@
using System.Windows;
using System.Windows.Interop;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace Bloxstrap.UI.Elements.Dialogs
{
// hmm... do i use MVVM for this?
@ -60,7 +63,7 @@ namespace Bloxstrap.UI.Elements.Dialogs
Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
PInvoke.FlashWindow((HWND)hWnd, true);
};
}
}

View File

@ -4,6 +4,9 @@ using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Windows.Win32;
using Windows.Win32.Foundation;
using Bloxstrap.UI.Utility;
namespace Bloxstrap.UI.Elements.Dialogs
@ -107,8 +110,8 @@ namespace Bloxstrap.UI.Elements.Dialogs
Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
var hWnd = new WindowInteropHelper(this).Handle;
PInvoke.FlashWindow((HWND)hWnd, true);
};
}

View File

@ -1,25 +0,0 @@
using System.Runtime.InteropServices;
namespace Bloxstrap.Utility
{
static class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
// i only bothered to add the constants that im using lol
public const int GWL_EXSTYLE = -20;
public const int WS_EX_TOOLWINDOW = 0x00000080;
}
}