ATTN:Mike D. Sutton

ATTN:Mike D. Sutton

Post by EdG » Thu, 06 Oct 2005 05:00:17

Back around May 2004 you were helping me try and get a grasp on some of
the pic to icon stuff and my last Q. was about icon transparency, I
finally took another look at the code you left me and came up this.
If you have a minute could you just take a quick look at this, it does
what I want it to do, just wondering if it's the correct way? ;p
btw, it's really nice to see you're still around here in the groups!
Take Care,
Ed Wilk

NumToIcon Usage: Takes an Integer and turns it into a system tray icon
to display CPU/PC temps, Virutual Desktop desk#, etc, etc, 3 pic boxes
used - (picture1, holder, newicon).

Sub NumToIcon(num As Integer)
'Notes: Holder.AutoRedraw = True (set in IDE) other 2 pic boxes = False
'To change icon look, Set Picture1.Font[properties], Colors etc,etc....

Dim hIcon As Long, retval As Long, T As Long, L As Long
Dim IconPic As StdPicture
Dim TextSize As POINTAPI
Dim tex As String

'conv Val to two char string
If num < 10 Then
tex$ = "0" & Trim$(Str$(num))
tex$ = Trim$(Str$(num))
End If

Picture1.Picture = LoadPicture("") 'erase old and put new text in center
GetTextExtentPoint32 Picture1.hDC, tex$, 2, TextSize
L = (32 - TextSize.X) \ 2: T = (32 - TextSize.Y) \ 2
TextOut Picture1.hDC, L, T, tex$, 2

retval = BitBlt(Holder.hDC, 0, 0, 32, 32, Picture1.hDC, 0, 0, SRCCOPY)

Set Holder.Picture = Holder.Image
'Create an icon from the picture making any magenta pixels transparent!
hIcon = BitmapToIcon(Holder.Picture.Handle, vbMagenta)
Set IconPic = GDIToPicture(hIcon)

If (IconPic Is Nothing) Then
'Clean up API icon handle to prevent resource leaks
Call DestroyIcon(hIcon)
Else 'At this point IconPic owns the API icon handle so,
Set NewIcon.Picture = IconPic 'we don't need to kill it
TrayIcon.hIcon = NewIcon.Picture
Shell_NotifyIcon &H1, TrayIcon 'update the sys tray icon.
End If

End Sub
'***** end of new code ******

ATTN:Mike D. Sutton

Post by Mike D Sut » Thu, 06 Oct 2005 21:26:02

[Comments in-line]

<code snipped>

Here's a quicker way of making sure you get at least a two-digit number:
tex$ = Format$(num, "00")

If you would prefer to pass in the number of digits to pad to then this will do it:
tex$ = Format$(num, String$(Digits, "0"))
Where "Digits" is an integer variable specifying the number of digits to pad to.

This should be marginally faster:
Set Picture1.Picture = Nothing

It's possible that tex$ can be more than 2 characters long (in your original code and in my versions above) so it's probably best to
pass in Len(tex$) as the last parameter of TextOut() (and in GetTextExtentPoint32()) unless you're validating it elsewhere.

If you grab the latest version of the OLEPicture library from my site, it will perform the cleanup for you to cut down the amount
your code has to do. There's also an icon library which was derived from my original post to you which you may find handy.
I've not tested it, but the rest looks ok to me - Check though that you've got no GDI resource leaks when creating a bunch of
different icons, icons are notorious for leaking their internal bitmaps if you're not careful.
Hope this helps,


- Microsoft Visual Basic MVP -


ATTN:Mike D. Sutton

Post by EdG » Thu, 06 Oct 2005 22:43:59

On Wed, 5 Oct 2005 13:26:02 +0100, "Mike D Sutton" < XXXX@XXXXX.COM >


Thanks for the feedback!