PlanetSquires Forums

Support Forums => General Board => Topic started by: Richard Kelly on November 16, 2011, 09:11:18 AM

Title: Transparent Bitmaps and Gradient Backgrounds
Post by: Richard Kelly on November 16, 2011, 09:11:18 AM
Here is what I've come up with to paint bitmaps on my FF forms with transparent backgrounds. I use a label control for the bitmap destination and in the forms CREATE I call the "SetTransparent" routine to be sure I get a null brush for the label control.

In the forms CUSTOM function:


Function MAIN_CUSTOM ( _
                     hWndForm      As Dword, _  ' handle of Form
                     wMsg          As Long,  _  ' type of message
                     wParam        As Dword, _  ' first message parameter
                     lParam        As Long   _  ' second message parameter
                     ) As Long
                     
Local hBMColor          As Dword

   Select Case wMsg
   
    Case %WM_ERASEBKGND
   
        GradientPaint (hWndForm)
       
        hBMColor = LoadImage (App.hInstance,"IMAGE_CTARGET",%IMAGE_BITMAP,0,0,%LR_SHARED)
   
        TransparentBitmap (HWND_MAIN_CTLOGO,hBMColor,Rgb(34,177,76))

        DeleteObject hBMColor
       
        Function = %TRUE
       
        Exit Function
       
   End Select

End Function


And the supporting routines:


Sub TransparentBitmap (ByVal hWndControl As Long, _
                       ByVal hPicture As Dword, _
                       ByVal nTransparentColor As Dword)

' Paint a bitmap with a transparent background

Local hDC               As Dword
Local hDCColor          As Dword
Local BMInfo            As Bitmap

' Get size of bitmap

    GetObject (hPicture,SizeOf(BMInfo),BMInfo)
   
' Get DC and set it to the loaded bitmap

    hDCColor = CreateCompatibleDC (0)
    SelectObject (hDCColor,hPicture)

' Get control's DC and paint bitmap
   
    hDC = GetDC (hWndControl)
    TransparentBlt (hDC,0,0,BMInfo.BMWidth,BMInfo.BMHeight,hDCColor,0,0,BMInfo.BMWidth,BMInfo.BMHeight,nTransparentColor)

' Cleanup
   
    DeleteDC hDC
    DeleteDC hDCColor

End Sub
Sub SetTransparent (ByVal hWndControl As Dword)

Local ff    As FLY_DATA Ptr
Local hDC   As Dword

    ff = GetProp (hWndControl, "FLY_PTR")
    @ff.hBackBrush = GetStockObject (%NULL_BRUSH)
    SetProp hWndControl, "FLY_PTR", ff
    hDC = GetDC(hWndControl)
   
    SetBkMode hDC, %TRANSPARENT

    ReleaseDC hWndControl, hDC   
   
End Sub
Sub GradientPaint (ByVal hWndForm As Dword)

Local hDC            As Dword
Local rc             As Rect
Dim gRect            As GRADIENT_RECT
Dim vert(1)          As TRIVERTEX

    hDC = GetDC (hWndForm)
    GetClientRect (hWndForm,rc)
    vert(0).x      = 0
    vert(0).y      = 0
    vert(0).Red    = &HFF00
    vert(0).Green  = &HFF00
    vert(0).Blue   = &HFF00
    vert(0).Alpha  = &H0000
    vert(1).x      = rc.nRight
    vert(1).y      = rc.nBottom
    vert(1).Red    = &HE300
    vert(1).Green  = &HED00
    vert(1).Blue   = &HF900
    vert(1).Alpha  = &H0000
    gRect.UpperLeft  = 0
    gRect.LowerRight = 1
   
    GradientFill hDC, vert(0), 2, gRect, 1, %GRADIENT_FILL_RECT_V
   
    ReleaseDC hWndForm, hDC

End Sub


Attached is a sample of the result for the project (main form) I'm currently working on.

Rick Kelly