Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Basic 6 - Form trasparente
Forum - Visual Basic 6 - Form trasparente

Avatar
.:ViRuS:. (Ex-Member)
Pro


Messaggi: 174
Iscritto: 01/06/2008

Segnala al moderatore
Postato alle 12:35
Domenica, 14/06/2009
Ciao a tutti ragazzi volevo sapere.. pura curiosità... è possibile far si che un form diventi trasparente?


so che si può.. ho degli esempi sul mio pc che fanno appunto questo..


però io vorrei che il form diventi trasparente lasciando solo degli shape


per esempio metto una linea rossa, e il form che è trasparente

quando lo apro il form nn si vede.. ma si vede solo la linea..

Spero di essere stato chiaro...

Potete darmi una mano voi? Grazie ;)

Ultima modifica effettuata da .:ViRuS:. il 14/06/2009 alle 12:36
PM Quote
Avatar
giuggiolo (Normal User)
Newbie


Messaggi: 6
Iscritto: 14/06/2009

Segnala al moderatore
Postato alle 16:11
Domenica, 14/06/2009

ciao virus!

ho avuto la necessità anche io di rendere trasparente un form in un mio programma e cercando in Internet ho trovato il seguente codice...

incolla questo nel tuo form all'avvio:

Codice sorgente - presumibilmente VB.NET

  1. ' form trasparente
  2.     For Each controllo In Me.Controls
  3.         NumControlli = NumControlli + 1
  4.     Next
  5.     x = 0
  6.     Me.ScaleMode = vbPixels
  7.     ReDim obj(NumControlli - 1, 3) As Integer
  8.     For Each controllo In Me.Controls
  9.         On Error GoTo prossimo
  10.         obj(x, 0) = controllo.Left
  11.         obj(x, 1) = controllo.Top
  12.         obj(x, 2) = controllo.Width
  13.         obj(x, 3) = controllo.Height
  14.         x = x + 1
  15. 'prossimo:
  16.     Next
  17.     SetTransparent frmPrincipale, obj
  18.     ' fine form trasparente




e questo codice dove vuoi, in un modulo o nello stesso form:


Codice sorgente - presumibilmente VB.NET

  1. Public Sub SetTransparent(frm As Form, obj() As Integer)
  2. Dim rctClient As RECT, rctFrame As RECT
  3. Dim hClient As Long, hFrame As Long, hObj As Long
  4. Dim Start As Integer, Finish As Integer, i As Integer
  5. Dim lpTL As POINTAPI, lpBR As POINTAPI
  6.  
  7. GetWindowRect frm.hWnd, rctFrame
  8. GetClientRect frm.hWnd, rctClient
  9.  
  10. lpTL.x = rctFrame.Left
  11. lpTL.Y = rctFrame.Top
  12. lpBR.x = rctFrame.Right
  13. lpBR.Y = rctFrame.Bottom
  14. ScreenToClient frm.hWnd, lpTL
  15. ScreenToClient frm.hWnd, lpBR
  16. rctFrame.Left = lpTL.x
  17. rctFrame.Top = lpTL.Y
  18. rctFrame.Right = lpBR.x
  19. rctFrame.Bottom = lpBR.Y
  20. rctClient.Left = Abs(rctFrame.Left)
  21. rctClient.Top = Abs(rctFrame.Top)
  22. rctClient.Right = rctClient.Right + Abs(rctFrame.Left)
  23. rctClient.Bottom = rctClient.Bottom + Abs(rctFrame.Top)
  24. rctFrame.Right = rctFrame.Right + Abs(rctFrame.Left)
  25. rctFrame.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
  26. rctFrame.Top = 0
  27. rctFrame.Left = 0
  28. hClient = CreateRectRgn(rctClient.Left, rctClient.Top, rctClient.Right, rctClient.Bottom)
  29. hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, rctFrame.Right, rctFrame.Bottom)
  30. Dim mode As Integer
  31. mode = frm.ScaleMode
  32. frm.ScaleMode = 3
  33. CombineRgn hFrame, hClient, hFrame, RGN_XOR
  34. Start = LBound(obj)
  35. Finish = UBound(obj)
  36.  
  37. For i = Start To Finish
  38.     hObj = CreateRectRgn(obj(i, 0), obj(i, 1), obj(i, 0) + obj(i, 2), obj(i, 1) + obj(i, 3))
  39.     CombineRgn hFrame, hObj, hFrame, RGN_OR
  40. Next
  41.  
  42. SetWindowRgn frm.hWnd, hFrame, True
  43. frm.ScaleMode = mode
  44. End Sub




