獲取當前激活窗口(頂置)
GetForegroundWindow()
[DllImport("user32.dll")]public static extern IntPtr GetForegroundWindow();[DllImport("user32.dll", EntryPoint = "GetWindowText")]public static extern int GetWindowText(int hwnd,string lpString,int cch);
IntPtr hWnd = GetForegroundWindow();//hWnd:窗口句柄。
?
獲取進程和線程ID
GetWindowThreadProcessId
函數原型:
DWORD?GetWindowThreadProcessId(HWND?hWnd,LPDWORD?lpdwProcessId);
參數說明:
hWnd:傳入的窗口句柄;lpdwProcessId:返回的進程ID地址。
返回值:
函數返回的是窗口所屬線程ID。
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
uint procId = 0;uint id = GetWindowThreadProcessId(hWnd, out procId);
?
獲取process
public static System.Diagnostics.Process GetProcessById (int processId);
通過process可以獲取相應的屬性,如名字,titile。
var proc = Process.GetProcessById((int)procId);string titleName = proc.MainWindowTitle;
?
?