Skip to content

Commit

Permalink
Use CsWin32 in favor of NativeMethods
Browse files Browse the repository at this point in the history
i was planning to use pinvoke but turns out it was deprecated just 6 days ago lol
  • Loading branch information
pizzaboxer committed Aug 1, 2023
1 parent bf53b06 commit 48afdd7
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 49 deletions.
7 changes: 4 additions & 3 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Windows;
using System.Windows.Threading;

using Microsoft.Win32;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Bloxstrap
{
Expand Down Expand Up @@ -215,9 +216,9 @@ protected override void OnStartup(StartupEventArgs e)

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
{
Expand Down
4 changes: 4 additions & 0 deletions Bloxstrap/Bloxstrap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
4 changes: 4 additions & 0 deletions Bloxstrap/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SetForegroundWindow
FlashWindow
GetWindowLong
SetWindowLong
26 changes: 10 additions & 16 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -92,10 +85,11 @@ private void Window_Loaded(object? sender, RoutedEventArgs e)
// 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");
Expand Down
7 changes: 5 additions & 2 deletions Bloxstrap/UI/Elements/Dialogs/ConnectivityDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -34,8 +37,8 @@ public ConnectivityDialog(string targetName, string description, Exception excep

Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
var hWnd = new WindowInteropHelper(this).Handle;
PInvoke.FlashWindow((HWND)hWnd, true);
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion Bloxstrap/UI/Elements/Dialogs/ExceptionDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -60,7 +63,7 @@ public ExceptionDialog(Exception exception)
Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
PInvoke.FlashWindow((HWND)hWnd, true);
};
}
}
Expand Down
7 changes: 5 additions & 2 deletions Bloxstrap/UI/Elements/Dialogs/FluentMessageBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
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
Expand Down Expand Up @@ -107,8 +110,8 @@ public FluentMessageBox(string message, MessageBoxImage image, MessageBoxButton

Loaded += delegate
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
NativeMethods.FlashWindow(hWnd, true);
var hWnd = new WindowInteropHelper(this).Handle;
PInvoke.FlashWindow((HWND)hWnd, true);
};
}

Expand Down
25 changes: 0 additions & 25 deletions Bloxstrap/Utility/NativeMethods.cs

This file was deleted.

0 comments on commit 48afdd7

Please sign in to comment.