home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD78987182000.psc / modTaskStuff.bas < prev    next >
Encoding:
BASIC Source File  |  2000-07-17  |  3.9 KB  |  96 lines

  1. Attribute VB_Name = "modTaskStuff"
  2. Option Explicit
  3.  
  4. ' API Constants
  5. Const WS_MINIMIZE = &H20000000 ' Style bit 'is minimized'
  6. Const HWND_TOP = 0 ' Move to top of z-order
  7. Const SWP_NOSIZE = &H1 ' Do not re-size window
  8. Const SWP_NOMOVE = &H2 ' Do not reposition window
  9. Const SWP_SHOWWINDOW = &H40 ' Make window visible/active
  10. Const GW_HWNDFIRST = 0 ' Get first Window handle
  11. Const GW_HWNDNEXT = 2 ' Get next window handle
  12. Const GWL_STYLE = (-16) ' Get Window's style bits
  13. Const SW_RESTORE = 9 ' Restore window
  14.  
  15. ' The following constants will be combined to define properties
  16. ' of a 'normal' task top-level window. Any window with ' these set will be
  17. ' included in the list:
  18. Const WS_VISIBLE = &H10000000 ' Window is not hidden
  19. Const WS_BORDER = &H800000 ' Window has a border
  20. ' Other bits that are normally set include:
  21. Const WS_CLIPSIBLINGS = &H4000000 ' can clip windows
  22. Const WS_THICKFRAME = &H40000 ' Window has thick border
  23. Const WS_GROUP = &H20000 ' Window is top of group
  24. Const WS_TABSTOP = &H10000 ' Window has tabstop
  25.  
  26. ' API Functions Definition
  27. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  28. Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
  29. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  30. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  31. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
  32. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  33. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  34.  
  35. ' Public Task Item Structure
  36. Public Type TASK_STRUCT
  37.     TaskName As String
  38.     TaskID As Long
  39. End Type
  40.  
  41. 'Structure filled by FillTaskList Sub call
  42. Public TaskList(1000) As TASK_STRUCT
  43. Public NumTasks As Long
  44.  
  45. ' Returns if a Process is a Visible Window
  46. Public Function IsTask(hwndTask As Long) As Boolean
  47.     Dim WndStyle As Long
  48.     Const IsTaskStyle = WS_VISIBLE Or WS_BORDER
  49.  
  50.     WndStyle = GetWindowLong(hwndTask, GWL_STYLE)
  51.     If (WndStyle And IsTaskStyle) = IsTaskStyle Then IsTask = True
  52. End Function
  53.  
  54. ' Fills the Task structure with captions and hWnd of all active programs
  55. Public Sub FillTaskList(hwnd As Long)
  56.     Dim hwndTask As Long
  57.     Dim intLen As Long
  58.     Dim strTitle As String
  59.     Dim cnt As Integer
  60.  
  61.     cnt = 0
  62.     ' process all top-level windows in master window list
  63.     hwndTask = GetWindow(hwnd, GW_HWNDFIRST) ' get first window
  64.     Do While hwndTask ' repeat for all windows
  65.         If hwndTask <> hwnd And IsTask(hwndTask) Then
  66.             intLen = GetWindowTextLength(hwndTask) + 1 ' Get length
  67.             strTitle = Space(intLen) ' Get caption
  68.             intLen = GetWindowText(hwndTask, strTitle, intLen)
  69.             If intLen > 0 Then ' If we have anything, add it
  70.                 TaskList(cnt).TaskName = strTitle
  71.                 TaskList(cnt).TaskID = hwndTask
  72.                 cnt = cnt + 1
  73.             End If
  74.         End If
  75.         hwndTask = GetWindow(hwndTask, GW_HWNDNEXT)
  76.     Loop
  77.     NumTasks = cnt
  78. End Sub
  79.  
  80. ' Give focus to another Task
  81. Public Sub SwitchTo(hwnd As Long)
  82.     Dim ret As Long
  83.     Dim WStyle As Long ' Window Style bits
  84.  
  85.    ' Get style bits for window
  86.     WStyle = GetWindowLong(hwnd, GWL_STYLE)
  87.     ' If minimized do a restore
  88.     If WStyle And WS_MINIMIZE Then
  89.         ret = ShowWindow(hwnd, SW_RESTORE)
  90.     End If
  91.     ' Move window to top of z-order/activate; no move/resize
  92.     ret = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
  93. End Sub
  94.  
  95.  
  96.