(PB8 Windows XP)
I recently had the need to be able to create a "ghost" icon, similiar to that which explorer creates to indicate a file or directory is hidden.
After struggling to find anything on the internet I made a function. I have it working fine when the screen is set to 16-bit colour mode, but in 32-bit the icon mask seems to have a problem:
Normal icon:

Ghosted icon in 16-bit:
Ghosted icon in 32-bit:

I thought that perhaps someone with more knowledge of this sort of thing might know what the problem is. Also I am sure it would run faster if there is a way to get all the bits in one go rather than using GetPixel.
Here is the function. It takes an HICON handle of the original icon, and returns a new HICON handle for the new icon:
I recently had the need to be able to create a "ghost" icon, similiar to that which explorer creates to indicate a file or directory is hidden.
After struggling to find anything on the internet I made a function. I have it working fine when the screen is set to 16-bit colour mode, but in 32-bit the icon mask seems to have a problem:
Normal icon:

Ghosted icon in 16-bit:

Ghosted icon in 32-bit:

I thought that perhaps someone with more knowledge of this sort of thing might know what the problem is. Also I am sure it would run faster if there is a way to get all the bits in one go rather than using GetPixel.
Here is the function. It takes an HICON handle of the original icon, and returns a new HICON handle for the new icon:
Code:
Function CreateGhostIcon(ByVal SourceIcon As Dword) As Dword Local MemoryDC_Source As Dword Local MemoryDC_Target As Dword Local MemoryDC_Mask As Dword Local MainDC As Dword Local NewIcon As Dword Local SourceBitmap As Bitmap Local OldBitmap_Source As Dword Local OldBitmap_Target As Dword Local OldBitmap_Mask As Dword Local SourceIconInfo As ICONINFO Local NewIconInfo As ICONINFO Local IconHeight As Long Local IconWidth As Long Local IconY As Long Local IconX As Long Local SourcePixel As Dword Local SourceMask As Dword Local ColourR As Byte Local ColourG As Byte Local ColourB As Byte Local ColourAverage As Byte MainDC = GetDC(0) MemoryDC_Source = CreateCompatibleDC(MainDC) MemoryDC_Target = CreateCompatibleDC(MainDC) MemoryDC_Mask = CreateCompatibleDC(MainDC) ReleaseDC 0, MainDC If GetIconInfo(SourceIcon,SourceIconInfo) <> 0 And GetObject(SourceIconInfo.hbmColor, Len(Bitmap),SourceBitmap) <> 0 Then IconHeight = SourceIconInfo.yHotSpot * 2 IconWidth = SourceIconInfo.xHotSpot * 2 NewIconInfo.hbmColor = CreateBitmap(IconWidth,IconHeight,SourceBitmap.bmPlanes,SourceBitmap.bmBitsPixel,ByVal 0) If NewIconInfo.hbmColor <> 0 Then OldBitmap_Source = SelectObject(MemoryDC_Source,SourceIconInfo.hbmColor ) OldBitmap_Target = SelectObject(MemoryDC_Target,NewIconInfo.hbmColor) OldBitmap_Mask = SelectObject(MemoryDC_Mask,SourceIconInfo.hbmMask) For IconY = 0 To IconHeight -1 For IconX = 0 To IconWidth -1 SourcePixel = GetPixel(MemoryDC_Source,IconX,IconY) SourceMask = GetPixel(MemoryDC_Mask,IconX,IconY) If SourceMask = 0 Then ' Retrieve and lighten the colours ColourR = (GetRValue(SourcePixel) + 255) \ 2 ColourG = (GetGValue(SourcePixel) + 255) \ 2 ColourB = (GetBValue(SourcePixel) + 255) \ 2 ' Get the average colour value and use it to make the colours more similiar to each other (ie more grey) ColourAverage = (ColourR + ColourG + ColourB) \ 3 ColourR = (ColourR + ColourAverage + ColourAverage) \ 3 ColourG = (ColourG + ColourAverage + ColourAverage) \ 3 ColourB = (ColourB + ColourAverage + ColourAverage) \ 3 SetPixel MemoryDC_Target,IconX,IconY,RGB(ColourR,ColourG,ColourB) End If Next Next SelectObject MemoryDC_Source, OldBitmap_Source SelectObject MemoryDC_Target, OldBitmap_Target SelectObject MemoryDC_Mask, OldBitmap_Mask NewIconInfo.fIcon = %TRUE NewIconInfo.yHotSpot = SourceIconInfo.yHotSpot NewIconInfo.xHotSpot = SourceIconInfo.xHotSpot NewIconInfo.hbmMask = SourceIconInfo.hbmMask NewIcon = CreateIconIndirect(NewIconInfo) DeleteObject NewiconInfo.hbmColor End If DeleteObject SourceIconInfo.hbmColor DeleteObject SourceIconInfo.hbmMask End If ReleaseDC 0, MemoryDC_Source ReleaseDC 0, MemoryDC_Target ReleaseDC 0, MemoryDC_Mask Function = NewIcon End Function
Comment