Results 1 to 13 of 13

Thread: [RESOLVED] icon with text and transparent background

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    291

    Resolved [RESOLVED] icon with text and transparent background

    Hello everybody.
    I need to put on the systray an icon with text and transparent background.
    I have the next code, but it is always drawn in black.
    Then choose to paint the background with the color RGB but it is not the optimal.
    I am very bad with graphics and even more with GDI XD.

    Code:
    Public Function Icon_text(Text As String, hWnd As Long, Optional FontName As String = "Times New Roman", Optional FontSize As Long = 16, Optional Sizeicon As Long = 16, Optional colorfont As ColorConstants = vbWhite, Optional colorbackground As ColorConstants = vbBlack) As Long
        Dim Hdc As Long, hdcMem As Long
        Dim hBitmap  As Long, hOldBitMap  As Long, hBitmapMask   As Long
        Dim iconData As ICONINFO
        Dim hBrush As Long
        Dim tr As RECT
        Dim hFont As Long
        Dim hIcon As Long
        Hdc = GetDC(hWnd)
        hdcMem = CreateCompatibleDC(Hdc)
        hBitmap = CreateCompatibleBitmap(Hdc, Sizeicon, Sizeicon)
        hBitmapMask = CreateCompatibleBitmap(Hdc, Sizeicon, Sizeicon)
        ReleaseDC hWnd, Hdc
        hOldBitMap = SelectObject(hdcMem, hBitmap)
    
    
        hBrush = CreateSolidBrush(colorbackground)
        tr.Right = Sizeicon: tr.Bottom = Sizeicon
        FillRect hdcMem, tr, hBrush
        
        
        SetTextColor hdcMem, colorfont
    '    PatBlt hdcMem, 0, 0, 32, 32, WHITENESS
        
        SetBkMode hdcMem, TRANSPARENT
        hFont = CreateFont(FontSize, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName)
        hFont = SelectObject(hdcMem, hFont)
        TextOut hdcMem, 5, 0, Text, lstrlen(Text)
        SelectObject Hdc, hOldBitMap
        hOldBitMap = 0
        iconData.fIcon = 1
        iconData.xHotspot = 0
        iconData.yHotspot = 0
        iconData.hbmMask = hBitmapMask
        iconData.hbmColor = hBitmap
        
        hIcon = CreateIconIndirect(iconData)
        
        DeleteObject SelectObject(hdcMem, hFont)
        DeleteDC hdcMem
        DeleteDC Hdc
        DeleteObject hBitmap
        DeleteObject hBitmapMask
        Icon_text = hIcon
    End Function
    Sorry for my translation
    a greeting

  2. #2
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    531

    Re: icon with text and transparent background

    you can used ImageList1 maskcolor to do this ico background transparent

    https://www.vbforums.com/showthread....hlight=hbmMask
    Last edited by xxdoc123; Jul 2nd, 2021 at 03:47 AM.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,851

    Re: icon with text and transparent background

    You need to create the hBitmapMask as a monochrome bitmap based on your hBitmap after rendering text to it. That's how icon transparency works: the mask says which pixels are opaque and which are transparent.

    Your icon is broken, and I'm surprised it works at all.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    291

    Re: icon with text and transparent background

    You could show me an example please.
    If my knowledge in GDI is null.

  5. #5
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    531

    Re: icon with text and transparent background


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    291

    Re: icon with text and transparent background

    Thanks xxdoc123.
    With the icons it works perfectly.
    But I need to generate the icons with the text and there is where I have problems.
    If dilatant could help me with an example.
    I have tried it but I always get in black

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,851

    Re: icon with text and transparent background

    I took a run at this myself and ran into a similar problem.

    These days Windows has dark (normal) and light (always a very pale gray) themes. The dark theme always uses white simple line-drawn or text "icons." Light theme always uses black text. I tried auto-sensing this to use white or black text against the opposite color background, and made the background transparent. That part seems fine, but you might not want to do that yourself anyway.

    Here is the problem:

    White text on black works fine. But black text on white or even ButtonFace gives me what looks like a solid black square. Drawing the icon against a colored backdrop though shows color inversion in the transparent areas. For example: if the backdrop is orange the "transparent" areas draw as blue!

    I'll try to correct this, but for now the code is full of debugging junk so I won't post it as it stands. There is obviously a mistake somewhere in it yet.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,851

    Re: icon with text and transparent background

    Grrr.

    It is even worse. "White text on black" only appeared to work but I just couldn't see it due to colors that were very similar. So my code is broken for any colors. I'll look again later.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    291

    Re: icon with text and transparent background

    Wauu did not think it was so complicated.
    any help is welcome and more coming from you

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,851

    Re: icon with text and transparent background

    Ok, here is a working example.

    You can assign a different color to the two target PictureBox controls instead to see that the icon is properly transparent. Seems fine now.

    I don't expect you to use the class, but at least it shows the GDI calls required.

    The key to this was using black (&H00000000) as the backdrop and "transparent color" when rendering the color image. When I used my "black color" which is slightly non-black or my "white color" I got the bad results. True black was the key here, though I'm not sure why. I never knew that was a requirement.
    Attached Files Attached Files

  11. #11
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    531

    Re: icon with text and transparent background

    Isn’t it enough to convert the picture to an icon with a transparent background?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    291

    Re: icon with text and transparent background

    Thank you very much diletante , it works perfectly for me, is what I needed.
    As it is perfect for me.

    thank you

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,851

    Re: icon with text and transparent background

    Quote Originally Posted by xxdoc123 View Post
    Isn’t it enough to convert the picture to an icon with a transparent background?
    I think he has brief varying text that he wants to use as a notification area icon.

    My example creates typical "square" icons, but it might be altered to accept both width and height to allow something like a 4-digit value that changes each time. I'm not sure those icons can be used in the tray but in general you can make them.

    I'm not even sure I have the icon size correct for tray icons (SM_CXSMICON x SM_CYSMICON) because the ones from the system in Windows 10 look slightly larger, perhaps 24 x 24 at 96 DPI settings.


    So you can do something like paint/print into a PictureBox, then stuff that into a VB6 MSCOMCTL.OCX ImageList, set the MaskColor, and call the ExtractIcon method (ImageList1.ListImages(1).ExtractIcon). However I'm not sure that works for non-square icons.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width