in un modulo inserisci queste righe:

Codice sorgente - presumibilmente VB.NET

  1. Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long
  2. Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long
  3. Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
  4. Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  5. Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
  6. Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
  7.  
  8. Public Const RGN_AND = 1
  9. Public Const RGN_COPY = 5
  10. Public Const RGN_DIFF = 4
  11. Public Const RGN_OR = 2
  12. Public Const RGN_XOR = 3




finito! :)
Facci sapere!

Giuggiolo

PM Quote
Avatar
.:ViRuS:. (Ex-Member)
Pro


Messaggi: 174
Iscritto: 01/06/2008

Segnala al moderatore
Postato alle 14:00
Lunedì, 15/06/2009
Ciao Giuggiolo grazie della risposta.


c'è un'errore quando faccio partire il programma.. mi dice tipo definito dall'utente non definito.. e mi segnala


Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long

dov'è può essere il problema?

PM Quote
Avatar
theprogrammer (Normal User)
Guru^2


Messaggi: 2509
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 14:54
Lunedì, 15/06/2009
Testo quotato

Postato originariamente da .:ViRuS:.:
Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRECT As RECT) As Long
dov'è può essere il problema?



Ovviamente manca la dichiarazione della struttura RECT

PM Quote
Avatar
giuggiolo (Normal User)
Newbie


Messaggi: 6
Iscritto: 14/06/2009

Segnala al moderatore
Postato alle 18:02
Lunedì, 15/06/2009
si, scusami, non ti ho postato le dichiarazioni dei tipi RECT e POINTAPI!

eccoli...

Codice sorgente - presumibilmente VB.NET

  1. Type POINTAPI
  2.     x As Long
  3.     Y As Long
  4. End Type
  5.  
  6.  
  7. Type RECT
  8.     Left As Long
  9.     Top As Long
  10.     Right As Long
  11.     Bottom As Long
  12. End Type



facci sapere!

Giulio

PM Quote
Avatar
.:ViRuS:. (Ex-Member)
Pro


Messaggi: 174
Iscritto: 01/06/2008

Segnala al moderatore
Postato alle 22:00
Lunedì, 15/06/2009
ho problemi con questa parte di codice:

Codice sorgente - presumibilmente VB.NET

  1. Private Sub Form_Load()
  2.     ' form trasparente
  3.     For Each controllo In Me.Controls
  4.         NumControlli = NumControlli + 1
  5.     Next
  6.     x = 0
  7.     Me.ScaleMode = vbPixels
  8.     ReDim obj(NumControlli - 1, 3) As Integer
  9.     For Each controllo In Me.Controls
  10.         'On Error GoTo prossimo
  11.         obj(x, 0) = controllo.Left
  12.         obj(x, 1) = controllo.Top
  13.         obj(x, 2) = controllo.Width
  14.         obj(x, 3) = controllo.Height
  15.         x = x + 1
  16. 'prossimo:
  17.     Next
  18.     SetTransparent Form1, obj
  19.     ' fine form trasparente
  20. End Sub



mi dice :indice non compreso nell'intervallo..
e mi evidenzia questo: ReDim obj(NumControlli - 1, 3) As Integer

scusate la mia ignoranza ma non riesco a capire cosa sia...


EDIT:

se metto controlli tipo commandbutton option o altro.. non da problemi...

ma se non metto controlli O METTO SOLO UNO SHAPE SUL FORM..

mi da questo errore :S


e la mia domanda era proprio questa.. form trasparente solo con uno shape sopra.. è possibile?

RIEDIT:

con gli shape non da errori, però si vedono male.. cioè a metà non tutti interi.

come mai?

Ultima modifica effettuata da .:ViRuS:. il 15/06/2009 alle 22:05
PM Quote
Avatar
ruggy94 (Member)
Guru


