\'----------------------------------------- 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 -------------------------------------------------
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
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.
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
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??
Sorry ventetiden, men jeg er travlt... Jeg synes du har fortjent de point..så, her er de.. :)
Synes godt om
Ny brugerNybegynder
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.