-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find process name instead of window name
Fixes #3
- Loading branch information
Showing
4 changed files
with
54 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace GenshinLyreMidiPlayer.Core | ||
{ | ||
public static class WindowHelper | ||
{ | ||
public const string GenshinProcessName = "GenshinImpact"; | ||
|
||
private static IntPtr? FindWindowByProcessName(string processName) | ||
{ | ||
var process = Process.GetProcessesByName(processName); | ||
return process.FirstOrDefault()?.MainWindowHandle; | ||
} | ||
|
||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] | ||
private static extern IntPtr GetForegroundWindow(); | ||
|
||
[DllImport("user32.dll")] | ||
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown); | ||
|
||
public static bool EnsureGameOnTop() | ||
{ | ||
var genshinWindow = FindWindowByProcessName(GenshinProcessName); | ||
|
||
if (genshinWindow is null) | ||
return false; | ||
|
||
SwitchToThisWindow((IntPtr) genshinWindow, true); | ||
|
||
return !genshinWindow.Equals(IntPtr.Zero) && | ||
GetForegroundWindow().Equals(genshinWindow); | ||
} | ||
|
||
private static bool IsWindowFocused(IntPtr windowPtr) | ||
{ | ||
var hWnd = GetForegroundWindow(); | ||
return hWnd.Equals(windowPtr); | ||
} | ||
|
||
public static bool IsGameFocused() | ||
{ | ||
var genshinWindow = FindWindowByProcessName(GenshinProcessName); | ||
return genshinWindow != null && | ||
IsWindowFocused((IntPtr) genshinWindow); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters