DisplayMagician/HeliosDisplayManagement.Shared/ShellHelper.cs
temacdonald a9bb295d1f Renamed app to HeliosPlus namespace and more
Renamed app to HeliosPlus namespace so that the updated
changes don't interfere with HeliosDisplayManagement if
that is also installed. The fact that I've changed so much of
the app means that my changes would be unlikely to be
accepted by Soroush, so I'm best to release this work in a
similar way to other projects like Notepad++, by keeping
the same root name, and adding a plus.
    I've also changed the Shortcut form to put all the games
in a single list to reduce the number of clicks a user has to
do in order for them to create a shortcut. I have begun to
prepare the form so that it will support multiple game
libraries, but now I am at a point that I need to fix the Steam
and Uplay game detection mechanisms so that they report
the correct information for the lv_games list view.
2020-04-23 20:16:16 +12:00

75 lines
2.6 KiB
C#

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace HeliosPlus.Shared
{
public static class ShellHelper
{
private static readonly uint NotifyMessage_SettingChange = 0x001A;
private static readonly uint NotifyMessage_ThemeChanged = 0x031A;
private static readonly uint ShellChange_AllEvents = 0x7FFFFFFF;
private static readonly uint ShellChange_FlushFlag = 0x1000;
private static readonly uint ShellChange_NotifyRecursiveFlag = 0x10000;
private static readonly UIntPtr WindowHandleBroadcast = (UIntPtr) 0xffff;
public static Process GetShellProcess()
{
try
{
var shellWindowHandle = GetShellWindow();
if (shellWindowHandle != IntPtr.Zero)
{
GetWindowThreadProcessId(shellWindowHandle, out var shellPid);
if (shellPid > 0)
{
return Process.GetProcessById((int) shellPid);
}
}
}
catch (Exception)
{
// ignored
}
return null;
}
public static async Task IntrigueShellToWriteSettings()
{
try
{
SendNotifyMessage(WindowHandleBroadcast, NotifyMessage_SettingChange, (UIntPtr) 0, "Policy");
SendNotifyMessage(WindowHandleBroadcast, NotifyMessage_ThemeChanged, (UIntPtr) 0, null);
ShellChangeNotify(ShellChange_AllEvents, ShellChange_FlushFlag | ShellChange_NotifyRecursiveFlag,
IntPtr.Zero, IntPtr.Zero);
}
catch (Exception)
{
// ignored
}
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}
[DllImport("user32")]
private static extern IntPtr GetShellWindow();
[DllImport("user32", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr windowHandle, out uint processId);
[DllImport("user32", EntryPoint = "SendNotifyMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SendNotifyMessage(
UIntPtr windowHandle,
uint messageId,
UIntPtr wParam,
string lParam);
[DllImport("shell32", EntryPoint = "SHChangeNotify", SetLastError = true)]
private static extern int ShellChangeNotify(uint eventId, uint flags, IntPtr item1, IntPtr item2);
}
}