Results 1 to 8 of 8

Thread: Close All Open Windows

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Connecticut, USA
    Posts
    308

    Close All Open Windows

    There are times when I have to many things open that my desktop gets cluttered. Is there a way to close everything that is open?

  2. #2
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    1. Log off, log back in.

    2. Minimize All Windows, they're still there but no clutter.
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Connecticut, USA
    Posts
    308
    Thanks TiPeRa, but I'm looking for something a bit more "instant".

    I don't want to have to log off. I just want to close everything.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I have a question.

    How do you want to do this?

    Are you looking for something intrinsic in Windows to do this for you, or do you want to write a VB program to do this for you?

    If your direction is your own program, where would that be?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Connecticut, USA
    Posts
    308
    Well...wouldn't it be nice if you had something that you could just click on and it would do it?

  6. #6
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Use the Enum Window API function to get a list of all windows. Then you're probably only going to want to close windows that are visible, so use the IsWindowVisible API function against each HWND in the callback. THEEEEEEEEEEN if the window IS visible, send it a WM_CLOSE message via SendMessage.
    Please rate my post.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Part One

    I have to give you this in two parts, because there is a 10,000 character limit on individual posts (I know because I've exceeded it before). First: Start a new project, and paste this into the declarations section of Form1
    VB Code:
    1. Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    2.  
    3. Private Type NOTIFYICONDATA
    4.     cbSize As Long
    5.     hWnd As Long
    6.     uId As Long
    7.     uFlags As Long
    8.     ucallbackMessage As Long
    9.     hIcon As Long
    10.     szTip As String * 64
    11. End Type
    12.  
    13. Private Const NIM_ADD = &H0
    14. Private Const NIM_MODIFY = &H1
    15. Private Const NIM_DELETE = &H2
    16. Private Const WM_MOUSEMOVE = &H200
    17. Private Const NIF_MESSAGE = &H1
    18. Private Const NIF_ICON = &H2
    19. Private Const NIF_TIP = &H4
    20. Private Const WM_LBUTTONDBLCLK = &H203
    21. Private Const WM_LBUTTONDOWN = &H201
    22. Private Const WM_LBUTTONUP = &H202
    23. Private Const WM_RBUTTONDBLCLK = &H206
    24. Private Const WM_RBUTTONDOWN = &H204
    25. Private Const WM_RBUTTONUP = &H205
    26.  
    27. Private SysTray As NOTIFYICONDATA
    28.  
    29. Private Sub StartInSysTray()
    30.     SysTray.cbSize = Len(SysTray)
    31.     SysTray.hwnd = Picture1.hwnd
    32.     SysTray.uId = 1&
    33.     SysTray.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    34.     SysTray.ucallbackMessage = WM_MOUSEMOVE
    35.     SysTray.hIcon = Me.Icon
    36.     SysTray.szTip = "&R&e&s&t&a&r&t" & Chr$(0)
    37.     Shell_NotifyIcon NIM_ADD, SysTray
    38.     Me.Hide
    39.     App.TaskVisible = False
    40. End Sub
    41.  
    42. Private Sub Form_Load()
    43. StartInSysTray
    44. End Sub
    45.  
    46. Private Sub Form_Unload(Cancel As Integer)
    47.     SysTray.cbSize = Len(SysTray)
    48.     SysTray.hwnd = Picture1.hwnd
    49.     SysTray.uId = 1&
    50.     Shell_NotifyIcon NIM_DELETE, SysTray
    51. End Sub
    52.  
    53. 'Add A Picture Control To The Form.   Set its visible property to False
    54. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    55.  
    56. Static rec As Boolean, msg As Long
    57.     Dim RetVal As String
    58.     Dim returnstring
    59.     Dim retvalue
    60.     msg = X / Screen.TwipsPerPixelX
    61.     If rec = False Then
    62.         rec = True
    63.     Select Case msg
    64.     'here are the various mouse events that you can use once
    65.     'you application is in the system tray.   select the event of your
    66.     'choice and put CloseExceptUs there.   (CloseExceptUs is a
    67.     'routine that I will post momentarily.   It also needs to go on
    68.     'this form.)
    69.     Case WM_LBUTTONDOWN
    70.  
    71.     Case WM_LBUTTONDBLCLK
    72.  
    73.     Case WM_LBUTTONUP
    74.  
    75.     Case WM_RBUTTONUP
    76.  
    77.     End Select
    78.         rec = False
    79.     End If
    80.  
    81. End Sub

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Part Two (Say: Thank You Nucleus)

    VB Code:
    1. Public Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    2. Public Const SMTO_BLOCK = &H1
    3. Public Const SMTO_ABORTIFHUNG = &H2
    4. Public Const SC_CLOSE = &HF060&
    5. Public Const WM_SYSCOMMAND = &H112
    6. Public Const WM_NULL = &H0
    7.  
    8. Public Declare Function IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
    9. Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
    10. Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    11. Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    12.  
    13. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    14. Const PROCESS_ALL_ACCESS = &H1F0FFF
    15.  
    16. Public Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
    17. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    18. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    19.  
    20. Public m_hTaskBar          As Long
    21. Public m_hDeskTopIcons     As Long
    22.  
    23. Public Sub CloseExceptUs()
    24.     ';->';->';->';->';->';->';->';->';->';->';->';->';->';->
    25.     ' Author:   Nucleus                                     *
    26.     ' Location: VB World Forum                              *
    27.     ' Purpose:  Close all applications except this one      *
    28.     ';->';->';->';->';->';->';->';->';->';->';->';->';->';->
    29.     m_hDeskTopIcons = FindWindowEx(0&, 0&, "Progman", vbNullString)
    30.     m_hTaskBar = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
    31.     EnumWindows AddressOf EnumWindowsProc, 0&
    32. End Sub
    33.  
    34. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    35. Dim lThreadID               As Long
    36. Dim lPid                    As Long
    37. Dim lHp                     As Long
    38. '
    39. ' If the window is not desktop icons or taskabar
    40. '
    41. If hwnd <> m_hTaskBar And hwnd <> m_hDeskTopIcons Then
    42.     '
    43.     ' Get ThreadID and Process Id from hwnd
    44.     '
    45.     lThreadID = GetWindowThreadProcessId(hwnd, lPid)
    46.     '
    47.     ' If the ThreadId is not from this application
    48.     '
    49.     If lThreadID <> App.ThreadID Then
    50.         '
    51.         ' Check if the window is visible
    52.         '
    53.         If IsWindowVisible(hwnd) Then
    54.             '
    55.             ' Tell the window to close gently, give it a timeout in case it does not respond
    56.             '
    57.             SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    58.             '
    59.             ' If the window doesn't close via gently persuasion, bring out the nipple screws to force it to close
    60.             '
    61.             If IsWindow(hwnd) Then
    62.                 lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    63.                 TerminateProcess lHp&, 0&
    64.                 CloseHandle lHp
    65.             End If
    66.             '
    67.         End If
    68.         '
    69.     End If
    70.     '
    71. End If
    72.  
    73. EnumWindowsProc = 1
    74.  
    75. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width