C# 对程序窗口进程和进程ID
生活随笔
收集整理的這篇文章主要介紹了
C# 对程序窗口进程和进程ID
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
獲取當(dāng)前激活窗口(頂置)
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:窗口句柄。?
獲取進(jìn)程和線程ID
GetWindowThreadProcessId
函數(shù)原型:
DWORD?GetWindowThreadProcessId(HWND?hWnd,LPDWORD?lpdwProcessId);
參數(shù)說明:
hWnd:傳入的窗口句柄;lpdwProcessId:返回的進(jìn)程ID地址。
返回值:
函數(shù)返回的是窗口所屬線程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可以獲取相應(yīng)的屬性,如名字,titile。
var proc = Process.GetProcessById((int)procId);string titleName = proc.MainWindowTitle;?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的C# 对程序窗口进程和进程ID的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: powershell /遍历/psobj
- 下一篇: C# 快捷键/hotkey简单例子