cs_Yanith
Messages postés34Date d'inscriptionmercredi 26 octobre 2005StatutMembreDernière intervention24 septembre 2014
-
25 oct. 2012 à 09:39
cs_Yanith
Messages postés34Date d'inscriptionmercredi 26 octobre 2005StatutMembreDernière intervention24 septembre 2014
-
28 oct. 2012 à 22:38
Bonjour à tous,
j'ai un soucis avec la class EnumWindows et FindWindow que voici :
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
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.
Utilise l'api GetClassName pendant l'énumération pour faire le test.
Par contre, tu devrai revoir le fonctionnement de ta class, c'est ce qui fais peur aux autre je pense.