Avatar billede danielhep Nybegynder
06. januar 2002 - 21:15 Der er 9 kommentarer og
1 løsning

Gennemsigtig

Hvilket API opkald skal man bruge for at gøre noget af en piturebox gennemsigtig??

Det som skal blive gennemsigtigt er farven sort som er i picturebox1.

Avatar billede sjh Nybegynder
06. januar 2002 - 22:44 #1
\'----------------------------------------- Form1 eller Module1 -----------------------------------------
Option Explicit

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 Declare Function CreateCompatibleDC Lib \"gdi32\" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib \"gdi32\" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function GetObject Lib \"gdi32\" Alias \"GetObjectA\" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) 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 CombineRgn Lib \"gdi32\" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteDC Lib \"gdi32\" (ByVal hdc As Long) As Long
Private Declare Function GetPixel Lib \"gdi32\" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Public Function FitToPictureBox(cPicture As StdPicture, cTransparent As Long) As Long
Dim hRgn As Long, tRgn As Long
Dim x As Integer, y As Integer, X0 As Integer
Dim hdc As Long, BM As BITMAP

hdc = CreateCompatibleDC(0)
  If hdc Then
    SelectObject hdc, cPicture
    GetObject cPicture, Len(BM), BM
    hRgn = CreateRectRgn(0, 0, BM.bmWidth, BM.bmHeight)
    For y = 0 To BM.bmHeight
      For x = 0 To BM.bmWidth
        While x <= BM.bmWidth And GetPixel(hdc, x, y) <> cTransparent
          x = x + 1
            Wend
              X0 = x
            While x <= BM.bmWidth And GetPixel(hdc, x, y) = cTransparent
          x = x + 1
        Wend
        If X0 < x Then
          tRgn = CreateRectRgn(X0, y, x, y + 1)
          CombineRgn hRgn, hRgn, tRgn, 4
          DeleteObject tRgn
        End If
      Next x
    Next y
    FitToPictureBox = hRgn
    DeleteObject SelectObject(hdc, cPicture)
  End If
DeleteDC hdc
End Function
\'----------------------------------------- Form1 eller Module1 -----------------------------------------


\'----------------------------------------------- Form1 -------------------------------------------------
Private Sub Form_Load()
Dim hRgn As Long

Picture1.BorderStyle = 0
Picture1.AutoSize = True

  hRgn = FitToPictureBox(Picture1.Picture, vbBlack)
  SetWindowRgn Picture1.hwnd, hRgn, True
  DeleteObject hRgn
End Sub
\'----------------------------------------------- Form1 -------------------------------------------------
Avatar billede sjh Nybegynder
06. januar 2002 - 22:48 #2
hvis du smider det i et module skal \"SetWindowRgn\" og \"DeleteObject\" se sådan ud.


Declare Function SetWindowRgn Lib \"user32\" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Declare Function DeleteObject Lib \"gdi32\" (ByVal hObject As Long) As Long

Avatar billede danielhep Nybegynder
06. januar 2002 - 23:48 #3
Der må findes en mere enkel metode??
Avatar billede sjh Nybegynder
07. januar 2002 - 00:06 #4
Måske.
Avatar billede danielhep Nybegynder
07. januar 2002 - 11:21 #5
Kender du en?
Avatar billede tjacob Juniormester
07. januar 2002 - 12:15 #6
Der er ingen vej uden om API-kald, hvis du vil gøre formen gennemsigtig, da du er nødt til at gå \"udenfor\" VB (altså ind i Windows interface), for at hente den bitmap der tegner skærmen bagved din form.

MVH tjacob
Avatar billede sjh Nybegynder
07. januar 2002 - 20:58 #7
Jeg har prøvet lidt med \"BeginPath\" men kan ikke få det til at funke med et transparent billede men det kan i måske. :-)


Private Declare Function BeginPath Lib \"gdi32\" (ByVal hdc As Long) As Long
Private Declare Function EndPath Lib \"gdi32\" (ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib \"gdi32\" (ByVal hdc As Long) As Long
Private Declare Function TextOut Lib \"gdi32\" Alias \"TextOutA\" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) 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 Sub Form_Click()
  Unload Me
End Sub

Private Sub Form_Load()
Dim hRgn As Long
Const sText = \"Click Here!\"
  Me.Width = 7400
  Me.FontName = \"Times New Roman\"
  Me.FontSize = 72
  Me.BackColor = vbRed

  BeginPath Me.hdc
 
  TextOut Me.hdc, 0, 0, sText, Len(sText)
 
  EndPath Me.hdc

  hRgn = PathToRegion(Me.hdc)
  SetWindowRgn Me.hWnd, hRgn, True
  DeleteObject hRgn
End Sub
Avatar billede danielhep Nybegynder
09. januar 2002 - 16:41 #8
SJH>>>

Jeg har prøvet din første store kode...Den virker da, men der er et eller andet som ikke er helt rigtigt...Jeg har lavet et test billede \"picture1boxen\".  Billedet er sort og inde i midten er det nogle røde streger.
De røde streger bliver hvide og man kan se noget kode bag ved den...Underligt....Hvad kan det skyldes??
Avatar billede sjh Nybegynder
10. januar 2002 - 05:59 #9
Det skulle virke prøv at hente filen her og se.

http://hjem.get2net.dk/sjh/eksperten/155198/Eks155198.zip
Avatar billede danielhep Nybegynder
17. januar 2002 - 12:37 #10
Sjh..

Sorry ventetiden, men jeg er travlt...
Jeg synes du har fortjent de point..så, her er de..
:)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester