Transparent Form

Started by Marc van Cauwenberghe, August 31, 2013, 08:01:33 AM

Previous topic - Next topic

Marc van Cauwenberghe

Hi,

Is there an easy way to implement a transparent form in FF. Maybe Paul could use some magic then you set the background bitmap of a form? BackBitmapMode = Transparent  :D

Thank you.
Marc

Marc van Cauwenberghe

Hi,

a question that could more or less do away with previous question.

Does anyone have a working FF peace of code that would capture part of a screen (window) and then put it as a Backbitmap of a FF form.

Thanks
Marc

Paul Squires

#2
Does something like the following work? I don't use transparent forms so this is new to me.

'--------------------------------------------------------------------------------
Function FORM1_WM_CREATE ( _
                         hWndForm As Dword, _      ' handle of Form
                         ByVal UserData As Long _  ' optional user defined Long value
                         ) As Long

   ' Must set the layered extended style for the Form
   SetWindowLong hWndForm, %GWL_EXSTYLE, GetWindowLong(hWndForm, %GWL_EXSTYLE) Or %WS_EX_LAYERED     

   ' Set the Form to 80% transparency
   SetLayeredWindowAttributes hWndForm, 0, (.80 * 255), %LWA_COLORKEY Or %LWA_ALPHA


End Function

Paul Squires
PlanetSquires Software

David Warner

It works here on Windows XP SP3.

Marc van Cauwenberghe

Thank you Paul but I think I have misrepresented what I want.
I want a form to be another shape so I thought making a form transparent an putting a
picture in the form in want would maybe give me that effect.

Marc

Pedro Marquez

Creating my library for FF3,


SetTransparentWindow.inc, copy in \CodeStore\PowerBASIC\My Snippets

use:
SetTransparentWindow(hWndForm,80) ---> 0 - 100


Function SetTransparentWindow (ByVal hWndForm As Dword, ByVal trans As Integer) As Integer
  If trans>0 And trans<101 Then
  SetWindowLong hWndForm, %GWL_EXSTYLE, GetWindowLong(hWndForm, %GWL_EXSTYLE) Or %WS_EX_LAYERED
  SetLayeredWindowAttributes hWndForm, 0, ((trans/100) * 255), %LWA_COLORKEY Or %LWA_ALPHA
  End If
End Function



Paul Squires

Paul Squires
PlanetSquires Software

Elias Montoya

Marc, this does what you want:

BACK_BITMAP = LoadImage(App.hInstance, "BACK_BITMAP", %IMAGE_BITMAP, AfxScaleX(33), AfxScaleY(34), 32768)
hReg     = TabRegion(BACK_BITMAP)
Call SetWindowRgn(HWND_MAINFORM_LABEL2, hReg, 1)



And this is the function to set it.

'=================================================================================
Function TabRegion (hBmp As Dword) As Long
      Local hDC As Dword, bm As Bitmap, rc As Rect
      Local hMem1DC As Dword, hMem2DC As Dword, hMemBmp As Dword
      Local bmi As BITMAPINFO, rdh As RGNDATAHEADER Ptr
      Local i As Long, j As Long, k As Long, m As Long, t As Long, tt As Long
      Local hRgn1 As Long, hRgn2 As Long
      Local sRgnData As String, lpRect As Rect Ptr

      hDC = GetDC(%HWND_DESKTOP)
      hMem1DC = CreateCompatibleDC (hDC)
      hMem2DC = CreateCompatibleDC (hDC)

      GetObject hBmp, Len(Bitmap), bm
      bmi.bmiHeader.biSize = SizeOf(bmi.bmiHeader)
      bmi.bmiHeader.biWidth = bm.bmWidth
      bmi.bmiHeader.biHeight = bm.bmHeight
      bmi.bmiHeader.biPlanes = 1
      bmi.bmiHeader.biBitCount = 32
      bmi.bmiHeader.biCompression = %BI_RGB

      hMemBmp = CreateDIBSection(hMem1DC, bmi, %DIB_RGB_COLORS, 0, 0, 0)
      GetObject hMemBmp, Len(Bitmap), bm

      SelectObject hMem1DC, hBmp
      SelectObject hMem2DC, hMemBmp

      BitBlt hMem2DC, 0, 0, bm.bmWidth, bm.bmHeight, hMem1DC, 0, 0, %SRCCOPY

      ReDim Ar(0) As Local Long At bm.bmBits
      t = (Ar((bm.bmHeight - 1) * bm.bmWidth) And &HFFFFFF) '<--- (0, 0) transparent

      sRgnData = String$(Len(RGNDATAHEADER) + Len(Rect) * 3800, 0)

      rdh = StrPtr(sRgnData)
      @rdh.nCount = 3800 + 1
      @rdh.dwSize = Len(RGNDATAHEADER)
      @rdh.iType = %RDH_RECTANGLES
      @rdh.rcBound.nLeft = 0
      @rdh.rcBound.nTop = 0
      @rdh.rcBound.nRight = bm.bmWidth
      @rdh.rcBound.nBottom = bm.bmHeight

      For j = 0 To bm.bmHeight - 1
         tt = bm.bmWidth * (bm.bmHeight - 1 - j): m = -1
         For i = 0 To bm.bmWidth
            If i = bm.bmWidth Then k = t Else k = (Ar(tt) And &HFFFFFF): Incr tt
            If k <> t Then
               If m = -1 Then m = i
            ElseIf m >= 0 Then
               If @rdh.nCount >= 3800 Then
                  If @rdh.nCount = 3800 Then
                     hRgn2 = ExtCreateRegion(ByVal 0, Len(RGNDATAHEADER) + (Len(Rect) * @rdh.nCount), ByVal rdh)
                     If hRgn1 = 0 Then hRgn1 = hRgn2 Else _
                        CombineRgn hRgn1, hRgn1, hRgn2, %RGN_OR: DeleteObject hRgn2
                  End If
                  lpRect = Len(RGNDATAHEADER) + rdh
                  @rdh.nCount = 0
               End If

               Incr @rdh.nCount
               @lpRect.nLeft = m
               @lpRect.nRight = i-1
               @lpRect.nTop = j
               @lpRect.nBottom = j + 1
               lpRect = lpRect + Len(Rect): m = -1
            End If
         Next
      Next

      hRgn2 = ExtCreateRegion(ByVal 0, Len(RGNDATAHEADER) + (Len(Rect) * @rdh.nCount), ByVal rdh)
      If hRgn1 = 0 Then hRgn1 = hRgn2 Else _
         CombineRgn hRgn1, hRgn1, hRgn2, %RGN_OR: DeleteObject hRgn2

      ReleaseDC %HWND_DESKTOP, hDC
      DeleteDC hMem1DC
      DeleteDC hMem2DC
      DeleteObject hMemBmp

      Function = hRgn1
End Function
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

Marc van Cauwenberghe

Hi Elias,
I will certainly have a look. Thanks.

Best regards,
Marc

Petrus Vorster

This was quite fun to play with, but i encountered something interesting.

When you set the opacity of the form, say to 50%, obviously all the buttons and text on it also has the same opacity. (Tried to create a from fade-in effect)
BUT, if you then change the opacity to 100%(fully opague) the TEXT on a button remains at the old opacity.
It looks very weird when you drag the form around and you can actually see little bits of your desktop showing in the text of the buttons.

While we are on this topic, is there a method where one can have the controls have a different opacity than the form?
-Regards
Peter