I discovered an API message today which helps to sort this problem out. We
need to extract an Icon from the imagelist and pass this to the method on my
site which returns a 32bit bitmap. We then use a ColorMatrix to paint a
grayscale image if it's disabled.
I was hoping that DrawThemeImage() would do the job, but it doesn't draw a
disabled Image despite the fact that it takes a State argument.
Since no language was specified I'll give the solution in VB, although the
main part of the solution is from my site and is available in both VB and
C#. I'll create a couple of methods, or maybe even a class and upload them
to the site shortly, but in the meantime this is how I tested the code by
painting on a Label.
Note that pngs draw Alpha with a bluish tint (when not disabled) but Icons
don't so if possible use Icons instead of pngs.
\\\
<DllImport("comctl32.dll")> _
Private Shared Function ImageList_GetIcon(ByVal himl As IntPtr, _
ByVal i As Int32, ByVal diFlags As Int32) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
End Function
Private Sub Label1_Paint(ByVal sender As Object, _
ByVal e As PaintEventArgs) Handles Label1.Paint
If Label1.ImageIndex = -1 Then Return
Dim hIco As IntPtr = ImageList_GetIcon(ImageList1.Handle, 0, 0)
Dim ico As Icon = Icon.FromHandle(hIco)
Dim bmp As Bitmap = IconToAlphaBitmap(ico)
DestroyIcon(hIco)
ico.Dispose()
Dim cm As New ColorMatrix(New Single()() { _
New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim ia As New ImageAttributes
If Label1.Enabled = False Then
ia.SetColorMatrix(cm)
End If
e.Graphics.DrawImage(bmp, New Rectangle(0, 0, 32, 32), _
0, 0, 32, 32, GraphicsUnit.Pixel, ia)
bmp.Dispose()
ia.Dispose()
End Sub
///
The IconToAlphaBitmap method can be found on my site:
http://dotnetrix.co.uk/misc.html --> Get Alpha Bitmap from 32 bit Icon
--
Mick Doherty
http://dotnetrix.co.uk/nothing.html