home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Enterprise206415552007.psc / cDIBSection.cls < prev    next >
Text File  |  2006-10-24  |  6KB  |  187 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 = "cDIBSection"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ''''''''''''''''''''''''''''
  16.  
  17. Private Const BI_RGB = 0&
  18. Private Const BI_RLE4 = 2&
  19. Private Const BI_RLE8 = 1&
  20. Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
  21.  
  22. Private Type RGBQUAD
  23.     rgbBlue As Byte
  24.     rgbGreen As Byte
  25.     rgbRed As Byte
  26.     rgbReserved As Byte
  27. End Type
  28.  
  29. Private Type BITMAPINFOHEADER '40 bytes
  30.     biSize As Long
  31.     biWidth As Long
  32.     biHeight As Long
  33.     biPlanes As Integer
  34.     biBitCount As Integer
  35.     biCompression As Long
  36.     biSizeImage As Long
  37.     biXPelsPerMeter As Long
  38.     biYPelsPerMeter As Long
  39.     biClrUsed As Long
  40.     biClrImportant As Long
  41. End Type
  42.  
  43. Private Type BITMAPINFO
  44.     bmiHeader As BITMAPINFOHEADER
  45.     bmiColors As RGBQUAD
  46. End Type
  47.  
  48. Private Type BITMAP
  49.     bmType As Long
  50.     bmWidth As Long
  51.     bmHeight As Long
  52.     bmWidthBytes As Long
  53.     bmPlanes As Integer
  54.     bmBitsPixel As Integer
  55.     bmBits As Long
  56. End Type
  57.  
  58. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  59. Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
  60. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  61. Private Declare Function CreateDIBSection Lib "gdi32" _
  62.     (ByVal hDC As Long, _
  63.     pBitmapInfo As BITMAPINFO, _
  64.     ByVal un As Long, _
  65.     lplpVoid As Long, _
  66.     ByVal handle As Long, _
  67.     ByVal dw As Long) As Long
  68. Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  69. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  70. 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
  71. Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  72. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  73. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
  74.  
  75. Private m_hDIb As Long
  76. Private m_hBmpOld As Long
  77. Private m_hDC As Long
  78. Private m_lPtr As Long
  79. Private m_tBI As BITMAPINFO
  80.  
  81. Public Function CreateDIB( _
  82.         ByVal lHDC As Long, _
  83.         ByVal lWidth As Long, _
  84.         ByVal lHeight As Long, _
  85.         ByRef hDib As Long _
  86.     ) As Boolean
  87.     With m_tBI.bmiHeader
  88.         .biSize = Len(m_tBI.bmiHeader)
  89.         .biWidth = lWidth
  90.         .biHeight = lHeight
  91.         .biPlanes = 1
  92.         .biBitCount = 24
  93.         .biCompression = BI_RGB
  94.         .biSizeImage = BytesPerScanLine * .biHeight
  95.     End With
  96.     hDib = CreateDIBSection( _
  97.             lHDC, _
  98.             m_tBI, _
  99.             DIB_RGB_COLORS, _
  100.             m_lPtr, _
  101.             0, 0)
  102.     CreateDIB = (hDib <> 0)
  103. End Function
  104. Public Function CreateFromPicture( _
  105.         ByRef picThis As StdPicture _
  106.     )
  107. Dim lHDC As Long
  108. Dim lhDCDesktop As Long
  109. Dim lhBmpOld As Long
  110. Dim tBMP As BITMAP
  111.     
  112.     GetObjectAPI picThis.handle, Len(tBMP), tBMP
  113.     If (Create(tBMP.bmWidth, tBMP.bmHeight)) Then
  114.         lhDCDesktop = GetDC(GetDesktopWindow())
  115.         If (lhDCDesktop <> 0) Then
  116.             lHDC = CreateCompatibleDC(lhDCDesktop)
  117.             DeleteDC lhDCDesktop
  118.             If (lHDC <> 0) Then
  119.                 lhBmpOld = SelectObject(lHDC, picThis.handle)
  120.                 LoadPictureBlt lHDC
  121.                 SelectObject lHDC, lhBmpOld
  122.                 DeleteObject lHDC
  123.             End If
  124.         End If
  125.     End If
  126. End Function
  127.  
  128. Public Function Create( _
  129.         ByVal lWidth As Long, _
  130.         ByVal lHeight As Long _
  131.     ) As Boolean
  132.     ClearUp
  133.     m_hDC = CreateCompatibleDC(0)
  134.     If (m_hDC <> 0) Then
  135.         If (CreateDIB(m_hDC, lWidth, lHeight, m_hDIb)) Then
  136.             m_hBmpOld = SelectObject(m_hDC, m_hDIb)
  137.             Create = True
  138.         Else
  139.             DeleteObject m_hDC
  140.             m_hDC = 0
  141.         End If
  142.     End If
  143. End Function
  144. Public Property Get BytesPerScanLine() As Long
  145.     ' Scans must align on dword boundaries:
  146.     BytesPerScanLine = (m_tBI.bmiHeader.biWidth * 3 + 3) And &HFFFFFFFC
  147. End Property
  148.  
  149. Public Property Get Width() As Long
  150.     Width = m_tBI.bmiHeader.biWidth
  151. End Property
  152. Public Property Get Height() As Long
  153.     Height = m_tBI.bmiHeader.biHeight
  154. End Property
  155.  
  156. Public Sub LoadPictureBlt( _
  157.         ByVal lHDC As Long, _
  158.         Optional ByVal lSrcLeft As Long = 0, _
  159.         Optional ByVal lSrcTop As Long = 0, _
  160.         Optional ByVal lSrcWidth As Long = -1, _
  161.         Optional ByVal lSrcHeight As Long = -1, _
  162.         Optional ByVal eRop As RasterOpConstants = vbSrcCopy _
  163.     )
  164.     If lSrcWidth < 0 Then lSrcWidth = m_tBI.bmiHeader.biWidth
  165.     If lSrcHeight < 0 Then lSrcHeight = m_tBI.bmiHeader.biHeight
  166.     BitBlt m_hDC, 0, 0, lSrcWidth, lSrcHeight, lHDC, lSrcLeft, lSrcTop, eRop
  167. End Sub
  168.  
  169. Public Property Get DIBSectionBitsPtr() As Long
  170.     DIBSectionBitsPtr = m_lPtr
  171. End Property
  172.  
  173. Public Sub ClearUp()
  174.     If (m_hDC <> 0) Then
  175.         If (m_hDIb <> 0) Then
  176.             SelectObject m_hDC, m_hBmpOld
  177.             DeleteObject m_hDIb
  178.         End If
  179.         DeleteObject m_hDC
  180.     End If
  181.     m_hDC = 0: m_hDIb = 0: m_hBmpOld = 0: m_lPtr = 0
  182. End Sub
  183.  
  184. Private Sub Class_Terminate()
  185.     ClearUp
  186. End Sub
  187.