1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . Runtime . InteropServices ;
3
4
using System . Text ;
4
5
@@ -8,6 +9,9 @@ public static class Windows
8
9
{
9
10
internal static class NativeMethods
10
11
{
12
+ public const int WM_KEYDOWN = 0x100 ;
13
+ public const int VK_RETURN = 0x0D ;
14
+
11
15
[ DllImport ( "user32.dll" ) ]
12
16
public static extern IntPtr GetForegroundWindow ( ) ;
13
17
@@ -16,10 +20,16 @@ internal static class NativeMethods
16
20
17
21
[ DllImport ( "user32.dll" , CharSet = CharSet . Unicode , SetLastError = true ) ]
18
22
public static extern int GetWindowText ( IntPtr hWnd , StringBuilder lpString , int nMaxCount ) ;
23
+
24
+ [ DllImport ( "User32.dll" ) ]
25
+ public static extern int SetForegroundWindow ( IntPtr point ) ;
26
+
27
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
28
+ public static extern IntPtr SendMessage ( IntPtr hWnd , int Msg , int wParam , IntPtr lParam ) ;
19
29
}
20
30
21
31
22
- static public bool IsForegroundWindowTitle ( string title )
32
+ static public bool IsForegroundWindowByTitle ( string title )
23
33
{
24
34
IntPtr hwnd = NativeMethods . GetForegroundWindow ( ) ;
25
35
@@ -36,5 +46,61 @@ static public bool IsForegroundWindowTitle(string title)
36
46
Logger . Information ( $ "Foreground Window title = '{ windowtitle . ToString ( ) } '") ;
37
47
return ( windowtitle . ToString ( ) . Equals ( title , StringComparison . OrdinalIgnoreCase ) ) ;
38
48
}
49
+
50
+ static public bool SetForegroundWindowByTitle ( string title )
51
+ {
52
+ try
53
+ {
54
+ var client = Process . GetProcessesByName ( title ) [ 0 ] ;
55
+ return NativeMethods . SetForegroundWindow ( client . MainWindowHandle ) != 0 ;
56
+ }
57
+ catch ( Exception ex )
58
+ {
59
+ Logger . Error ( $ "Exception wile trying to bring '{ title } ' to the foreground.", ex ) ;
60
+ }
61
+
62
+ return false ;
63
+ }
64
+
65
+ static public bool SetForegroundWindowByHandle ( IntPtr handle )
66
+ {
67
+ if ( handle != null )
68
+ {
69
+ return NativeMethods . SetForegroundWindow ( handle ) != 0 ;
70
+ }
71
+ return false ;
72
+ }
73
+
74
+ static public bool SendEnterByTitle ( string title )
75
+ {
76
+ var windows = Process . GetProcesses ( ) ;
77
+
78
+ bool sent = false ;
79
+ foreach ( var window in windows )
80
+ {
81
+ if ( window . MainWindowTitle == title )
82
+ {
83
+ NativeMethods . SendMessage ( window . MainWindowHandle ,
84
+ NativeMethods . WM_KEYDOWN , NativeMethods . VK_RETURN , IntPtr . Zero ) ;
85
+
86
+ Logger . Information ( $ "Sending Enter to '{ window . MainWindowTitle } '") ;
87
+ sent = true ;
88
+ }
89
+ }
90
+
91
+ return sent ;
92
+ }
93
+
94
+ static public void SendEnterByHandle ( IntPtr handle )
95
+ {
96
+ if ( handle == IntPtr . Zero )
97
+ {
98
+ Logger . Error ( $ "Givne null handle. aborting...") ;
99
+ return ;
100
+ }
101
+
102
+ Logger . Information ( "Sending enter key to window" ) ;
103
+ NativeMethods . SendMessage ( handle , NativeMethods . WM_KEYDOWN , NativeMethods . VK_RETURN , IntPtr . Zero ) ;
104
+ }
39
105
}
40
106
}
0 commit comments