GetWindowHandles and kill them

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

GetWindowHandles and kill them

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Rings.

Code: Select all

Procedure EnumerateChildren(hWndParent)
  ;Get the first child of hWndParent
  hWndChild = GetWindow_(hWndParent, #GW_CHILD | #GW_HWNDFIRST)
  While hWndChild <> 0
    sWindow.s = Space(255)
    sClass.s = Space(255)
    GetWindowText_(hWndChild, sWindow, 255) 
    GetClassName_(hWndChild, sClass, 255)
    If sWindow <> ""
      AddGadgetItem (1, -1, sWindow + "#" + sClass + "#" +
                     StrU(hWndParent, 2) + ":" + StrU(hWndChild, 2))
    EndIf 
    ;Now get any children for hWndChild
    EnumerateChildren(hWndChild)
    ;and move on to the next window
    hWndChild = GetWindow_(hWndChild, #GW_HWNDNEXT)
  Wend
EndProcedure

If OpenWindow(0, #PB_Ignore, #PB_Ignore, 500, 500,
              "Window Handles: Click Double to kill them!",
              #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListViewGadget(1, 10, 10, 480, 450)
  EnumerateChildren(GetDesktopWindow_())
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
        Case 1   ;Enable...
          If EventType() = #PB_EventType_LeftDoubleClick
            sDummy.s = GetGadgetText(1)
            ;find hwnd-handle in String
            Position = FindString(sDummy, ":", 1)
            If Position
              killhWnd = Val(Right(sDummy, Len(sDummy) - Position))
              SendMessage_(killhWnd, #WM_CLOSE, 0, 0)
              ClearGadgetItems(1)   ;reload list
              EnumerateChildren(GetDesktopWindow_()) 
            EndIf
          EndIf
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
EndIf
Its a long way to the top if you wanna .....CodeGuru