Die Community zu .NET und Classic VB.
Menü

FAQ 0078: Wie verhindere ich, daß eine minimierte Form in der TaskBar angezeigt wird?

 von 

Frage 

Wie verhindere ich, daß eine minimierte Form in der TaskBar angezeigt wird?

Antwort  

Manchmal ist es nicht erwünscht, daß eine minimierte Form in der TaskBar angezeigt wird.
Das Setzen der Eigenschaft ShowInTaskbar hat dabei oft nicht den erhofften Erfolg.

Folgender SourceCode wurde von Konrad Doblander gepostet:

' Anwendung/Fenster in Taskbar anzeigen/verstecken
' (ShowInTaskBar = schreibgeschützt zur Laufzeit!)

Private Declare Function GetLastError Lib "kernel32.dll" ( _
        ) As Long

Private Declare Sub SetLastError Lib "kernel32.dll" ( _
        ByVal dwErrCode As Long)

Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal wIndx As Long) As Long

Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long

Private Declare Function ShowWindow Lib "user32" ( _
        ByVal hWnd As Long, _
        ByVal nCmdShow As Long) As Long

Private Const GWL_EXSTYLE As Long = -20
Private Const SW_HIDE As Long = &H0
Private Const SW_SHOW As Long = &H5
Private Const WS_EX_APPWINDOW As Long = &H00040000

Public Sub ShowMeInTaskBar( _
        ByVal hForm As Form, _
        ByRef OnOff As Boolean)
    Dim retval As Long ' API-Rückgabewerte
    Dim oldstate As Long ' vorheriger Status des Fensters
    
    ' Fenster kurz ausblenden damit die Änderungen greifen!
    oldstate = ShowWindow(hForm.hWnd, SW_HIDE)
    DoEvents
    
    ' um Fehlschlagen von SetWindowLong feststellen zu können
    Call SetLastError(0) 
    
    ' Fenster-Stil ändern
    If (OnOff = False) Then
        retval = SetWindowLong(hForm.hWnd, GWL_EXSTYLE, _
                            (GetWindowLong(hForm.hWnd, _
                             GWL_EXSTYLE) And Not WS_EX_APPWINDOW))
    Else
        retval = SetWindowLong(hForm.hWnd, GWL_EXSTYLE, _
                            (GetWindowLong(hForm.hWnd, _
                             GWL_EXSTYLE) Or WS_EX_APPWINDOW))
    End If
    
    If (retval = 0) And (GetLastError <> 0) Then
        Call MsgBox("Beim Ändern des Fensterstiles ist ein " & _
            "Fehler aufgetreten!", vbExclamation Or vbOKOnly)
    End If

    'Nur wenn Fenster vorher sichtbar wieder anzeigen
    If (oldstate <> 0) Then
        Call ShowWindow(hForm.hWnd, SW_SHOW)
        DoEvents
    End If
End Sub

Listing 1: Minimierte Form verbergen

Eine etwas andere Lösung wurde von Udo Schmidt gepostet:

'-----------------------------------------------------------------------
' Author    : (softKUS)
'-----------------------------------------------------------------------
'
' How to    delete/add/activate a taskbar item
'
' uses:     ole32, oleaut32, shdocvw
'
Option Explicit
                                                                                
Private Const VarOffset As Long = 8

' (shlguid.h)
' clsid: => SHDOCVW.DLL
Private Const CLSID_TaskbarList As String = _
        "{56FDF344-FD6D-11d0-958A-006097C9A090}"
Private Const IID_ITaskbarList2 As String = _
        "{602D4995-B13A-429b-A66E-1935E44F4317}"
                                                        
'    vTable
' => QueryInterface(riid, @ppvObject) as HResult
Private Const IID_QueryInterface        As Long = 0
' => AddRef() as ULong
Private Const IID_AddRef                As Long = 4
' => Release() as ULong
Private Const IID_Release               As Long = 8
' => HrInit() as HResult
Private Const IID_HrInit                As Long = 12
' => AddTab(hWnd as Long) as HResult
Private Const IID_AddTab                As Long = 16
' => DeleteTab(hWnd as Long) as HResult
Private Const IID_DeleteTab             As Long = 20
' => ActivateTab(hWnd as Long) as HResult
Private Const IID_ActivateTab           As Long = 24
' => SetActiveAlt(hWnd as Long) as HResult
Private Const IID_SetActiveAlt          As Long = 28
' => MarkFullscreenWindow(hWnd as Long, fFullscreen as boolean) _
'                                                             as HResult
Private Const IID_MarkFullscreenWindow  As Long = 32

