-
Notifications
You must be signed in to change notification settings - Fork 25
/
NativeUtils.cs
41 lines (33 loc) · 1.09 KB
/
NativeUtils.cs
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
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WinForm_Ollama_Copilot
{
internal class NativeUtils
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static IntPtr getForegroundWindow()
{
return GetForegroundWindow();
}
public static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
}
}