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 - classe ClsTransForm
Forum - Visual Basic 6 - classe ClsTransForm

Avatar
fusebyte (Normal User)
Expert


Messaggi: 332
Iscritto: 24/12/2008

Segnala al moderatore
Postato alle 13:19
Lunedė, 30/11/2009
Mi hanno spiegato che usando questa Classe si riesce a dare forme fantasiose ai Form.
Ho cercato informazioni senza successo.
Potrei avere per gentilezza esempi a riguardo?
Da come ho capito si usano le immagini e poi modificarle tramite questa Classe,ma potrei non aver capito molto bene cosa si intendeva.
Grazie anticipate.

Credo che la ClsTransForm.cls sia questo:
Codice sorgente - presumibilmente VB.NET

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsTransForm"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. 'Doug Gaede
  16. 'version 3.0.0
  17. 'December 28, 2000
  18. '----------------------------------------------------
  19. 'Notes:
  20. 'See the attached README.txt file.
  21. '----------------------------------------------------
  22. 'You are free to use, modify and distribute this code.
  23.  
  24. 'This class causes a Form, PictureBox, CommandButton,
  25. 'CheckBox or OptionButton to become shaped depending on the picture
  26. 'that is assigned to the Picture property.  One color in the picture
  27. 'will become the transparent color, depending on the color values passed to ShapeMe.
  28. 'The DragForm sub allows the user to drag a form that doesn't have a title bar.
  29.  
  30. 'Note that you MUST set certain properties for each object manually.
  31. 'See the notes in the code below to find out which and what values.
  32. 'I set as many as I could in code, but some can not
  33. 'because they are read-only at runtime.
  34.  
  35. Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
  36. Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
  37. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  38. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
  39. Private Declare Sub ReleaseCapture Lib "user32" ()
  40. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  41. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  42. Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
  43. Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
  44. Private Declare Function GetRegionData Lib "gdi32" (ByVal hRgn As Long, ByVal dwCount As Long, lpRgnData As Any) As Long
  45. Private Declare Function ExtCreateRegion Lib "gdi32" (lpXform As Any, ByVal nCount As Long, lpRgnData As Any) As Long
  46.  
  47. Private Const RGN_DIFF = 4
  48.  
  49. Dim CurRgn As Long, TempRgn As Long  ' Region variables
  50.  
  51. 'For dragging the form
  52. Private Const WM_NCLBUTTONDOWN = &HA1
  53. Private Const HTCAPTION = 2
  54.  
  55. 'these next 2 variables are used in the ShapeMe sub, but are declared here so the code in the Initialize... subs works
  56. Private objName As Object 'the object that will be worked on
  57. Private lngHeight As Long, lngWidth As Long 'height and width of object
  58.  
  59. Public Sub ShapeMe(Name As Object, Color As Long, Optional Load As Boolean = True, Optional FileName As String = vbNullString)
  60.  
  61. 'Name = a Form or PictureBox name.
  62. 'Color = the color to convert to transparent (easiest to use RGB function to pass in this value)
  63.  
  64. Dim X As Long, Y As Long 'points on form
  65. Dim booMiddleOfSet As Boolean 'used during the gathering of transparent points in ShapeMe
  66. Dim colPoints As Collection 'this will hold all usrPoints
  67. Set colPoints = New Collection
  68. Dim Z As Variant 'used during iteration through collection
  69. Dim lngTransY As Long 'these 3 variables hold each point that will be made transparent
  70. Dim lngTransStartX As Long
  71. Dim lngTransEndX As Long
  72. Dim intStoreScaleMode As Integer 'stores the commandbutton's form's scalemode
  73. Dim lngHDC As Long 'the hDC property of the object
  74.  
  75. 'set this so it can be used in the Initialization routine
  76. Set objName = Name
  77.  
  78. 'gather all points that need to be made transparent
  79. If TypeOf objName Is Form Then 'check to see if this is a form and use GetPixel function which is a little faster than VB's Point
  80.    
  81.     'don't forget to set the Picture to a picture, BorderStyle = None settings manually
  82.     'you CAN set Picture in your code since it isn't read-only at runtime
  83.     InitializeForFormOrPictureBox
  84.    
  85.     If FileName <> vbNullString And Load = True Then 'load region data from file
  86.         LoadRegionDataFromFile FileName
  87.        
  88.     Else 'do it the hard way
  89.         lngHDC = objName.hDC 'device context for object, used in GetPixel.  GetDC does not work for a form (?) so must use VB's hwnd property
  90.         booMiddleOfSet = False
  91.         For Y = 0 To lngHeight  ' Go through each column of pixels on form
  92.             lngTransY = Y
  93.             For X = 0 To lngWidth  ' Go through each line of pixels on form
  94.                 If GetPixel(lngHDC, X, Y) = Color Then  ' If the pixel's color is the transparency color, record it
  95.                     If booMiddleOfSet = False Then
  96.                         lngTransStartX = X
  97.                         lngTransEndX = X
  98.                         booMiddleOfSet = True
  99.                     Else
  100.                         lngTransEndX = X
  101.                     End If 'booMiddleOfSet = False
  102.                 Else
  103.                     If booMiddleOfSet Then
  104.                         colPoints.Add Array(lngTransY, lngTransStartX, lngTransEndX)
  105.                         booMiddleOfSet = False
  106.                     End If 'booMiddleOfSet = True
  107.                 End If 'GetPixel(lngHDC, X, Y) = Color
  108.             Next X
  109.         Next Y
  110.         'create base region to draw to; used below
  111.         CurRgn = CreateRectRgn(0, 0, lngWidth, lngHeight)
  112.     End If 'load region data from file
  113.    
  114. ElseIf TypeOf objName Is PictureBox Then 'if a PictureBox then use Point function; a little slower but works when GetPixel doesn't
  115.    
  116.     'don't forget to set the Picture to a picture, BorderStyle = None settings manually
  117.     'you CAN set Picture in your code since it isn't read-only at runtime
  118.     InitializeForFormOrPictureBox
  119.    
  120.     If FileName <> vbNullString And Load = True Then 'load region data from file
  121.         LoadRegionDataFromFile FileName
  122.        
  123.     Else 'do it the hard way
  124.         'don't need a DC since we don't use GetPixel here
  125.         booMiddleOfSet = False
  126.         For Y = 0 To lngHeight  ' Go through each column of pixels on form
  127.             lngTransY = Y
  128.             For X = 0 To lngWidth  ' Go through each line of pixels on form
  129.                 If objName.Point(X, Y) = Color Then
  130.                     If booMiddleOfSet = False Then
  131.                         lngTransStartX = X
  132.                         lngTransEndX = X
  133.                         booMiddleOfSet = True
  134.                     Else
  135.                         lngTransEndX = X
  136.                     End If 'booMiddleOfSet = False
  137.                 Else
  138.                     If booMiddleOfSet Then
  139.                         colPoints.Add Array(lngTransY, lngTransStartX, lngTransEndX)
  140.                         booMiddleOfSet = False
  141.                     End If 'booMiddleOfSet = True
  142.                 End If 'Name.Point(X, Y) = Color
  143.             Next X
  144.         Next Y
  145.         'create base region to draw to; used below
  146.         CurRgn = CreateRectRgn(0, 0, lngWidth, lngHeight)
  147.     End If 'load region data from file
  148.    
  149. ElseIf TypeOf objName Is CommandButton Or TypeOf objName Is OptionButton Or TypeOf objName Is CheckBox Then 'check to see if this is a button
  150.    
  151.     'don't forget to set Picture and DownPicture to pictures, and Style = Graphical settings manually
  152.     'you CAN set the picture properties in your code since they aren't read-only at runtime
  153.     'I tried moving this initialization to a separate sub, but the buttons didn't draw correctly...don't know why
  154.     'initialization
  155.     With objName
  156.         intStoreScaleMode = .parent.ScaleMode 'store it to set it back when done so you don't mess with the programmer's mind
  157.         .parent.ScaleMode = 3 'the button's form's scalemode must = pixel
  158.         .Caption = "" 'you can remove this line if you really want a caption, but it does weird things
  159.         .BackColor = Color 'necessary
  160.         .Refresh 'necessary
  161.         lngHDC = GetWindowDC(.hWnd) 'device context (DC) for object.
  162.         lngHeight = .Height 'faster to use a variable
  163.         lngWidth = .Width 'faster to use a variable
  164.     End With
  165.    
  166.     If FileName <> vbNullString And Load = True Then 'load region data from file
  167.         LoadRegionDataFromFile FileName
  168.        
  169.     Else 'do it the hard way
  170.         booMiddleOfSet = False
  171.         For Y = 0 To lngHeight ' Go through each column of pixels on form
  172.             lngTransY = Y
  173.             For X = 0 To lngWidth ' Go through each line of pixels on form
  174.                 If GetPixel(lngHDC, X, Y) = Color Then  ' If the pixel's color is the transparency color, record it
  175.                     If booMiddleOfSet = False Then
  176.                         lngTransStartX = X
  177.                         lngTransEndX = X
  178.                         booMiddleOfSet = True
  179.                     Else
  180.                         lngTransEndX = X
  181.                     End If 'booMiddleOfSet = False
  182.                 Else
  183.                     If booMiddleOfSet Then
  184.                         colPoints.Add Array(lngTransY, lngTransStartX, lngTransEndX)
  185.                         booMiddleOfSet = False
  186.                     End If 'booMiddleOfSet = True
  187.                 End If 'GetPixel(lngHDC, X, Y) = Color
  188.             Next X
  189.         Next Y
  190.         'create base region to draw to; used below
  191.         CurRgn = CreateRectRgn(2, 2, lngWidth - 2, lngHeight - 2)
  192.     End If 'load region data from file
  193.    
  194. Else 'not a supported object
  195.     Err.Raise vbObjectError + 512 + 2000, "TransForm", "Must pass in the name of a Form, PictureBox, CommandButton, CheckBox or OptionButton.  TransForm ShapeMe method failed."
  196.     Exit Sub
  197. End If 'test for each object
  198.  
  199. If FileName <> vbNullString And Load = True Then 'we loaded the region data from a file so...
  200.     'do nothing
  201. Else
  202.     'create the transparent areas
  203.     For Each Z In colPoints
  204.         TempRgn = CreateRectRgn(Z(1), Z(0), Z(2) + 1, Z(0) + 1)  ' Create a temporary pixel region for this pixel
  205.         CombineRgn CurRgn, CurRgn, TempRgn, RGN_DIFF  ' Combine temp pixel region with base region using RGN_DIFF to extract the pixel and make it transparent
  206.         DeleteObject (TempRgn)  ' Delete the temporary region and free resources
  207.     Next
  208. End If
  209.  
  210. If FileName <> vbNullString And Load = False Then 'save the region data to a file
  211.     SaveRegionDataToFile FileName
  212. End If
  213.  
  214. SetWindowRgn objName.hWnd, CurRgn, True  ' Finally set the windows region to the final product
  215. 'I do not use DeleteObject on the CurRgn, going with the advice in Dan Appleman's book:
  216. 'once set to a Window using SetWindowRgn, do not delete the region.
  217.  
  218. ReleaseDC objName.hWnd, lngHDC 'release the DC.  Does not hurt the form even though you got its DC from VB.
  219.  
  220. 'final cleanup if a commandbutton
  221. If TypeOf objName Is CommandButton Then
  222.     objName.parent.ScaleMode = intStoreScaleMode 'set it back
  223. End If
  224.  
  225. Set colPoints = Nothing
  226.  
  227. End Sub
  228. Public Sub DragForm(hWnd As Long, intButton As Integer)
  229.  
  230. On Error Resume Next
  231.  
  232. If intButton = vbLeftButton Then
  233.     'Move the borderless form...
  234.     ReleaseCapture
  235.     SendMessage hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0
  236. End If
  237.  
  238. End Sub
  239.  
  240. Private Sub InitializeForFormOrPictureBox()
  241. 'initialization for form or picturebox
  242. With objName
  243.     .AutoRedraw = True 'object must have this setting
  244.     .ScaleMode = 3 'object must have this setting
  245.     .ClipControls = False 'supposedly improves drawing performance.  Haven't seen it but doesn't hurt.
  246.     lngHeight = .ScaleHeight 'faster to use a variable
  247.     lngWidth = .ScaleWidth 'faster to use a variable
  248. End With
  249.  
  250. End Sub
  251.  
  252. Private Sub SaveRegionDataToFile(ByVal sPath As String)
  253. 'this sub was pulled from www.vbaccelerator.com
  254. Dim iFile As Long
  255. Dim nBytes As Long
  256. Dim b() As Byte
  257.  
  258. On Error GoTo ErrorHandler ' Out of memory
  259.      
  260.       nBytes = GetRegionData(CurRgn, 0, ByVal 0&)
  261.       If nBytes > 0 Then
  262.          ReDim b(0 To nBytes - 1) As Byte
  263.          If nBytes = GetRegionData(CurRgn, nBytes, b(0)) Then
  264.             On Error Resume Next ' Attempt to kill file
  265.             Kill sPath
  266.             On Error GoTo ErrorHandler ' Error handler checks for file error
  267.             iFile = FreeFile
  268.             Open sPath For Binary Access Write Lock Read As #iFile
  269.             Put #iFile, , b
  270.             Close #iFile
  271.          Else
  272.             Err.Raise vbObjectError + 512 + 2001, "TransForm", "Unable to get region data in SaveRegionDataToFile"
  273.          End If
  274.       Else
  275.          Err.Raise vbObjectError + 512 + 2002, "TransForm", "Unable to determine size of region in SaveRegionDataToFile"
  276.       End If
  277.    
  278.    Exit Sub
  279.    
  280. ErrorHandler:
  281. Dim lErr As Long, sErr As String
  282.    lErr = Err.Number: sErr = Err.Description
  283.    If iFile > 0 Then
  284.       Close #iFile
  285.    End If
  286.    Err.Raise lErr, "TransForm", sErr
  287.    Exit Sub
  288.    
  289. End Sub
  290.  
  291. Private Sub LoadRegionDataFromFile(ByVal sFileName As String)
  292. 'this sub was pulled from www.vbaccelerator.com
  293. Dim iFile As Long
  294. Dim b() As Byte
  295. Dim dwCount As Long
  296. On Error GoTo ErrorHandler
  297.  
  298.    iFile = FreeFile
  299.    Open sFileName For Binary Access Read Lock Write As #iFile
  300.    ReDim b(0 To LOF(iFile) - 1) As Byte
  301.    Get #iFile, , b
  302.    Close #iFile
  303.    
  304.    dwCount = UBound(b) - LBound(b) + 1
  305.    CurRgn = ExtCreateRegion(ByVal 0&, dwCount, b(0))
  306.  
  307.    Exit Sub
  308.  
  309. ErrorHandler:
  310. Dim lErr As Long, sErr As String
  311.    lErr = Err.Number: sErr = Err.Description
  312.    If iFile > 0 Then
  313.       Close #iFile
  314.    End If
  315.    Err.Raise lErr, App.EXEName & ".cDIBSectionRegion", sErr
  316.    Exit Sub
  317. End Sub



