News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Transparent Traybar Icon Problem

Started by fearless, June 29, 2013, 12:21:03 AM

Previous topic - Next topic

fearless

Hi,

Ive been trying to create a transparent background icon with text (for a percentage monitor type thing) for display in the traybar. Found some code online that i had hoped to adapt to make it work, but for the life of me i cant seen to figure out what im doing wrong. I was hoping someone with more experience in gdi, bitmaps, dc stuff etc would have a look and it might jump out at them what ive missed or am not doing right.

I used the code from this link as my starting point: http://support.microsoft.com/kb/318876

Ive created a little example in radasm to give everyone an idea of how it looks at the moment.

Thanks in advance for taking the time to look at this little example thing.


dedndave

i like TransparentBlt
you have to add
        INCLUDE     \masm32\include\msimg32.inc
        INCLUDELIB  \masm32\lib\msimg32.lib


here is an example

http://dedndave.x10.mx/files/MillerTime.zip

fearless

Thanks Dave, unfortunately it didnt work to create a transparent background to copy onto.

Found another source whilst searching/googling, and i adapted the code, which seems to work fine. I added a param to change color of text as well.

;------------------------------------------------------------------------------------------
; Create Transparent Text Icon For Traybar
; Original sourcecode: http://www.techpowerup.com/forums/showthread.php?t=141783
;------------------------------------------------------------------------------------------
TrayIconText PROC lpszText:DWORD, dwTextColorRGB:DWORD
    ;// Creates a DC for use in multithreaded programs (works in single threaded as well)
    LOCAL hdc:HDC
    LOCAL hMemDC:HDC
    LOCAL hdcMem2:HDC
    LOCAL hBitmap:DWORD
    LOCAL hbmMask:DWORD
    LOCAL hAphaCursor:HICON
    LOCAL cbox:RECT
    LOCAL hbrBkgnd:HBRUSH
    LOCAL lentext:DWORD
    LOCAL ii:ICONINFO
    LOCAL hAlphaCursor:DWORD
   
    Invoke lstrlen, lpszText
    mov lentext, eax
   
    ;// Only safe way I could find to make a DC for multithreading
    Invoke CreateDC, CTEXT("DISPLAY"), NULL,NULL,NULL
    mov hdc, eax

    ;// Makes it easier to center the text
    mov cbox.left, 0
    mov cbox.top, 0
    mov cbox.right, 16
    mov cbox.bottom, 16
   
    ;// Create the text bitmap.
    Invoke CreateCompatibleBitmap, hdc, cbox.right, cbox.bottom
    mov hBitmap, eax
    Invoke CreateCompatibleDC, hdc
    mov hMemDC, eax
    Invoke SelectObject, hMemDC, hBitmap

    ;// Draw the text bitmap
    Invoke CreateSolidBrush, RGBCOLOR(0,0,0)
    mov hbrBkgnd, eax
    Invoke FillRect, hMemDC, Addr cbox, hbrBkgnd
    Invoke DeleteObject, hbrBkgnd
    Invoke GetStockObject, DEFAULT_GUI_FONT
    Invoke SelectObject, hMemDC, eax
    Invoke SetBkColor, hMemDC, RGBCOLOR(0,0,0)
    Invoke SetTextColor, hMemDC, dwTextColorRGB ;RGBCOLOR(118,198,238) ;RGBCOLOR(255,255,255)
    Invoke DrawText, hMemDC, lpszText, lentext, Addr cbox, DT_SINGLELINE or DT_VCENTER or DT_CENTER
   
    ;// Create monochrome (1 bit) mask bitmap.
    Invoke CreateBitmap, cbox.right, cbox.bottom, 1, 1, NULL
    mov hbmMask, eax
    Invoke CreateCompatibleDC, 0
    mov hdcMem2, eax
    Invoke SelectObject, hdcMem2, hbmMask

    ;// Draw transparent color and create the mask
    Invoke SetBkColor, hMemDC, RGBCOLOR(0,0,0)
    Invoke BitBlt, hdcMem2, 0, 0, cbox.right, cbox.bottom, hMemDC, 0, 0, SRCCOPY
   
    ;// Clean up
    Invoke DeleteDC, hMemDC
    Invoke DeleteDC, hdcMem2

    mov ii.fIcon, TRUE
    mov ii.xHotspot, 0
    mov ii.yHotspot, 0
    mov eax, hbmMask
    mov ii.hbmMask, eax
    mov eax, hBitmap
    mov ii.hbmColor, eax

    ;// Create the icon with transparent background
    Invoke CreateIconIndirect, Addr ii
    mov hAlphaCursor, eax

    Invoke DeleteObject, hBitmap
    Invoke DeleteObject, hbmMask
    Invoke DeleteDC, hdc

    mov eax, hAlphaCursor
    ret

TrayIconText ENDP


Hopefully if someone else goes looking for a similar issue, they will find this post with the attached code.