-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManipulateWindow.cs
More file actions
57 lines (51 loc) · 2.07 KB
/
ManipulateWindow.cs
File metadata and controls
57 lines (51 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsInput;
namespace NaturalCommands
{
public class ManipulateWindow
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hwnd);
private enum ShowWindowEnum
{
Hide = 0,
ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3,
Maximize = 3, ShowNormalNoActivate = 4, Show = 5,
Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8,
Restore = 9, ShowDefault = 10, ForceMinimized = 11
};
public void BringMainWindowToFront(string processName)
{
// get the process
Process? bProcess = Process.GetProcessesByName(processName).FirstOrDefault();
InputSimulator inputSimulator = new InputSimulator();
inputSimulator.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.APPS);
// check if the process is running
if (bProcess != null)
{
// check if the window is hidden / minimized
if (bProcess.MainWindowHandle == IntPtr.Zero)
{
// the window is hidden so try to restore it before setting focus.
ShowWindow(bProcess.MainWindowHandle, ShowWindowEnum.Restore);
}
// set user the focus to the window
SetForegroundWindow(bProcess.MainWindowHandle);
}
else
{
// the process is not running, so start it
Process.Start(processName);
}
inputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.APPS);
}
}
}