Messaggi: 890
Iscritto: 21/04/2008

Segnala al moderatore
Postato alle 22:10
Lunedì, 15/06/2009
Testo quotato

Postato originariamente da .:ViRuS:.:
con gli shape non da errori, però si vedono male.. cioè a metà non tutti interi.


In che senso? Magari posta uno screen per capirci meglio.

PM Quote
Avatar
.:ViRuS:. (Ex-Member)
Pro


Messaggi: 174
Iscritto: 01/06/2008

Segnala al moderatore
Postato alle 22:41
Lunedì, 15/06/2009
RIIISOLTO :D


ho cambiato tipo di codice facendo delle ricerche...

ed ora funziona...


per gli interessati ecco il codice:


in un modulo:

Codice sorgente - presumibilmente VB.NET

  1. ''
  2. Option Explicit
  3. Public visu As String
  4.  
  5. Const SW_SHOWNORMAL = 1
  6.  
  7. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  8.     (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
  9.     ByVal lpParameters As String, ByVal lpDirectory As String, _
  10.     ByVal nShowCmd As Long) As Long




nel form:

Codice sorgente - presumibilmente VB.NET

  1. Option Explicit
  2. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, _
  3. ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  4. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As _
  5. Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal _
  6. nCombineMode As Long) As Long
  7. Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As _
  8. Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
  9.  
  10. Public Sub FormTrasparente(frm As Form)
  11. on error resume next
  12.     frm.ScaleMode = vbPixels
  13.     Const RGN_DIFF = 4
  14.     Const RGN_OR = 2
  15.  
  16.     Dim rgn_esclusa As Long
  17.     Dim rgn_inclusa As Long
  18.     Dim rgn_combinate As Long
  19.     Dim wid As Single
  20.     Dim hgt As Single
  21.     Dim border_width As Single
  22.     Dim title_height As Single
  23.     Dim control_rgn As Long
  24.  
  25.     If frm.WindowState = vbMinimized Then Exit Sub
  26.  
  27.     ' Crea la regione principale del form.
  28.     wid = frm.ScaleX(frm.Width, vbTwips, vbPixels)
  29.     hgt = frm.ScaleY(frm.Height, vbTwips, vbPixels)
  30.     rgn_esclusa = CreateRectRgn(0, 0, wid, hgt)
  31.  
  32.     border_width = (wid - frm.ScaleWidth) / 2
  33.     title_height = hgt - border_width - frm.ScaleHeight
  34.     rgn_inclusa = CreateRectRgn(border_width, title_height, wid - border_width, _
  35.         hgt - border_width)
  36.  
  37.     ' Esclude la sezione inclusa da quella esclusa.
  38.     rgn_combinate = CreateRectRgn(0, 0, 0, 0)
  39.     CombineRgn rgn_combinate, rgn_esclusa, rgn_inclusa, RGN_DIFF
  40.    For Each ctl In frm.Controls
  41.         If ctl.Container Is frm Then
  42.             ctl_left = frm.ScaleX(ctl.Left, frm.ScaleMode, vbPixels) _
  43.                 + border_width
  44.             ctl_top = frm.ScaleX(ctl.Top, frm.ScaleMode, vbPixels) + title_height
  45.             ctl_right = frm.ScaleX(ctl.Width, frm.ScaleMode, vbPixels) + ctl_left
  46.             ctl_bottom = frm.ScaleX(ctl.Height, frm.ScaleMode, vbPixels) + ctl_top
  47.             control_rgn = CreateRectRgn(ctl_left, ctl_top, ctl_right, ctl_bottom)
  48.             CombineRgn rgn_combinate, rgn_combinate, control_rgn, RGN_OR
  49.         End If
  50.  
  51.     Next ctl
  52.  
  53.     'Limita la form alla regione
  54.     SetWindowRgn frm.hwnd, rgn_combinate, True
  55. End Sub
  56.  
  57. Private Sub Form_Resize()
  58.     FormTrasparente Me
  59. End Sub





Ultima modifica effettuata da .:ViRuS:. il 15/06/2009 alle 23:06
PM Quote