[C#]取得当前活动窗体的Title
1 [DllImport("user32.dll")]
2 static extern IntPtr GetForegroundWindow();
3
4 [DllImport("user32.dll")]
5 static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
6
7 private string GetActiveWindowTitle()
8 {
9 const int nChars = 256;
10 IntPtr handle = IntPtr.Zero;
11 StringBuilder Buff = new StringBuilder(nChars);
12 handle = GetForegroundWindow();
13
14 if (GetWindowText(handle, Buff, nChars) > 0)
15 {
16 return Buff.ToString();
17 }
18 return null;
19 }
浙公网安备 33010602011771号