' const: (WTYPES.h)   => ClassContext
Private Const CLSCTX_INPROC_SERVER      As Long = 1
' const: (OAIDL.h)    => CallConvention
Private Const CC_STDCALL                As Long = 4
' const: (WINERROR.h)
Private Const S_OK                      As Long = 0


Public Enum ITaskBarList
    AddItem = 16
    DeleteItem = 20
    ActivateItem = 24
    MarkFullscreenWindow = 32
End Enum

Private Declare Function CLSIDFromString Lib "ole32" ( _
        ByVal lpszProgID As Long, _
        ByVal pCLSID As Long) As Long

Private Declare Function CoCreateInstance Lib "ole32" ( _
        ByVal rclsid As String, _
        ByVal pUnkOuter As Long, _
        ByVal dwClsContext As Long, _
        ByVal riid As String, _
        ByRef ppv As Long) As Long

Private Declare Sub DispCallFunc Lib "oleaut32" ( _
        ByVal ppv As Long, _
        ByVal oVft As Long, _
        ByVal cc As Long, _
        ByVal rtTYP As VbVarType, _
        ByVal paCNT As Long, _
        ByVal paTypes As Long, _
        ByVal paValues As Long, _
        ByRef fuReturn As Variant)

' tskBAR        Add/Delete/Mark taskbar item
'
' CALL:         tskBAR(H1:hWnd, [C1:enmCommand], [F1:fFullscreen])
'
' IN:           lng:H1  Window to be deleted from list
'               enm:C1  Command (default = AddItem)
'               log:F1  Fullscreenmode (defined only with 
'                        C1=MarkfullscreenWindow)
'
' OUT:          log     success
'
Public Function tskBAR( _
        hWnd As Long, _
        Optional enmCommand As ITaskBarList = AddItem, _
        Optional fFullscreen As Boolean = True) As Boolean
    Dim oid     As String     ' object-id
    Dim iid     As String     ' interface-id
    Dim ipt     As Long       ' interface-ptr
    Dim ret     As Long
    Dim arT(1)  As Integer    ' parameter types
    Dim arV(1)  As Long       ' parameter values
    
    oid = cnvCLSID(CLSID_TaskbarList)
    iid = cnvCLSID(IID_ITaskbarList2)
    
    arT(0) = vbLong
    arT(1) = vbBoolean
    arV(0) = VarPtr(hWnd) - VarOffset
    arV(1) = VarPtr(fFullscreen) - VarOffset
    
    If CoCreateInstance(oid, 0&, CLSCTX_INPROC_SERVER, iid, ipt) = S_OK Then
        Call DispCallFunc(ipt, IID_HrInit, CC_STDCALL, vbLong, 0&, 0&, _
                                                                    0&, ret)
        If enmCommand = MarkFullscreenWindow Then
            Call DispCallFunc(ipt, enmCommand, CC_STDCALL, vbLong, 2, _
                                             VarPtr(arT(0)), VarPtr(arV(0)), ret)
        Else
            Call DispCallFunc(ipt, enmCommand, CC_STDCALL, vbLong, 1, _
                                             VarPtr(arT(0)), VarPtr(arV(0)), ret)
        End If
        Call DispCallFunc(ipt, IID_Release, CC_STDCALL, vbLong, 0, 0&, 0&, ret)
        tskBAR = True
    End If
End Function

' cnvCLSID      Converts clsid-string to binary string (unicode)
'
' CALL:         cnvCLSID(clsid)
'
' IN:           chr:clsid   i.e. {3C374A40-BAE4-11CF-BF7D-00AA006946EE}
'
' OUT:          chr         16-byte converted string
'
Private Function cnvCLSID(clsid As String) As String
    Dim B1(15) As Byte
    
    API_CLSIDFromString StrPtr(clsid), VarPtr(B1(0))
    cnvCLSID = StrConv(B1, vbUnicode)
End Function

Listing 2: Minimierte Form verbergen

Ihre Meinung  

Falls Sie Fragen zu dieser FAQ haben, Ihre Erfahrung mit anderen Nutzern austauschen möchten oder auf eine Ergänzung hinweisen möchten, dann teilen Sie uns diese bitte in einem der unten vorhandenen Themen oder über einen neuen Beitrag mit. Hierzu können sie einfach einen Beitrag in einem zum Thema passenden Forum anlegen, welcher automatisch mit dieser Seite verknüpft wird.