Icons von Format
Icons in verschiedenen Größen aus einer Ressource/Datei laden.
Wenn Sie Icons in einer Ressource zusammenfassen und diese mit der LoadResPicture Funktion extrahieren, erhalten Sie immer ein Icon im Format 32x32, selbst wenn es eigentlich nur im Format 16x16 vorliegt. Abhilfe verspricht -wie so oft- ein Griff in die API Kiste.
Icondateien können nebeneinander mehrere Icons in verschiedenen Größen und Farbtiefen aufnehmen. Die VB eigene LoadPicture-Funktion berücksichtigt diesen Umstand erst ab VB6 und erlaubt die Angabe der gewünschten Größe und Farbtiefe des zu ladenden Icons. Das nutzt allerdings nicht viel, wenn man Icons in einer Ressource zusammenfassen möchte und mit der LoadResPicture-Funktion extrahiert. Zurück kommt immer ein Icon im Format 32x32, selbst wenn das eigentliche Icon nur im Format 16x16 vorliegt.
Mehr Flexibilität bietet hier die API Funktion LoadImage, wo gewünschte Größe und Farbtiefe des zu extrahierenden Icons frei gewählt werden können. Extrahiert wird das Iconformat, was dem gefragtem am nächsten liegt und Differenzen ggf. gestreckt oder gestaucht. Als Ergebnis erhält man einen Icon-Handle, der über die Funktion HandleToPicture in ein VB freundliches StdPicture-Objekt konvertiert wird:
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" _ (ByVal hInst As Long, ByVal lpszName As Any, ByVal uType As Long, _ ByVal cxDesired As Long, ByVal cyDesired As Long, _ ByVal fuLoad As Long) As Long Private Const IMAGE_ICON = 1 Public Enum IconSizeConstants isIconUser = -1 isIcon16 = 16 isIcon32 = 32 isIcon48 = 48 End Enum Public Enum ColorDepthConstants cdDefault = &H0 cdMonochrome = &H1 cdColor = &H2 cdVGAColor = &H80 End Enum Public Function LoadResIcon(ByVal ID As Variant, _ Optional ByVal Size As IconSizeConstants = isIcon32, _ Optional ByVal ColorDepth As ColorDepthConstants, _ Optional ByVal Width As Long, _ Optional ByVal Height As Long) As StdPicture Dim hIcon As Long If (Size <> isIconUser) Then Height = Size Width = Size End If Select Case VarType(ID) Case vbLong, vbInteger hIcon = LoadImage(App.hInstance, CLng(ID), IMAGE_ICON, _ Width, Height, ColorDepth) Case vbString hIcon = LoadImage(App.hInstance, CStr(ID), IMAGE_ICON, _ Width, Height, ColorDepth) End Select Set LoadResIcon = HandleToPicture(hIcon, vbPicTypeIcon) End Function
Einen kleinen Haken hat diese Funktion allerdings: Sie funktioniert nur in der kompilierten Anwendung, nicht in der IDE.
Für VB4/VB5 noch eine Funktion LoadIcon, die ähnlich wie die LoadPicture Funktion ab VB6 die Möglichkeit bietet, das passende Iconformat aus einer Icondatei zu lesen:
Private Const LR_LOADFROMFILE = &H10 Public Function LoadIcon(ByVal FileName As String, _ Optional ByVal Size As IconSizeConstants = isIcon32, _ Optional ByVal ColorDepth As ColorDepthConstants, _ Optional ByVal Width As Long, _ Optional ByVal Height As Long) As StdPicture Dim hIcon As Long If (Size <> isIconUser) Then Height = Size Width = Size End If hIcon = LoadImage(App.hInstance, FileName, IMAGE_ICON, _ Width, Height, LR_LOADFROMFILE Or ColorDepth) Set LoadIcon = HandleToPicture(hIcon, vbPicTypeIcon) End Function
Download
- modUserIcons Modul und Beispielprojekt (usericons.zip - 18 KB)