Re: Determining if a process is visible on the Taskbar...
Adam Tuliper <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.advanced |
|---|---|
| Message-ID | <[email protected]> |
try this... its old vb6 code I had in a program to close taskbar pograms....
if something is missing let me know and i'll send
'Get all the tasks visible in the taskbar and then close them.
Public Sub CloseTaskBarPrograms()
Dim i As Integer
Dim lngPID As Long
Dim lngHwnd As Long
Dim frm As frmCloseProcess
Dim strPath As String
Dim lngRealDesktopHwnd As Long '1.1.71
Const ERR_SRC = "CProcess.CloseTaskBarPrograms"
1 lngRealDesktopHwnd = GetRealDesktopHwnd() '1.1.71
'This will fill in the gcolRunningPrograms dictionary with all the
visible tasks
2 EnumWindowsForTaskList
'close each window
3 For i = 1 To gcolRunningPrograms.Count
'get the handle from the collection
4 lngHwnd = CLng(gcolRunningPrograms.Item(i))
5 GetWindowThreadProcessId lngHwnd, lngPID
6 If InDebugMode Then
7 strPath = GetPathFromProcessID(lngPID)
8 frmDebug.PrintDebugString "Closing " & lngHwnd & " " & strPath
9 End If
'process found, close it
10 If lngHwnd <> lngRealDesktopHwnd Then '1.1.71 checked to make sure
we don't try to shutdown main explorer window (logoff)
11 Set frm = New frmCloseProcess
12 frm.ProcessID = lngPID
13 frm.handle = lngHwnd
14 frm.CloseProgram
LogData ERR_SRC, "Closing task bar program:" & strPath
15 End If
16 Next i
End Sub
Public Function GetRealDesktopHwnd() As Long
Dim lngProgManHwnd As Long
Dim lngShellDllDefViewHwnd As Long
Dim lngDesktop As Long
1 lngProgManHwnd = FindWindow("ProgMan", vbNullString)
2 lngShellDllDefViewHwnd = FindWindowEx(lngProgManHwnd, 0&,
"SHELLDLL_DefView", vbNullString)
3 lngDesktop = FindWindowEx(lngShellDllDefViewHwnd, 0&, "SysListView32",
vbNullString)
4 GetRealDesktopHwnd = lngDesktop
End Function
----
Attribute VB_Name = "MEnumWindow"
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal
wCmd As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long)
As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd
As Long, lpdwProcessId As Long) As Long
Private Declare Function EnumWindowsAPI Lib "user32" Alias "EnumWindows"
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd 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 Const GW_OWNER = 4
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_TOOLWINDOW = &H80
Private Const WS_EX_APPWINDOW = &H40000
'See if a passed in hwnd belongs to this program.
Public Function WindowOfThisTask(plngHwnd As Long) As Boolean
Dim i As Integer
1 For i = Forms.Count - 1 To 0 Step -1
2 If Forms(i).hWnd = plngHwnd Then
3 WindowOfThisTask = True
4 Exit Function
5 End If
6 Next i
End Function
Public Function EnumWindowsForTaskListCallBack(ByVal hWnd As Long, ByVal
lParam As Long) As Long
Dim lReturn As Long
Dim lExStyle As Long
Dim bNoOwner As Boolean
Dim sWindowText As String
'
' This callback function is called by Windows (from
' the EnumWindows API call) for EVERY window that exists.
' It populates the listbox with a list of windows that we
' are interested in.
'
' Windows to display are those that:
' - are not this app's
' - are visible
' - do not have a parent
' - have no owner and are not Tool windows OR
' have an owner and are App windows
' - Has a caption,otherwise explorer.exe (the shell instance) could
close
1 If WindowOfThisTask(hWnd) = False Then
2 If IsWindowVisible(hWnd) Then
3 If GetParent(hWnd) = 0 Then
4 bNoOwner = (GetWindow(hWnd, GW_OWNER) = 0)
5 lExStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
6 If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _
((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
'
' Get the window's caption.
'
7 sWindowText = Space$(256)
8 lReturn = GetWindowText(hWnd, sWindowText,
Len(sWindowText))
9 If Trim(sWindowText) <> "" Then
10 If InDebugMode Then frmDebug.PrintDebugString "Adding
To Close List:" & Trim(sWindowText)
'
' Add it to our list.
'
11 sWindowText = Left$(sWindowText, lReturn)
12 gcolRunningPrograms.Add hWnd 'sWindowText, hWnd
13 End If
14 End If
15 End If
16 End If
17 End If
18 EnumWindowsForTaskListCallBack = True
End Function
Public Function EnumWindowsCallBack(ByVal plngHwnd As Long, ByVal plngData
As Long) As Long
Dim lngParent As Long
Dim lngThreadId As Long
Dim lngProcessId As Long
'
' This callback function is called by Windows (from the EnumWindows
' API call) for EVERY top-level window that exists. It populates a
' collection with the handles of all parent windows owned by the
' process that we started.
'
1 EnumWindowsCallBack = 1
2 lngThreadId = GetWindowThreadProcessId(plngHwnd, lngProcessId)
3 If gdctCloseProcess.Exists(lngProcessId) Then
4 lngParent = GetParent(plngHwnd)
5 If lngParent = 0 Then
6 gcolHandle.Add plngHwnd
7 End If
8 End If
End Function
'This will return the windows that are visible in the task list.
Public Sub EnumWindowsForTaskList()
Dim hWnd As Long
'erase it
1 Set gcolRunningPrograms = Nothing
2 Set gcolRunningPrograms = New Collection
3 Call EnumWindowsAPI(AddressOf EnumWindowsForTaskListCallBack, hWnd)
End Sub
Public Sub EnumWindows()
Dim hWnd As Long
'
' The EnumWindows function enumerates all top-level windows
' by passing the handle of each window, in turn, to an
' application-defined callback function. EnumWindows
' continues until the last top-level window is enumerated or
' the callback function returns FALSE.
'
1 Set gcolHandle = Nothing
2 Set gcolHandle = New Collection
3 Call EnumWindowsAPI(AddressOf EnumWindowsCallBack, hWnd)
End Sub
On Mon, Nov 24, 2008 at 1:18 PM, Mike Andrews
<[email protected]>wrote:
> Mattias,
>
> Those seem very straight-forward and similar to what Peter said earlier.
> I'll look at them.
> However, I have a question...
>
> The article is a Vista article. Does the same apply to XP and Server 2003?
>
> Thanks,
> Mike
>
> On Mon, Nov 24, 2008 at 3:10 PM, Mattias Sjögren <[email protected]> wrote:
>
> > Mike,
> >
> > Have a look at these MSDN pages if you haven't already.
> >
> > http://msdn.microsoft.com/en-us/library/cc144179(VS.85).aspx<http://msdn.microsoft.com/en-us/library/cc144179%28VS.85%29.aspx>
> <http://msdn.microsoft.com/en-us/library/cc144179%28VS.85%29.aspx>(under
> "Managing Taskbar Buttons")
> > http://msdn.microsoft.com/en-us/library/aa511446.aspx (under "Appearing
> on
> > taskbar")
> >
> >
> > Mattias
> >
> >
> > ===================================
> > View archives and manage your subscription(s) at
> > http://peach.ease.lsoft.com/archives
> >
>
> ===================================
> View archives and manage your subscription(s) at
> http://peach.ease.lsoft.com/archives
>
===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives