Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwnewlong As Long) As Long
Private Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor As Long, ByVal lHPalette As Long, ByRef lColorRef As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function CreatePatternBrush Lib "gdi32.dll" (ByVal hBitmap As Long) As Long
Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As Long, ByVal bErase As Long) As Long
Private Const GCL_HBRBACKGROUND As Long = -10
Private Function GDI_TranslateColor(OleClr As OLE_COLOR, Optional hPal As Integer = 0) As Long
' used to return the correct color value of OleClr as a long
If OleTranslateColor(OleClr, hPal, GDI_TranslateColor) Then
GDI_TranslateColor = &HFFFF&
End If
End Function
Function GDI_CreateSoildBrush(bColor As OLE_COLOR) As Long
'Create a Brush form a picture handle
GDI_CreateSoildBrush = CreateSolidBrush(GDI_TranslateColor(bColor))
End Function
Public Sub SetToolbarBG(hwnd As Long, hBmp As Long)
'Set the toolbars background image
DeleteObject SetClassLong(hwnd, GCL_HBRBACKGROUND, CreatePatternBrush(hBmp))
InvalidateRect 0&, 0&, False
End Sub
Public Sub SetToolbarBK(hwnd As Long, hColor As OLE_COLOR)
' Set a toolbars Backcolor
DeleteObject SetClassLong(hwnd, GCL_HBRBACKGROUND, GDI_CreateSoildBrush(hColor))
InvalidateRect 0&, 0&, False
End Sub
Private Sub cmdBk_Click()
Call SetToolbarBK(Toolbar1.hwnd, vbYellow)
End Sub
Private Sub Command1_Click()
SetToolbarBG Toolbar1.hwnd, Image1.Picture
End Sub
Last edited by xiaoyao; Apr 10th, 2021 at 06:38 AM.
Everything works, except that icons look ugly. It seems that
color depth is decreased and alpha blending is not applied
at all. Here's screenshot fragment to illustrate the
problem:
I have never figured out how to create an ImageList control using a PNG file with an alpha channel. I want to use it as a small image in a ListView control.
I have been trying for a few hours, and the descriptions of what I have tried seem to be too numerous to list. I have searched the Internet, but I haven't found anything.
Most of the operations I have tried will only cause the listview to appear without any images.
Then, I tried to import the following files as resources into my project.
Best answer
As suggested by xMRi, I can do this by saving the PNG image as a 32-bit/alpha channel BMP file.
There are some restrictions on the support of loaded resources. For example, CBitmap::GetBitmapDimensionreturn an empty rectangle. Moreover, there is no support for Alpha channel and high-quality bitmaps in Visual Studio.
However, if I hard-code the image size, it does work. When the item is selected, my ListView control correctly highlights the image, but not the transparent area. Therefore, the Alpha channel can be correctly identified.