,poi come si applicherebbe? (sempre sia giusto il file)


Ciao












Ciao

Ultima modifica effettuata da fusebyte il 30/11/2009 alle 13:29
PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 14:41
Lunedė, 30/11/2009
tu vuoi modificare la forma del form... ti consiglio di dare un'occhiata a ShapeCreator, programma fatto appunto per questo scopo (se googli troverai il download)

PM Quote
Avatar
fusebyte (Normal User)
Expert


Messaggi: 332
Iscritto: 24/12/2008

Segnala al moderatore
Postato alle 15:42
Lunedė, 30/11/2009
Trovo Shape Creator 3.2 di Claudio Gucchierato ma non riesco a scaricarlo,pčerche' ovunque
lo trovo non c'č la voce download.
Potresti cortesemente mettermi un link di scarico?

Grazie 1000

Ciao

PM Quote
Avatar
lorenzo (Normal User)
Guru


Messaggi: 1178
Iscritto: 15/04/2008

Segnala al moderatore
Postato alle 16:34
Lunedė, 30/11/2009

Ultima modifica effettuata da lorenzo il 30/11/2009 alle 16:34
PM Quote
Avatar
fusebyte (Normal User)
Expert


Messaggi: 332
Iscritto: 24/12/2008

Segnala al moderatore
Postato alle 17:21
Lunedė, 30/11/2009
Grazie Lorenzo,..evidentemente so proprio negato io a trovare le cose.

Ciao

PM Quote