例如,如果用戶當前正在運行VS2008,那麼我需要VS2008的值。如何確定C#中當前重點進程的名稱
4
A
回答
9
我假設你想獲得擁有當前焦點窗口的進程的名稱。對於一些的P/Invoke:
// The GetForegroundWindow function returns a handle to the foreground window
// (the window with which the user is currently working).
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
// The GetWindowThreadProcessId function retrieves the identifier of the thread
// that created the specified window and, optionally, the identifier of the
// process that created the window.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// Returns the name of the process owning the foreground window.
private string GetForegroundProcessName()
{
IntPtr hwnd = GetForegroundWindow();
// The foreground window can be NULL in certain circumstances,
// such as when a window is losing activation.
if (hwnd == null)
return "Unknown";
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.Id == pid)
return p.ProcessName;
}
return "Unknown";
}
+0
當前景窗口更改時,我可以收到事件通知嗎? – manishKungwani 2011-07-13 10:30:49
+0
@manishKungwani:你應該問這是一個新問題。 – 2011-07-13 11:16:54
0
using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FGHook
{
class ForegroundTracker
{
// Delegate and imports from pinvoke.net:
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
static extern bool UnhookWinEvent(IntPtr hWinEventHook);
// Constants from winuser.h
const uint EVENT_SYSTEM_FOREGROUND = 3;
const uint WINEVENT_OUTOFCONTEXT = 0;
// Need to ensure delegate is not collected while we're using it,
// storing it in a class field is simplest way to do this.
static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
public static void Main()
{
// Listen for foreground changes across all processes/threads on current desktop...
IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
// MessageBox provides the necessary mesage loop that SetWinEventHook requires.
MessageBox.Show("Tracking focus, close message box to exit.");
UnhookWinEvent(hhook);
}
static void WinEventProc(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Console.WriteLine("Foreground changed to {0:x8}", hwnd.ToInt32());
//Console.WriteLine("ObjectID changed to {0:x8}", idObject);
//Console.WriteLine("ChildID changed to {0:x8}", idChild);
GetForegroundProcessName();
}
static void GetForegroundProcessName()
{
IntPtr hwnd = GetForegroundWindow();
// The foreground window can be NULL in certain circumstances,
// such as when a window is losing activation.
if (hwnd == null)
return;
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.Id == pid)
{
Console.WriteLine("Pid is: {0}",pid);
Console.WriteLine("Process name is {0}",p.ProcessName);
return;
}
//return;
}
Console.WriteLine("Unknown");
}
}
}
相關問題
- 1. 如何在jboss應用程序服務器中獲取當前進程名稱,節點名稱
- 2. 如何確定當前進程中COM使用的內部HWND?
- 3. 我怎樣才能確定當前關注的流程名稱和版本C#
- 4. 如何獲取erlang集羣中的當前節點名稱?
- 5. 如何讓Python進程以正確的進程名稱運行?
- 6. 如何更改進程名稱objective-c
- 7. 在C++中使用其進程名稱進入前窗口
- 8. 獲取當前進程中所有句柄的名稱
- 9. 如何確定當前運行的進程是否爲父進程?
- 10. 如何指定AddIn進程名稱?
- 11. 如何在Perl中獲取當前子程序的名稱?
- 12. 如何確定進程是否爲當前活動/前臺應用程序
- 13. Java:DOM如何從名稱列表中重命名節點名稱
- 14. 如何確定Android中的當前IME?
- 15. 如何使用C#中的Skype4COM API獲得重點組名稱
- 16. 如何定義,在C#中稱爲當前窗體的形式?
- 17. C#:找到當前進程
- 18. 確定當前的域控制器名稱
- 19. 如何確定R中的當前目錄名?
- 20. 如何獲取C#中的開放端口的進程名稱?
- 21. 獲取當前JSON節點的完全限定名稱
- 22. C#WMI如何獲取當前audiodevice的名稱
- 23. 如何確定C++中的當前流類型?
- 24. 如何確定C++/CLI中的當前編譯目標?
- 25. 如何獲取當前選定的Ajax選項卡的名稱
- 26. 確定當前應用程序的父進程
- 27. 如何確定當前的應用程序在前臺?
- 28. 在C#中更改進程名稱?
- 29. T-SQL--被稱爲當前進程
- 30. 在ASP.NET MVC中確定部分視圖中當前視圖的名稱
的過程VS 2008是devenv.exe的。你認爲你可以從哪裏得到「VS2008」? – JRoppert 2008-09-18 21:44:08