Aide sur EnumWindows + FindWindow
Bonjour à tous,
j'ai un soucis avec la class EnumWindows et FindWindow que voici :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| class ClassEW
{
private IntPtr _hwdn;
private string _name;
private static List<ClassEW> _windows = new List<ClassEW>();
public IntPtr HWND { get { return this._hwdn; } }
public string Name { get { return this._name; } }
public ClassEW(IntPtr hwnd, string name)
{
this._hwdn = hwnd;
this._name = name;
}
public override string ToString()
{
return this._name;
}
private static bool EnumWindows(IntPtr hwnd, int lparam)
{
if (!string.IsNullOrEmpty(NativeMethods.GetWindowText()))
{
ClassEW window = new ClassEW(hwnd, NativeMethods.GetWindowText(hwnd));
_windows.Add(window);
}
return true;
}
public static List<ClassEW> GetWindowsList()
{
NativeMethods.EnumWindows(new NativeMethods.EnumWindowsCallBack(EnumWindows), 0);
return _windows;
}
internal static class NativeMethods
{
public delegate bool EnumWindowsCallBack(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
internal static extern int EnumWindows(EnumWindowsCallBack lpEnumFunc, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
internal static string GetWindowText((IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(GetWindowTextLength(hWnd)+ 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
}
} |
Je fait appel a la fonction GetWindowsList pour ensuite l'afficher dans une ListView comme ceci
Code:
1 2 3 4 5
| foreach (ClassEW A in ClassEW.GetWindowsList())
{
ListViewItem LVI = ListView.Items.Add(A.HWND.ToString());
LVI.SubItems.Add(A.Name);
} |
Je voudrais faire en sorte de lister seulement un seul processus par la "ClassName" pour voir ainsi les différentes instance de se processus. Pour cela j'ai rajouter la fonction "FindWindow", mais après plusieurs tests je n'y arrive pas, je ne sais pas ou et comment place cette dernière fonction.
Pourriez vous m'aider ?
PS : J'utilise cette API pour afficher correctement le titre de l'application, même en état réduit.
Bien cordialement.