AW: Userform in Taskleiste
20.09.2022 11:18:50
volti
Hallo Thomas,
hier noch ein alternativer Code, der das Icon nicht über SendMessage sondern über SetClassLong setzt. Gleichzeitig habe ich noch eine Function eingebaut, die die Userform immer im Vordergrund hält, auch wenn Du andere Anwendungen aktivierst.
Falls nicht gewünscht, SetWindowPos ausremmen oder entfernen.
Leider ist es auch hier so, dass das Icon in der Taskleiste nicht sofort sichtbar ist. In der Taskleiste ist die Userform mit dem Icon sichtbar wenn man über das Excel-Icon fährt, das Excel-Icon selbst wird nicht ersetzt. Das könnte m.E. daran liegen, dass die einzelnen Excelblätter und auch die Userform Childwindows (also untergeordnete Fenster von Excel) sind.
Mein Versuch, (wie früher mal bei einem Spiel) das App-Icon ebenfalls zu ändern, hat zwar geklappt, unter meiner derzeitigen Version wird es aber nicht mehr im Caption von Excel und auch nicht in der Taskleiste angezeigt.
Code:
Option Explicit
' UserForm in Taskleisten und Minimieren im Rahmen
Private Declare PtrSafe Function FindWindowA Lib "user32.dll" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#If Win64 Then
Private Declare PtrSafe Function GetWindowLongA Lib "user32.dll" _
Alias "GetWindowLongPtrA" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongA Lib "user32.dll" _
Alias "SetWindowLongPtrA" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
Private Declare PtrSafe Function SetClassLongA Lib "user32" _
Alias "SetClassLongPtrA" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLongA Lib "user32.dll" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongA Lib "user32.dll" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
Private Declare PtrSafe Function SetClassLongA Lib "user32" ( _
ByVal Hwnd As LongPtr, ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
#End If
Private Declare PtrSafe Function SetWindowPos Lib "user32" ( _
ByVal Hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Sub UserForm_Initialize()
Dim Hwnd As LongPtr
Const GCL_HICON = (-14)
Const GWL_STYLE = -16&
Const WS_MINIMAXIMIZEBOX = &H30000
Const HWND_TOPMOST = -1
' Mini/Maximieren-Funktion im Userform
Hwnd = FindWindowA("ThunderDFrame", Caption) ' Handle Userform holen
SetWindowLongA Hwnd, GWL_STYLE, GetWindowLongA(Hwnd, GWL_STYLE) _
Or WS_MINIMAXIMIZEBOX ' Mini/Maxiboxen zufügen
'Icon in Titlebar
SetClassLongA Hwnd, GCL_HICON, Image1.Picture.Handle ' Icon setzen
SetWindowPos Hwnd, HWND_TOPMOST, 0, 0, 0, 0, &H3 ' Immer im Vordergrund
Application.Visible = False ' Excel anzeigen aus
End Sub
Private Sub CommandButton1_Click()
Call Unload(Object:=Me)
Application.Visible = True
End Sub
_________________________
viele Grüße aus Freigericht 😊
Karl-Heinz
Ich fürchte daher, dass Dein Wunsch nicht umsetzbar ist.