C# 对程序窗口进程和进程ID
生活随笔
收集整理的這篇文章主要介紹了
C# 对程序窗口进程和进程ID
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取當前激活窗口(頂置)
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;?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的C# 对程序窗口进程和进程ID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: powershell /遍历/psobj
- 下一篇: C# 快捷键/hotkey简单例子