(Foreground,也就是持有Focus的視窗)
這時候可以透過Win32的API來取得相關的資訊
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(
IntPtr hWnd, out int lpdwProcessId);
string GetForegroundApplicationName()
{
/// get foreground window handle
IntPtr h = GetForegroundWindow();
/// get foreground process from handle
int pId;
GetWindowThreadProcessId(h, out pId);
Process p = Process.GetProcessById(pId);
return p.ProcessName;
}
--
參考資料
GetForegroundWindow Function
GetWindowThreadProcessId Function
Process.GetProcessById 方法 (Int32)
沒有留言:
張貼留言