Announcement

Collapse
No announcement yet.

Transparent Background for text over a Bitmap

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transparent Background for text over a Bitmap

    I have read the Poffs and the Examples archives and struggled to put together a splash screen.

    It works.

    The only thing is that the text i put on the bitmap image has the nasty grey background and I need it to be transparent so my nifty bitmap shows through.

    In the archive Lance says:
    In your dialog callback, draw the text during a %WM_PAINT event (Dont forget to set the background mode to %TRANSPARENT)

    yes but How do I do this?

    '------------------------------------------------------------------------------
    CALLBACK FUNCTION SplashScreen() AS LONG
    SELECT CASE CBMSG
    CASE %WM_INITDIALOG
    MOUSEPTR 13 'Working in BG
    sTimer = SetTimer(sDlg, &HDABEDABE, 2500, %NULL) ' Set for 2.5 seconds
    ' CALL updatewindow (hDlg)
    CASE %WM_PAINT
    ' %wm_background %WS_EX_TRANSPARENT - some how ????
    ' InvalidateRect CBHNDL, BYVAL 0, %TRUE
    CASE %WM_DESTROY
    SendDlgItemMessage sDlg, sTimer, %WM_DESTROY, CBWPARAM , CBLPARAM
    CASE %WM_TIMER
    DIALOG END sDlg, 1
    MOUSEPTR 0
    END SELECT
    END FUNCTION

    '------------------------------------------------------------------------------
    FUNCTION Splash() AS LONG
    DIALOG NEW 0, "",,, 335,160, %WS_POPUP OR %WS_DLGFRAME TO sDlg
    CONTROL ADD IMAGE, sDlg, -1, "Splash", 1, 1, 500, 300 ' load Bitmap from resource
    CONTROL ADD FRAME, sDlg, -2, "Registration:", 20, 55, 150, 35, %WS_EX_LEFT, %WS_EX_TRANSPARENT
    CONTROL ADD LABEL, sDlg, -3, "UserName", 25, 65, 140, 10, %WS_EX_TRANSPARENT
    CONTROL ADD LABEL, sDlg, -4, "UserCompany", 25, 75, 140, 10, %WS_EX_TRANSPARENT
    DIALOG SHOW MODELESS sDlg CALL SplashScreen
    END FUNCTION

    ------------------
    Kind Regards
    Mike

  • #2
    Code:
    CASE %WM_PAINT
     hDC = BeginPaint(hWnd, PS)
     SetBkMode hDC, %TRANSPARENT
     ...
    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Thx Lance,

      i know you guys are sick of answering these WM_PAINT questions cos everyone asks them. I have allmost got it, but i dont get why
      a BeginPaint call is needed. I have set the bkgd to transparent and still there is the grey box for each Label.

      I even tried adding the Label after the BkMode is set to transparent - no luck.

      I have copied and tried all sorts of combinations and permutations but i cant get it work.

      GLOBAL sDlg AS LONG
      '------------------------------------------------------------------------------
      CALLBACK FUNCTION SplashScreen() AS LONG
      SELECT CASE CBMSG
      CASE %WM_INITDIALOG
      LOCAL sTimer AS LONG
      MOUSEPTR 13 ' Working in background
      sTimer = SetTimer(sDlg, &HDABEDABE, 9500, %NULL) ' Set for 2.5 seconds
      CASE %WM_PAINT
      LOCAL hDC AS LONG
      LOCAL ps AS PAINTSTRUCT
      hDC = BeginPaint(CBHNDL, ps)
      SetBkMode hDC, %TRANSPARENT
      CASE %WM_DESTROY
      SendDlgItemMessage sDlg, sTimer, %WM_DESTROY, CBWPARAM , CBLPARAM
      CASE %WM_TIMER
      DIALOG END sDlg, 1
      MOUSEPTR 0 ' Normal function
      END SELECT
      END FUNCTION

      '------------------------------------------------------------------------------
      FUNCTION Splash() AS LONG
      DIALOG NEW 0, "",,, 227,116, %WS_POPUP OR %WS_DLGFRAME TO sDlg
      CONTROL ADD IMAGE, sDlg, -1, "Splash", 1, 1, 500, 300 ' load Bitmap from resource
      CONTROL ADD FRAME, sDlg, -2, "", 85, 20, 110, 35, %WS_EX_LEFT, %WS_EX_TRANSPARENT
      CONTROL ADD LABEL, sDlg, -3, "UserName", 90, 30, 100, 10, %WS_EX_LEFT, %WS_EX_TRANSPARENT
      CONTROL ADD LABEL, sDlg, -4, "UserCompany", 90, 42, 100, 10, %WS_EX_LEFT, %WS_EX_TRANSPARENT
      DIALOG SHOW MODELESS sDlg CALL SplashScreen
      END FUNCTION

      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        Mike --

        Your interpretation of Lance suggestion is too free
        There is a good sample - smtp.bas.
        With labels, options, checkboxes all is simple (see below).

        Simple way to make transparent caption for frame - no idea.
        That's why in similar cases I draw on hDC and do not use Frame.

        Code:
           #Compile Exe
           #Register None
           #Include "Win32Api.Inc"
           #Resource "Smtp.pbr"
           '% 1 BITMAP SPLASH.BMP
        
           %ID_Label  =  101
           %ID_Frame  =  102
           %ID_Option1 =  103
           %ID_Option2 =  104
           %ID_CHECKBOX =  105
        
           CallBack Function DlgProc
              Select Case CbMsg
                 Case %WM_INITDIALOG
                    Static bm As BITMAP, hBmp As Long
                    
                    hBmp = LoadImage(GetModuleHandle(ByVal %NULL), "#1", %IMAGE_BITMAP, 0, 0, %LR_DEFAULTSIZE)
                    GetObject hBmp, SizeOf(bm), bm
                    Control Add Label,   CbHndl, %ID_Label, "Label", 20, 26, 50, 12
                    Control Add Option, CbHndl, %ID_Option1, "Option-1", 20, 40, 80, 12
                    Control Add Option, CbHndl, %ID_Option2, "Option-2", 100, 40, 80, 12
                    Control Add CheckBox, CbHndl, %ID_CheckBox, "Check", 160, 24, 60, 12
                    Control Add Frame,   CbHndl, %ID_Frame, "", 10, 10, 280, 100
                    
                 Case %WM_DESTROY
                    DeleteObject hBmp
        
                 Case %WM_SIZE
                    InvalidateRect CbHndl, ByVal 0, 1
                    UpdateWindow CbHndl
        
                 Case %WM_ERASEBKGND
                    Dim rc As RECT, hBmpDC As Long
                    GetClientRect CbHndl, rc
                    hBmpDC = CreateCompatibleDC(CbWparam)
                    SelectObject hBmpDC, hBmp
                    StretchBlt CbWparam, rc.nLeft, rc.nTop, rc.nRight - rc.nLeft, rc.nBottom - rc.nTop, _
                       hBmpDC, 0, 0, bm.bmWidth, bm.bmHeight, %SRCCOPY
                    DeleteDC hBmpDC
                    Function = 1
        
                 Case %WM_CTLCOLORSTATIC
                    Select Case GetDlgCtrlId(CbLparam)
                       Case %ID_Label, %ID_OPTION1, %ID_OPTION2, %ID_FRAME, %ID_CHECKBOX
                          SetBkMode CbWparam, %Transparent
                          SetTextColor CbWparam, %White
                          Function = GetstockObject(%NULL_BRUSH)
                 End Select
           End Select
           End Function
        
           Function PbMain
              Local hDlg As Long
              Dialog New 0 ,"Test", , , 400, 200, %WS_CAPTION Or %WS_OVERLAPPEDWINDOW To hDlg
              Dialog Show Modal hDlg Call DlgProc
           End Function

        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          Semen thx so much. I get it

          I re-wrote it a little to use different font sizes. Becuase this is not easy, here it is For others following in these footsteps:

          #COMPILE EXE "Bitmap.exe"
          #INCLUDE "Win32Api.Inc"
          #RESOURCE "MyFirst.pbr" 'In Resource file: Splash BITMAP Splash.BMP

          '------------------------------------------------------------------------------
          FUNCTION MakeFont(BYVAL Font AS STRING, BYVAL PointSize AS LONG) AS LONG
          LOCAL hDC AS LONG, CyPixels AS LONG
          hDC = GetDC(%HWND_DESKTOP)
          CyPixels = GetDeviceCaps(hDC, %LOGPIXELSY)
          ReleaseDC %HWND_DESKTOP, hDC
          PointSize = (PointSize * CyPixels) \ 72
          FUNCTION = CreateFont(0 - PointSize, 0, 0, 0, %FW_NORMAL, 0, 0, 0, _
          %ANSI_CHARSET, %OUT_TT_PRECIS, %CLIP_DEFAULT_PRECIS, _
          %DEFAULT_QUALITY, %FF_DONTCARE, BYCOPY Font)
          END FUNCTION

          '------------------------------------------------------------------------------------
          CALLBACK FUNCTION SplashScreen
          SELECT CASE CBMSG
          CASE %WM_INITDIALOG
          STATIC bm AS BITMAP, hBmp AS LONG, sTimer AS LONG, hfont AS LONG, x AS LONG, y AS LONG
          MOUSEPTR 13 ' Working in background
          CONTROL ADD FRAME, CBHNDL, 101, "Registration", 84, 38, 114, 45
          CONTROL ADD LABEL, CBHNDL, 102, "UserName", 90, 45, 120, 15
          CONTROL ADD LABEL, CBHNDL, 103, "Company Name", 120, 67, 80, 10
          hFont = MakeFont("Arial Black", 16)
          CONTROL SEND CBHNDL, 102, %WM_SETFONT,hFont, 1
          hFont = MakeFont("Arial Black", 8)
          CONTROL SEND CBHNDL, 103, %WM_SETFONT,hFont, 1
          sTimer = SetTimer(CBHNDL, &HDABEDABE, 6500, %NULL) ' Set for 2.5 seconds
          hBmp = LoadImage(GetModuleHandle(BYVAL %NULL), "splash", %IMAGE_BITMAP, 0, 0, %LR_DEFAULTSIZE) ' Load the Bitmap from Resource
          ' DIALOG GET SIZE CBHNDL TO x, y
          ' DIALOG UNITS CBHNDL, x, y TO PIXELS x, y
          ' hBmp = LoadImage(BYVAL %NULL, "splash.bmp", %IMAGE_BITMAP, x, y, %LR_LOADFROMFILE) ' Load the bitmap from a file
          GetObject hBmp, SIZEOF(bm), bm

          CASE %WM_ERASEBKGND
          DIM rc AS RECT, hBmpDC AS LONG
          GetClientRect CBHNDL, rc
          hBmpDC = CreateCompatibleDC(CBWPARAM)
          SelectObject hBmpDC, hBmp
          StretchBlt CBWPARAM, rc.nLeft, rc.nTop, rc.nRight - rc.nLeft, rc.nBottom - rc.nTop, _
          hBmpDC, 0, 0, bm.bmWidth, bm.bmHeight, %SRCCOPY
          DeleteDC hBmpDC
          FUNCTION = 1

          CASE %WM_CTLCOLORSTATIC
          SELECT CASE GetDlgCtrlId(CBLPARAM)
          CASE 101, 102, 103, 104, 105 ' etc etc
          SetBkMode CBWPARAM, %Transparent
          SetTextColor CBWPARAM, %black
          FUNCTION = GetstockObject(%NULL_BRUSH)
          END SELECT

          CASE %WM_TIMER
          DIALOG END CBHNDL, 1
          MOUSEPTR 0 ' Normal function

          CASE %WM_DESTROY
          DeleteObject hBmp
          END SELECT
          END FUNCTION

          '--------------------------------------------------------------------------------
          FUNCTION PBMAIN
          LOCAL sDlg AS LONG
          DIALOG NEW 0 ,"", , , 200, 100, %WS_POPUP OR %WS_DLGFRAME TO sDlg
          DIALOG SHOW MODAL sDlg CALL SplashScreen
          END FUNCTION


          ------------------
          Kind Regards
          Mike

          Comment


          • #6
            Since I just spent 16 hrs working on this little problem I might as well share the results.

            After searching Poffs for hours i realized that the ONLY example of anything like this is in the smtp.bas example. Hiding way down in amongst a bunch of smtp stuff are the gems I needed.

            However, I had deleted all the examples I did not need (like anything to do with internet) so I had to re-install PB to get it. You have to search for it cos the file is cleverly hidden in a folder called Tcp - just where you would expect an example of fonts and bitmaps to be!

            So after digging throught the bloated smtp example for hours and hours i re-wrote it a little without the bloat.

            For anyone wanting to put text on a bitmap, or change the color of text, or resize text, or make a background color or transparent this is what you need to study.

            If you want to include the bitmap in a resource see the thread resource 101
            Code:
            '==============================================================================
            '  Modified SMTP example for PB/DLL 6.0
            '  Copyright (c) 1999 PowerBASIC, Inc.
            '==============================================================================
            #COMPILE EXE
            #INCLUDE "WIN32API.INC"
            
            '------------------------------------------------------------------------------
            FUNCTION MakeFont(BYVAL Font AS STRING, BYVAL PointSize AS LONG) AS LONG
              LOCAL hDC AS LONG, CyPixels AS LONG
              hDC = GetDC(%HWND_DESKTOP)
              CyPixels  = GetDeviceCaps(hDC, %LOGPIXELSY)
              ReleaseDC %HWND_DESKTOP, hDC
              PointSize = (PointSize * CyPixels) \ 72
              FUNCTION = CreateFont(0 - PointSize, 0, 0, 0, %FW_NORMAL, 0, 0, 0, _
                        %ANSI_CHARSET, %OUT_TT_PRECIS, %CLIP_DEFAULT_PRECIS, _
                        %DEFAULT_QUALITY, %FF_DONTCARE, BYCOPY Font)
            END FUNCTION
            
            '------------------------------------------------------------------------------
            CALLBACK FUNCTION DlgProc () AS LONG
              STATIC hFont AS LONG, hBmp AS LONG, x AS LONG, y AS LONG
              LOCAL hBmpDC AS LONG, e AS LONG, lf AS LOGFONT, bmpfile AS ASCIIZ * 64
            
              SELECT CASE CBMSG
                CASE %WM_INITDIALOG
                  CONTROL SET TEXT  CBHNDL, 101, "Some text you got"  ' CBHNDL = hDlgMain
                  CONTROL SET TEXT  CBHNDL, 102, "More text you want to display"
                  CONTROL SET FOCUS CBHNDL, 103
                  hFont = MakeFont("Courier New", 8)
                  CONTROL SEND      CBHNDL, 102, %WM_SETFONT,hFont, 1
                    DIALOG GET SIZE CBHNDL TO x, y
                    DIALOG UNITS    CBHNDL, x, y TO PIXELS x, y
                    hBmp = LoadImage(BYVAL %NULL, "smtp.bmp", %IMAGE_BITMAP, x, y, %LR_LOADFROMFILE) ' Load the bitmap from a file
                    FUNCTION = 1
            
                CASE %WM_CTLCOLOREDIT
                  SetTextColor CBWPARAM, %Red
                  FUNCTION = GetStockObject(%WHITE_BRUSH)
            
                CASE %WM_CTLCOLORSTATIC
                  DIALOG SEND CBHNDL, %WM_GETFONT, 0, 0 TO hFont  ' get the font and sixe that the dialog has
                  GetObject hFont, SIZEOF(lf), BYVAL VARPTR(lf)
                  lf.lfWeight = %FW_BOLD
                  hFont = CreateFontIndirect(lf)      ' set font to stock dialog font
                    SelectObject CBWPARAM, hFont
                    SetBkMode CBWPARAM, %TRANSPARENT
                    SetTextColor CBWPARAM, %YELLOW
                    FUNCTION = GetStockObject(%NULL_BRUSH)
            
                CASE %WM_ERASEBKGND
                  hBmpDC = CreateCompatibleDC(CBWPARAM)
                  SelectObject hBmpDC, hBmp
                  BitBlt CBWPARAM, 0, 0, x, y, hBmpDC, 0, 0, %SRCCOPY
                  DeleteDC hBmpDC
                  FUNCTION = 1
              END SELECT
            END FUNCTION
            
            '------------------------------------------------------------------------------
            CALLBACK FUNCTION CancelButton() AS LONG
              DIALOG END CBHNDL, 0
              FUNCTION = 1
            END FUNCTION
            
            '------------------------------------------------------------------------------
            FUNCTION PBMAIN () AS LONG
            LOCAL hDlgMain AS LONG
              DIALOG NEW 0, "Text on a .BMP with font size and color", ,, 365, 250, 0, 0 TO hDlgMain
                CONTROL ADD LABEL, hDlgMain, -1, "Server:",  17, 11, 40, 8, %SS_RIGHT, %WS_EX_TRANSPARENT
                CONTROL ADD LABEL, hDlgMain, -1, "From:",    17, 24, 40, 8, %SS_RIGHT, %WS_EX_TRANSPARENT
                CONTROL ADD LABEL, hDlgMain, -1, "To:",      17, 36, 40, 8, %SS_RIGHT, %WS_EX_TRANSPARENT
                CONTROL ADD LABEL, hDlgMain, -1, "Subject:", 17, 50, 40, 8, %SS_RIGHT, %WS_EX_TRANSPARENT
              CONTROL ADD TEXTBOX, hDlgMain, 101, "", 60,  9, 299,  12, %ES_AUTOHSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
              CONTROL ADD TEXTBOX, hDlgMain, 102, "", 60, 22, 299,  12, %ES_AUTOHSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
              CONTROL ADD TEXTBOX, hDlgMain, 103, "", 60, 35, 299,  12, %ES_AUTOHSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
              CONTROL ADD TEXTBOX, hDlgMain, 104, "", 60, 48, 299,  12, %ES_AUTOHSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
              CONTROL ADD TEXTBOX, hDlgMain, 105, "",  6, 66, 353, 161, %ES_AUTOHSCROLL OR %ES_AUTOVSCROLL OR %ES_MULTILINE OR %ES_WANTRETURN OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
                CONTROL ADD BUTTON,  hDlgMain, %IDCANCEL, "&Cancel", 319, 232, 40, 14, 0 CALL CancelButton
              DIALOG SHOW MODAL hDlgMain CALL DlgProc
            END FUNCTION

            ------------------
            Kind Regards
            Mike

            Comment

            Working...
            X