例如,如果用户当前正在运行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