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 :

Api Windows : GetWindowText


Sujet :

C#

  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2007
    Messages
    677
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2007
    Messages : 677
    Par défaut Api Windows : GetWindowText
    Bonjour à tous,
    Je viens vous embêter car après deux bonnes heures de prise de tête, je ne vois pas l’issue à mon problème.

    Le contexte :
    - J’ai un Notepad d’ouvert, dans lequel j’ai tapé du texte.
    - Je récupère le bon Handle de mon Notepad (ça je sais faire, et Spy++ me le confirme).
    - Je récupère le bon Handle de la zone de saisie de texte (ça aussi j’y arrive, Spy++ me le confirme également, et récupère bien le texte que j’ai saisi dans le susnommé Notepad).
    - Je tente donc de récupérer, via le Handle de ma zone de saisie, le texte que cette dernière contient (sans succès, et c’est à n’y rien comprendre).

    Un morceau de code (je vous fais grâce des déclaration concernant l’api) :

    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
        class Program
        {
            #region WinAPI
     
    [...]
     
            #endregion
     
            static void Main(string[] args)
            {
                IntPtr win      = GetWindow("Notepad");
                IntPtr control  = GetControl(win, "Edit");
     
                string text     = GetText(control);
     
                Console.WriteLine(text);
            }
     
            public static IntPtr GetWindow(string className)
            { return FindWindow(className, null); }
     
            public static IntPtr GetControl(IntPtr hWnd, string className)
            { return FindWindowEx(hWnd, IntPtr.Zero, className, IntPtr.Zero); }
     
            public static string GetText(IntPtr hWnd)
            {
                int length = GetWindowTextLength(hWnd);
     
                StringBuilder sb = new StringBuilder(length + 1);
                GetWindowText(hWnd, sb, sb.Capacity);
                return sb.ToString();
            }
        }
    Ma fonction GetText(IntPtr hWnd) fonctionne parfaitement sur le Handle de ma fenêtre (il me retourne le titre de ma fenêtre en somme), mais rien à faire sur le control « Edit » qu’elle contient (et je le répète, le Handle de « Edit » est pourtant tout à fait correct).
    Où est mon erreur ? je vénèrerai mon sauveur =D


    (ps : j'ai vu -grace à mon ami google- des bribes de conversation faisant référence à 'sendMessage' ou encore 'GetDlgItemText', mais à part tourner en rond, je n'ai pas réussi à aboutir à quoi que ce soit).

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2007
    Messages
    677
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2007
    Messages : 677
    Par défaut
    Le code au complet (histoire que mes potentiels sauveurs n'aient plus qu'à copier-coller, ouvrir un notepad, et exécuter).

    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
    namespace Test
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Runtime.InteropServices;
     
        class Program
        {
            #region WinAPI
     
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
     
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern int GetWindowTextLength(IntPtr hWnd);
     
            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
     
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
     
            #endregion
     
            static void Main(string[] args)
            {
                IntPtr win      = GetWindow("Notepad");
                IntPtr control  = GetControl(win, "Edit");
     
                string text     = GetText(control);
     
                Console.WriteLine(text);
            }
     
            public static IntPtr GetWindow(string className)
            { return FindWindow(className, null); }
     
            public static IntPtr GetWindow(string className, string windowName)
            { return FindWindow(className, windowName); }
     
            public static IntPtr GetControl(IntPtr hWnd, string className)
            { return FindWindowEx(hWnd, IntPtr.Zero, className, IntPtr.Zero); }
     
            public static string GetText(IntPtr hWnd)
            {
                int length = GetWindowTextLength(hWnd);
     
                StringBuilder sb = new StringBuilder(length + 1);
                GetWindowText(hWnd, sb, sb.Capacity);
                return sb.ToString();
            }
        }
    }

  3. #3
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Citation Envoyé par MSDN
    The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.
    Donc GetWindowText ne peut pas marcher dans ton cas. Par contre, la doc dit aussi :
    Citation Envoyé par MSDN
    To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.
    Donc en envoyant un WM_GETTEXT, ça devrait le faire, en déclarant SendMessage comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern SendMessage(IntPtr hWnd, int msg, int maxLength, StringBuilder buffer);

Discussions similaires

  1. Réponses: 29
    Dernier message: 14/01/2013, 10h40
  2. tutoriel : La programmation de l'API Windows en C++ par Bob
    Par Aurelien.Regat-Barrel dans le forum Windows
    Réponses: 19
    Dernier message: 21/06/2008, 14h34
  3. Documentation gratuite sur l'API Windows, COM, DCOM, OLE, etc.
    Par Community Management dans le forum Windows
    Réponses: 1
    Dernier message: 16/11/2006, 15h28
  4. [API Windows] Polices de caractères disponibles
    Par bebeours dans le forum C++Builder
    Réponses: 3
    Dernier message: 05/11/2003, 08h28
  5. Utilisation de Pointeurs dans API windows
    Par Drooxy dans le forum API, COM et SDKs
    Réponses: 4
    Dernier message: 13/03/2003, 22h39

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