From: DanS on
"JP Bless" <jp3BlessNoSpam(a)hotmail.com> wrote in
news:etLW3kl4HHA.3684(a)TK2MSFTNGP02.phx.gbl:

> Tested the code... worked OK with listbox. But I can't (so far) get it
> to work with listview. It did not throw any error yet the size or
> background color of the listview did not change. If any one figures
> how to make this code work with a listview... or if you have another
> code that could make a listview transparent please be kind enough and
> help.
>
> Thanks all

JP,

After finally getting Mike's code to work, I was messing with the
listview as well trying to get that to work.

It came with mixed results though. What I did was subclass the listview
and look for the WM_ERASEBKGND message.

When that message came through, I set the return to 1, and I was using
PaintDesktop() to paint the desktop image on the LV (simply because if I
was able to get a transparent LV, in my app I would then use
PaintDesktop).

This seemed to work to get the correct image in the listview, but the
icon text was drawn w/o a transparent background (the text was in a block
of color), and the icons didn't all redraw when the LV had focus and was
scrolled w/the mouse wheel.

I'm sure there are solutions for the anomalies, but that may be overly
complicated anyway. By overcomplicated, I mean almost near fully
supporting owner-draw.

If you do find a good technique, please let us know.



Regards,

DanS

PS - If the listview doesn't change positions on the form, one extra-
cheesy way to do it would be to set the .picture of the LV equal to the
picture under the listview on the form. The LV would have to be NOT
visible when this is done. I've pasted code below to easily do this.
I'm not sure where this operation would need to be done, it will take
some experimenting. (Maybe at the very first Form_Paint event only, using
a Static Boolean in the Paint event so it only runs the very first paint.
Where I am using it is in the Form_Load event, but at that point, the
form isn't shown, but that's OK because my picture is the desktop image.)

It would be done like this:

LV.visible = False
DoEvents
LV.picture = HDCtoPicture(me.hDC, insert the rest of your params
here)
LV.Visible = True
ReleaseDC(LV.hwnd,hdcDesk)

There's also an HDCtoPictureScale function if you wanted to resize the
image on the destination.

------------------Begin Module Code------------------------
Option Explicit

Const RC_PALETTE As Long = &H100
Const SIZEPALETTE As Long = 104
Const RASTERCAPS As Long = 38
Private Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type
Private Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll"
(PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long,
IPic As IPicture) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As
Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As
Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long,
ByVal hObject As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long,
ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc
As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long,
lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As
LOGPALETTE) As Long
Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long,
ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long)
As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal
X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long,
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop
As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As
Picture
Dim r As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID

'Fill GUID info
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With

'Fill picture info
With Pic
.Size = Len(Pic) ' Length of structure
.Type = vbPicTypeBitmap ' Type of Picture (bitmap)
.hBmp = hBmp ' Handle to bitmap
.hPal = hPal ' Handle to palette (may be null)
End With

'Create the picture
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

'Return the new picture
Set CreateBitmapPicture = IPic
End Function
Public Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long,
ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As
Picture
Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, r As Long
Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long,
HasPaletteScrn As Long
Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE

'Create a compatible device context
hDCMemory = CreateCompatibleDC(hDCSrc)
'Create a compatible bitmap
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
'Select the compatible bitmap into our compatible device context
hBmpPrev = SelectObject(hDCMemory, hBmp)

'Raster capabilities?
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
'Does our picture use a palette?
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
'What's the size of that palette?
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of

If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Set the palette version
LogPal.palVersion = &H300
'Number of palette entries
LogPal.palNumEntries = 256
'Retrieve the system palette entries
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry
(0))
'Create the palette
hPal = CreatePalette(LogPal)
'Select the palette
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
'Realize the palette
r = RealizePalette(hDCMemory)
End If

'Copy the source image to our compatible device context
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc,
TopSrc, vbSrcCopy)

'Restore the old bitmap
hBmp = SelectObject(hDCMemory, hBmpPrev)

If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Select the palette
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If

'Delete our memory DC
r = DeleteDC(hDCMemory)

Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
End Function

Public Function hDCToPictureScale(ByVal hDCSrc As Long, ByVal LeftSrc As
Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As
Long, ByVal destW As Long, ByVal destH As Long) As Picture


Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, r As Long
Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long,
HasPaletteScrn As Long
Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE

'Create a compatible device context
hDCMemory = CreateCompatibleDC(hDCSrc)
'Create a compatible bitmap
'hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmp = CreateCompatibleBitmap(hDCSrc, destW, destH)

'Select the compatible bitmap into our compatible device context
hBmpPrev = SelectObject(hDCMemory, hBmp)

'Raster capabilities?
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
'Does our picture use a palette?
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
'What's the size of that palette?
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of

If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Set the palette version
LogPal.palVersion = &H300
'Number of palette entries
LogPal.palNumEntries = 256
'Retrieve the system palette entries
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry
(0))
'Create the palette
hPal = CreatePalette(LogPal)
'Select the palette
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
'Realize the palette
r = RealizePalette(hDCMemory)
End If

'Copy the source image to our compatible device context
'R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc,
TopSrc, vbSrcCopy)
win.SetStretchBltMode hDCMemory, win.HALFTONE
r = StretchBlt(hDCMemory, 0, 0, destW, destH, hDCSrc, LeftSrc,
TopSrc, WidthSrc, HeightSrc, vbSrcCopy)
'r = FoxAlphaBlend(hDCMemory, 0, 0, destW, destH, hDCSrc, 0, 0, 150)

'Restore the old bitmap
hBmp = SelectObject(hDCMemory, hBmpPrev)

If HasPaletteScrn And (PaletteSizeScrn = 256) Then
'Select the palette
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If

'Delete our memory DC
r = DeleteDC(hDCMemory)

Set hDCToPictureScale = CreateBitmapPicture(hBmp, hPal)

End Function
------------------Begin Module Code------------------------