Avatar billede biohazard_666 Nybegynder
28. januar 2008 - 19:02 Der er 1 kommentar og
1 løsning

Find flere hWnd efter titel/window-class

Hejsa Eksperter :)

Jeg prøver at finde hWnd for 2 programmer. Programmerne har den sammen window-class og samme titel, men jeg kan stadig kun finde hWnd for en af programmerne med den kode jeg har fundet. Jeg har forgæves forsøgt at rette programmet til, og søger derfor efter hjælp til, hvordan jeg ellers kan finde de 2 hWnd's! :)


Hvis der er nogen, som har et forslag til hvordan dette kan findes (helst ud fra window-class), så vil jeg gladeligt tage imod hjælp :D


Mvh
Lars
Avatar billede biohazard_666 Nybegynder
29. januar 2008 - 12:13 #1
fandt ud af det :) Poster koden lidt senere
Avatar billede biohazard_666 Nybegynder
29. januar 2008 - 14:10 #2
ok her er koden:

Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const GWW_ID = (-12)

Private Declare Function FindWindow Lib "user32" _
  Alias "FindWindowA" _
  (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
 
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindow Lib "user32" _
  (ByVal hWnd As Long, _
  ByVal wCmd As Long) As Long

Private Declare Function GetWindowText Lib "user32" _
  Alias "GetWindowTextA" _
  (ByVal hWnd As Long, _
  ByVal lpString As String, _
  ByVal cch As Long) As Long

Private Declare Function GetClassName Lib "user32" _
  Alias "GetClassNameA" _
  (ByVal hWnd As Long, _
  ByVal lpClassName As String, _
  ByVal nMaxCount As Long) As Long

Public Function FindWindowLike(hWndArray() As Long, _
                                ByVal hWndStart As Long, _
                                WindowText As String, _
                                Classname As String) As Long
 
  Dim hWnd As Long
  Dim sWindowText As String
  Dim sClassname As String
  Dim r As Long
     
  'Hold the level of recursion and
  'hold the number of matching windows
  Static level As Long
  Static found As Long
 
  'Initialize if necessary
  If level = 0 Then
    found = 0
    ReDim hWndArray(0 To 0)
    If hWndStart = 0 Then hWndStart = GetDesktopWindow()
  End If
 
  'Increase recursion counter
  level = level + 1
 
  'Get first child window
  hWnd = GetWindow(hWndStart, GW_CHILD)

  Do Until hWnd = 0
     
      'Search children by recursion
      Call FindWindowLike(hWndArray(), hWnd, WindowText, Classname)
     
      'Get the window text and class name
      sWindowText = Space(255)
      r = GetWindowText(hWnd, sWindowText, 255)
      sWindowText = Left(sWindowText, r)
       
      sClassname = Space(255)
      r = GetClassName(hWnd, sClassname, 255)
      sClassname = Left(sClassname, r)
     
      'Check that window matches the search parameters
      If (sWindowText Like WindowText) And (sClassname Like Classname) Then
       
          found = found + 1
          ReDim Preserve hWndArray(0 To found)
          hWndArray(found) = hWnd
          'List1.AddItem hWndArray(found) & vbTab & sClassname & vbTab & sWindowText
          List1.AddItem hWndArray(found)
          ReDim Preserve hWnd_list(found)
          hWnd_list(found) = hWndArray(found)

      End If
     
      'Get next child window
      hWnd = GetWindow(hWnd, GW_HWNDNEXT)
       
  Loop
 
  'Decrement recursion counter
  level = level - 1
 
  'Return the number of windows found
    FindWindowLike = found

End Function

Og funktionen kaldes således:
Call FindWindowLike(hWnds(), 0, strWindowTitle, strWindowClass)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester