GetWindowTextA

Résolu
romainvv Messages postés 46 Date d'inscription lundi 19 mars 2007 Statut Membre Dernière intervention 22 mai 2009 - 11 janv. 2008 à 19:58
romainvv Messages postés 46 Date d'inscription lundi 19 mars 2007 Statut Membre Dernière intervention 22 mai 2009 - 11 janv. 2008 à 23:53
Bonjour,
Je finalise mon projet et je rencontre une difficulté. Mon application doit etre sensible au fenetre qui ont le focus.
J'aimerais donc que le progamme prenne note des changements de focus. Pour cela j'ai trouvé un bout de code sur lequel m'inspirer mais c'est du VB 6 et pas moyen de le faire fonctioner sur VB 2005.

Public Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Function GetCaption(hWnd As Long)
Dim hWndlength As Long, hWndTitle As String, a As Long
hWndlength = GetWindowTextLength(hWnd)
hWndTitle = String$(hWndlength, 0)
a = GetWindowText(hWnd, hWndTitle, (hWndlength + 1))
GetCaption = hWndTitle
End Function

ensuite je vérifie

If Not Wind = GetCaption(GetForegroundWindow) Then
Wind = GetCaption(GetForegroundWindow)
GetKey = vbCrLf &  " - " & Time & " --< " & Wind & " >--
"
End If

J'ai une erreur avec la ligne hWndTitle = String$(hWndlength, 0) le "string$" pose probleme.
Voici la définition que VB6 me donne pour string$ "Renvoie une chaîne formée du nombre spécifié de répétitions du même caractère"

Auriez vous une solution ?
Merci

5 réponses

Utilisateur anonyme
11 janv. 2008 à 20:03
Salut,

Les long sont à remplacer par des integers et les handles pas des IntPtr







__________
  Kenji
3
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
11 janv. 2008 à 20:40
salut
(Kenji corrige moi si besoin..)

çà devrait donner quelquechose comme çà :

Public Class Form1
    Private Declare Function GetForegroundWindow Lib "user32.dll" () As Integer
    Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Integer) As Integer

    Function GetCaption(ByVal hWnd As IntPtr) As String
        Dim lRet As Integer
        Dim sBuffer As String = String.Empty

        lRet = GetWindowTextLength(hWnd) 'nombre de caractères
        sBuffer = sBuffer.PadRight(lRet + 1, " ")  'buffer
        GetWindowText(hWnd, sBuffer, lRet + 1) 'récupère la variable
        Return sBuffer.Substring(0, lRet) 'retourne la bonne chaîne
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.WindowState = FormWindowState.Minimized
        Application.DoEvents()
        Debug.Print(GetCaption(GetForegroundWindow))
    End Sub
End Class

++
Prenez un instant pour répondre à [infomsg_SONDAGE-POP3-POUR-CS_769706.aspx ce sondage] svp
3
Utilisateur anonyme
11 janv. 2008 à 21:10
Il manque juste les IntPtr dans les déclarations, autrement, c'est parfait

Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As IntPtr) As Integer
__________
  Kenji
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
11 janv. 2008 à 22:22
merci, je retiendrai
(pourtant tu l'avais bien dit : TOUS les handle en intptr!!)
y'a rien à faire, j'arrive pas à m'y mettre à ce dotnet
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
romainvv Messages postés 46 Date d'inscription lundi 19 mars 2007 Statut Membre Dernière intervention 22 mai 2009
11 janv. 2008 à 23:53
Mon dieu ca fonctionne merci beaucoup
0
Rejoignez-nous