home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / Check_Box_2141481252009.psc / clsTrans.cls < prev    next >
Text File  |  2009-01-22  |  8KB  |  206 lines

  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 = "clsTrans"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '---------------------------------------------------------------------------------------
  15. ' Modulo         : clsTrans (M≤dulo de clase)
  16. ' Proyecto       : TransTest
  17. ' Autor          : el_coco
  18. ' Fecha          : 18/01/2009 00:46 (remod: 22/01/2009 06:45)
  19. ' Distribucion   : Remember the authors always...
  20. ' Creditos       : Benjamin Wilger (RegionFromBitmap)
  21. ' Propostio      : Makes a simple checkbox into transparent!
  22. ' Funciones/Subs : DoIT *
  23. '---------------------------------------------------------------------------------------
  24. '
  25.  
  26. Private Type BITMAPINFOHEADER
  27.     biSize As Long
  28.     biWidth As Long
  29.     biHeight As Long
  30.     biPlanes As Integer
  31.     biBitCount As Integer
  32.     biCompression As Long
  33.     biSizeImage As Long
  34.     biXPelsPerMeter As Long
  35.     biYPelsPerMeter As Long
  36.     biClrUsed As Long
  37.     biClrImportant As Long
  38. End Type
  39.  
  40. Private Type RGBQUAD
  41.     rgbBlue As Byte
  42.     rgbGreen As Byte
  43.     rgbRed As Byte
  44.     rgbReserved As Byte
  45. End Type
  46.  
  47. Private Type BITMAPINFO
  48.     bmiHeader As BITMAPINFOHEADER
  49.     bmiColors As RGBQUAD
  50. End Type
  51.  
  52. Private Type RECT
  53.     Left As Long
  54.     Top As Long
  55.     Right As Long
  56.     Bottom As Long
  57. End Type
  58.  
  59. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  60. Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hDC As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
  61. Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  62. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  63. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  64. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
  65. Private Declare Function OleTranslateColor Lib "olepro32.dll" (ByVal OLE_COLOR As Long, ByVal hPalette As Long, pccolorref As Long) As Long
  66. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  67. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
  68. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  69. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  70. Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
  71. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  72. Private Declare Function GetBkColor Lib "gdi32" (ByVal hDC As Long) As Long
  73. Private Declare Function RedrawWindow Lib "user32" (ByVal hwnd As Long, lprcUpdate As Any, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long
  74.  
  75.  
  76. Private Const BI_RGB As Long = 0&
  77. Private Const RGN_OR As Long = 2&
  78. Private Const DIB_RGB_COLORS As Long = 0&
  79. Private Const RDW_INVALIDATE As Long = &H1
  80.  
  81. Public Function DoIT(objSource As Object) As Boolean
  82.     Dim lngRegion As Long
  83.     Dim lngBackColor As Long
  84.     Dim lngHeight As Long
  85.     Dim lngWidth As Long
  86.     Dim lnghWnd As Long
  87.     Dim rcRect As RECT
  88.     
  89.     On Local Error Resume Next
  90.     If objSource.hwnd = 0 Or objSource.BackColor = 0 Then 'trap if object hasnt .backcolor or .hwnd
  91.         DoIT = False
  92.         Exit Function
  93.     End If
  94.     If Err.Number = 438 Then 'Object doesnt allow .backcolor or .hwnd!
  95.         DoIT = False
  96.         Exit Function
  97.     End If
  98.     
  99.     lnghWnd = objSource.hwnd
  100.     
  101.     Call GetWindowRect(lnghWnd, rcRect) 'get rect of object
  102.     
  103.     lngHeight = rcRect.Bottom - rcRect.Top 'height
  104.     lngWidth = rcRect.Right - rcRect.Left 'width
  105.     
  106.     If lngHeight > 0 Or lngWidth > 0 Then
  107.         Call OleTranslateColor(objSource.BackColor, 0, lngBackColor) 'translate color
  108.         lngRegion = RegionFromBitmap(lnghWnd, lngHeight, lngWidth, lngBackColor) 'create region
  109.         If lngRegion = 0 Then
  110.             DoIT = False
  111.         Else
  112.             If SetWindowRgn(lnghWnd, lngRegion, True) = 1 Then 'set new region
  113.                 If RedrawWindow(lnghWnd, ByVal 0&, ByVal 0&, RDW_INVALIDATE) = 0 Then
  114.                     DoIT = False
  115.                 Else
  116.                     DoIT = True
  117.                 End If
  118.             Else
  119.                 DoIT = False
  120.             End If
  121.         End If
  122.         DeleteObject lngRegion 'delete region to free some memory
  123.     Else
  124.         DoIT = False
  125.     End If
  126. End Function
  127.  
  128. Private Function RegionFromBitmap(ByVal lnghWnd As Long, ByVal lngH As Long, ByVal lngW As Long, ByVal lngTransColor As Long) As Long
  129.     Dim lngRetr As Long, lngHeight As Long, lngWidth As Long
  130.     Dim lngRgnFinal As Long, lngRgnTmp As Long
  131.     Dim lngStart As Long
  132.     Dim x As Long, y As Long
  133.     Dim hDC As Long
  134.     
  135.     Dim bi24BitInfo As BITMAPINFO
  136.     Dim iBitmap As Long
  137.     Dim BWidth As Long
  138.     Dim BHeight As Long
  139.     Dim iDC As Long
  140.     Dim PicBits() As Byte
  141.     Dim Col As Long
  142.     
  143.     hDC = GetDC(lnghWnd)
  144.     
  145.     lngWidth = lngW '- 1
  146.     lngHeight = lngH - 1
  147.  
  148.     BWidth = (lngW \ 4) * 4 + 4
  149.     BHeight = lngH
  150.  
  151.     'Bitmap-Header
  152.     With bi24BitInfo.bmiHeader
  153.         .biBitCount = 24
  154.         .biCompression = BI_RGB
  155.         .biPlanes = 1
  156.         .biSize = Len(bi24BitInfo.bmiHeader)
  157.         .biWidth = BWidth
  158.         .biHeight = BHeight + 1
  159.     End With
  160.     
  161.     ReDim PicBits(0 To bi24BitInfo.bmiHeader.biWidth * 3 - 1, 0 To bi24BitInfo.bmiHeader.biHeight - 1)
  162.     
  163.     iDC = CreateCompatibleDC(hDC)
  164.     iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
  165.     Call SelectObject(iDC, iBitmap)
  166.     Call BitBlt(iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, hDC, 0, 0, vbSrcCopy)
  167.     Call GetDIBits(hDC, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, PicBits(0, 0), bi24BitInfo, DIB_RGB_COLORS)
  168.     
  169.     'DIB-DC
  170.     Call DeleteDC(iDC)
  171.     'Bitmap
  172.     Call DeleteObject(iBitmap)
  173.  
  174.     lngRgnFinal = CreateRectRgn(0, 0, 0, 0)
  175.     For y = 0 To lngHeight
  176.         x = 0
  177.         Do While x < lngWidth
  178.             Do While x < lngWidth And _
  179.                 RGB(PicBits(x * 3 + 2, lngHeight - y + 1), _
  180.                     PicBits(x * 3 + 1, lngHeight - y + 1), _
  181.                     PicBits(x * 3, lngHeight - y + 1) _
  182.                     ) = lngTransColor
  183.                 
  184.                 x = x + 1
  185.             Loop
  186.             If x <= lngWidth Then
  187.                 lngStart = x
  188.                 Do While x < lngWidth And _
  189.                     RGB(PicBits(x * 3 + 2, lngHeight - y + 1), _
  190.                         PicBits(x * 3 + 1, lngHeight - y + 1), _
  191.                         PicBits(x * 3, lngHeight - y + 1) _
  192.                         ) <> lngTransColor
  193.                     x = x + 1
  194.                 Loop
  195.                 If x + 1 > lngWidth Then x = lngWidth
  196.                 lngRgnTmp = CreateRectRgn(lngStart, y, x, y + 1)
  197.                 lngRetr = CombineRgn(lngRgnFinal, lngRgnFinal, lngRgnTmp, RGN_OR)
  198.                 DeleteObject lngRgnTmp
  199.             End If
  200.         Loop
  201.     Next
  202.     
  203.     RegionFromBitmap = lngRgnFinal
  204. End Function
  205.  
  206.