Comment obtenir le nom du processus et le titre de la fenêtre supérieure sous Windows / C #

Comme dans le sujet … ou mieux – comment obtenir cette information à partir d’événements lorsque la fenêtre supérieure change?

Merci pour les conseils. Donc, je devrais utiliser P / Invoke de toute façon. Voici le code complet:

using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace CuckooCoach { class Monitor { [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hWnd); // int GetWindowText( // __in HWND hWnd, // __out LPTSTR lpSsortingng, // __in int nMaxCount // ); [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, SsortingngBuilder lpSsortingng, int nMaxCount); // DWORD GetWindowThreadProcessId( // __in HWND hWnd, // __out LPDWORD lpdwProcessId // ); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); //HANDLE WINAPI OpenProcess( // __in DWORD dwDesiredAccess, // __in BOOL bInheritHandle, // __in DWORD dwProcessId //); [DllImport("kernel32.dll")] private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId); [DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr handle); // DWORD WINAPI GetModuleBaseName( // __in HANDLE hProcess, // __in_opt HMODULE hModule, // __out LPTSTR lpBaseName, // __in DWORD nSize // ); [DllImport("psapi.dll")] private static extern uint GetModuleBaseName(IntPtr hWnd, IntPtr hModule, SsortingngBuilder lpFileName, int nSize); // DWORD WINAPI GetModuleFileNameEx( // __in HANDLE hProcess, // __in_opt HMODULE hModule, // __out LPTSTR lpFilename, // __in DWORD nSize // ); [DllImport("psapi.dll")] private static extern uint GetModuleFileNameEx(IntPtr hWnd, IntPtr hModule, SsortingngBuilder lpFileName, int nSize); public static ssortingng GetTopWindowText() { IntPtr hWnd = GetForegroundWindow(); int length = GetWindowTextLength(hWnd); SsortingngBuilder text = new SsortingngBuilder(length + 1); GetWindowText(hWnd, text, text.Capacity); return text.ToSsortingng(); } public static ssortingng GetTopWindowName() { IntPtr hWnd = GetForegroundWindow(); uint lpdwProcessId; GetWindowThreadProcessId(hWnd, out lpdwProcessId); IntPtr hProcess = OpenProcess(0x0410, false, lpdwProcessId); SsortingngBuilder text = new SsortingngBuilder(1000); //GetModuleBaseName(hProcess, IntPtr.Zero, text, text.Capacity); GetModuleFileNameEx(hProcess, IntPtr.Zero, text, text.Capacity); CloseHandle(hProcess); return text.ToSsortingng(); } } } 

Vous pouvez trouver le handle de fenêtre par nom de processus en utilisant ce code:

  Process[] processes = Process.GetProcessesByName(m.ProcessName); if (processes != null && processes.Length > 0) { Process process = processes[0]; process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.Refresh(); if (process.MainWindowHandle != IntPtr.Zero) { // process.mainwindows handle is needed for you 

et que vous pouvez trouver le texte de la fenêtre par handle title = GetWindowTitle (process.MainWindowHandle);

 private Ssortingng GetWindowTitle(IntPtr hWn) { object LParam = new object(); int WParam = 0; SsortingngBuilder title = new SsortingngBuilder(1024); SendMessage(hWn, WM_GETTEXT, WParam, LParam); GetWindowText(hWn, title, title.Capacity); return title.ToSsortingng(); } 

Vous avez besoin des déclarations suivantes pour appeler les fonctions winapi:

  [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] SsortingngBuilder lpSsortingng, int nMaxCount); [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, object lParam); 

Je ne pense pas qu’il existe une méthode .Net, je pense donc que vous utiliserez PInvoke.

Voici un exemple de code http://www.pinvoke.net/default.aspx/user32.getforegroundwindow

Et comme on peut le voir sur ce lien, il existe un projet (API Windows gérée) qui s’inscrit dans le code managé si vous ne souhaitez pas gérer vous-même le code PInvoke.

Pour le titre de la fenêtre, vous devrez faire un P / Invoke de GetWindowText avec le HWND renvoyé par GetForegroundWindow ().

En ce qui concerne les informations de processus, je pense que P / Invoquer GetWindowModuleFileName devrait fonctionner.