Gå til innhold

Bakgrunnesfarge i picturebox til å bli transparent


Anbefalte innlegg

Videoannonse
Annonse

Denne metoden vil dessverre ikke fungere med påtegnede kontroller som labeller og liknende - til det må du finne på noe annet. Men jeg henviser til den likevel:

 

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Const RGN_AND = 1
Private Const RGN_COPY = 5
Private Const RGN_DIFF = 4
Private Const RGN_OR = 2
Private Const RGN_XOR = 3

Private Type POINTAPI
   X As Long
   Y As Long
End Type

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type
       
Public Sub MakeTransparent(oControl As Control)

   On Error Resume Next
   Dim rctClient As RECT, rctFrame As RECT
   Dim hClient As Long, hFrame As Long, hTemp As Long
   Dim lpTL As POINTAPI, lpBR As POINTAPI
   Dim Control As Control, hWnd As Long
   
   GetWindowRect oControl.hWnd, rctFrame
   GetClientRect oControl.hWnd, rctClient
   
   lpTL.X = rctFrame.Left
   lpTL.Y = rctFrame.Top
   lpBR.X = rctFrame.Right
   lpBR.Y = rctFrame.Bottom
   
   ScreenToClient oControl.hWnd, lpTL
   ScreenToClient oControl.hWnd, lpBR
   
   ' Rammen
   With rctFrame
       rctFrame.Left = lpTL.X
       rctFrame.Top = lpTL.Y
       rctFrame.Right = lpBR.X
       rctFrame.Bottom = lpBR.Y
   End With
   
   ' Selve klientområdet
   With rctClient
       .Left = Abs(rctFrame.Left)
       .Top = Abs(rctFrame.Top)
       .Right = rctClient.Right + Abs(rctFrame.Left)
       .Bottom = rctClient.Bottom + Abs(rctFrame.Top)
   End With
   
   ' Rammeposisjon og -størrelse
   With rctFrame
       rctFrame.Right = rctFrame.Right + Abs(rctFrame.Left)
       rctFrame.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
   End With

   ' For å ikke gjøre rammen gjennomsiktig, må vi gjøre følgende
   hClient = CreateRectRgn(rctClient.Left, rctClient.Top, rctClient.Right, rctClient.Bottom)
   hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, rctFrame.Right, rctFrame.Bottom)
   CombineRgn hFrame, hClient, hFrame, RGN_XOR

   ' Vis alle kontroller i den omliggende formen
   For Each Control In oControl.Parent
       
       ' Fortsett kun dersom dette ikke der den kontrollen vi ønsker å gjøre gjennomsiktig
       If Not Control Is oControl Then
           
           ' Create the region of this control
           hTemp = CreateRectRgn(ConvertPixels(oControl, Control.Left) + rctClient.Left, ConvertPixels(oControl, Control.Top) + rctClient.Top, _
            ConvertPixels(oControl, Control.Left + Control.Width) + rctClient.Left, ConvertPixels(oControl, Control.Top + Control.Height) + rctClient.Left)
       
           ' Make the region visible
           CombineRgn hFrame, hTemp, hFrame, RGN_XOR
           
           ' Delete region
           DeleteObject hTemp
       
       End If
   
   Next

   ' Set regionen til denne kontrollen
   SetWindowRgn oControl.hWnd, hFrame, True
   
End Sub

Public Function ConvertPixels(objSource As Object, lngValue As Single) As Single

   ' Konverter til pixels
   ConvertPixels = objSource.ScaleX(lngValue, objSource.ScaleMode, vbPixels)

End Function

 

For å gjøre en kontroll gjennomsiktig, eksempelvis picTest, gjør du som følger:

 

MakeTransparent picTest

Lenke til kommentar

Du må ha med alt etsteds tilgjengelig.

 

Inkluder koden i formen din, eller legg det eventuelt inn i en modul; legg dernest til følgende:

 

Private Sub Form_Load()

   MakeTransparent picTest

End Sub

 

Dette vil gjøre bildeboksen picTest gjennomsiktig ved oppstart av formen. Verre er det ikke.

Lenke til kommentar

Kan du ikke heller benytte deg av en Image-kontroll istedenfor? I så fall kan du simpelthen lagre bildet som en gjennomsiktig GIF-fil (I IrfanView, sett Save transparent color), hvorpå du legger til denne filen i Picture-egenskapen til kontrollen.

 

Dette skulle i hvert fall være meget lettere enn å rote rundt med avanserte API-kall for å gjøre likeledes med en bildeboks.

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...