IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Trouver des infos sur une fenêtre à partir de son handle


Sujet :

C#

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 6
    Points : 7
    Points
    7
    Par défaut Trouver des infos sur une fenêtre à partir de son handle
    J'aurais besoin de connaître quelques infos (titre, position) d'une fenêtre dont je connais un nom partiel.
    J'ai trouvé un code pour récupérer le handle de cette fenêtre, est-ce qu'à partir de ce handle je peux obtenir ces infos ?
    Ou est-ce qu'il y a un moyen plus simple de faire ça ?



    Pour info, la façon dont je récupère le Hnwd :

    Pour le code, j'ai trouvé ça :http://www.pinvoke.net/default.aspx/user32.enumwindows (je copie / colle l'exemple "Oftentimes you'll want to have the EnumProc retrieve some data from a particular window and return it.").

    Voilà ce que j'utilise pour récupérer le Hnwd :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    using System.Runtime.InteropServices;
    using System.Text;
     
    public class WndSearcher
    {
        public static IntPtr SearchForWindow(string wndclass, string title)
        {
            SearchData sd = new SearchData { Wndclass=wndclass, Title=title };
            EnumWindows(new EnumWindowsProc(EnumProc), ref sd);
            return sd.hWnd;
        }
     
        public static bool EnumProc(IntPtr hWnd, ref SearchData data)
        {
            // Check classname and title
            // This is different from FindWindow() in that the code below allows partial matches
            StringBuilder sb = new StringBuilder(1024);
            GetClassName(hWnd, sb, sb.Capacity);
            if (sb.ToString().StartsWith(data.Wndclass))
            {
                sb = new StringBuilder(1024);
                GetWindowText(hWnd, sb, sb.Capacity);
                if (sb.ToString().StartsWith(data.Title))
                {
                    data.hWnd = hWnd;
                    return false;    // Found the wnd, halt enumeration
                }
            }
            return true;
        }
     
        public class SearchData
        {
            // You can put any dicks or Doms in here...
            public string Wndclass;
            public string Title;
            public IntPtr hWnd;
        }
     
        private delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data);
     
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref SearchData data);
     
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
     
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
    }
     
    //Then you'd call:
     
    // If you're viewing this page with IE, this *should* return the hwnd of the browser
    IntPtr hWnd = WndSearcher.SearchForWindow("IEFrame", "pinvoke.net: EnumWindows");
    Merci d'avance !

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    bonjour,

    Je ne comprends pas bien ta question: tu obtiens déjà le titre de la fenêtre puisque c'est ce que tu vérifie pour trouver cette fenêtre ^^.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GetWindowText(hWnd, sb, sb.Capacity);
    Pour la position/taille, tu fais à peu près la même chose mais avec la fonction GetWindowPlacement, ou GetWindowRect.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 6
    Points : 7
    Points
    7
    Par défaut
    Citation Envoyé par JohnDoe57 Voir le message
    bonjour,

    Je ne comprends pas bien ta question: tu obtiens déjà le titre de la fenêtre puisque c'est ce que tu vérifie pour trouver cette fenêtre ^^.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GetWindowText(hWnd, sb, sb.Capacity);
    Pour la position/taille, tu fais à peu près la même chose mais avec la fonction GetWindowPlacement, ou GetWindowRect.
    Merci, j'avais du mal à comprendre comment ça marchait. Je tentais de récupérer sb.Title, et je n'obtenais que le nom partiel que je donnais...
    J'ai rajouté un data.FullTitle = sb.ToString() quand il vient de trouver la fenêtre et comme ça je peux récupérer le titre.

    Merci !!!

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    J'ai rajouté un data.FullTitle = sb.ToString() quand il vient de trouver la fenêtre et comme ça je peux récupérer le titre.
    Oui c'est ce que j'avais suggéré, avant de modifier mon message, je disais que tu pouvais ajouter des éléments au type SearchData, et modifier la fonction SearchForWindow pour qu'elle retourne un object SearchData, mais que personnellement je déconseillais cette méthode puisque je suis plutôt du genre à économiser les ressources dans mes programmes, autant utiliser les fonctions GetWindowXXX seulement quand tu en as besoin.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AJAX] Avoir des infos sur une page web
    Par Skieur38 dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 07/03/2007, 21h38
  2. Réponses: 10
    Dernier message: 15/10/2006, 17h23
  3. Récupérer des infos sur une page externe
    Par Horrigan dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 20/07/2006, 14h46
  4. Obtenir des infos sur une page web en ligne
    Par Logan_Cale dans le forum Web & réseau
    Réponses: 1
    Dernier message: 20/08/2005, 15h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo