home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD8529822000.psc / MemoryBitmap.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-07-31  |  10.9 KB  |  352 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 = "ascMemoryBitmap"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14.  
  15. '-----------------------------------------'
  16. '           Ariad Development Library 2.1 '
  17. '-----------------------------------------'
  18. '                     Memory Bitmap Class '
  19. '                             Version 1.0 '
  20. '-----------------------------------------'
  21. ' Based on original code by Steve McMahon '
  22. '-----------------------------------------'
  23. 'Copyright ⌐ 1999 by Ariad Software. All Rights Reserved
  24.  
  25. 'Date Created:
  26. 'Last Updated: 21/05/1999
  27.  
  28. Option Explicit
  29. DefInt A-Z
  30.  
  31. Private Type BITMAP '14 bytes
  32.  bmType         As Long
  33.  bmWidth        As Long
  34.  bmHeight       As Long
  35.  bmWidthBytes   As Long
  36.  bmPlanes       As Integer
  37.  bmBitsPixel    As Integer
  38.  bmBits         As Long
  39. End Type
  40.  
  41. Private Type PicBmp
  42.  Size       As Long
  43.  tType      As Long
  44.  hBmp       As Long
  45.  hPal       As Long
  46.  Reserved   As Long
  47. End Type
  48.  
  49. Private Type GUID
  50.  Data1      As Long
  51.  Data2      As Integer
  52.  Data3      As Integer
  53.  Data4(7)   As Byte
  54. End Type
  55.  
  56. 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
  57. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  58. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  59. Private Declare Function CreateDCAsNull Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, lpDeviceName As Any, lpOutput As Any, lpInitData As Any) As Long
  60. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
  61. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  62. Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
  63. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  64. Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  65. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
  66. Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
  67. Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  68.  
  69. Const BITSPIXEL = 12
  70. Const LOGPIXELSX = 88    '  Logical pixels/inch in X
  71. Const LOGPIXELSY = 90    '  Logical pixels/inch in Y
  72.  
  73. Dim m_hDC As Long
  74. Dim m_hBmp As Long
  75. Dim m_hBmpOld As Long
  76. Dim m_lWidth As Long
  77. Dim m_lHeight As Long
  78. '----------------------------------------------------------------------
  79. 'Name        : Picture
  80. 'Created     : 28/06/1999 14:14
  81. 'Modified    :
  82. '----------------------------------------------------------------------
  83. 'Author      : Richard Moss
  84. 'Organisation: Ariad Software
  85. '----------------------------------------------------------------------
  86. Public Property Get Picture() As IPicture
  87.  Dim pic As PicBmp
  88.  Dim IPic As IPicture
  89.  Dim IID_IDispatch As GUID
  90.  If m_hBmp Then
  91.   ' Fill in with IDispatch Interface ID.
  92.   With IID_IDispatch
  93.    .Data1 = &H20400
  94.    .Data4(0) = &HC0
  95.    .Data4(7) = &H46
  96.   End With
  97.   ' Fill Pic with necessary parts.
  98.   With pic
  99.    .Size = Len(pic) ' Length of structure.
  100.    .tType = vbPicTypeBitmap ' Type of Picture (bitmap).
  101.    .hBmp = m_hBmp ' Handle to bitmap.
  102.   End With
  103.   ' Create Picture object.
  104.   Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
  105.   ' Return the new Picture object.
  106.   Set Picture = IPic
  107.  End If
  108. End Property
  109. Public Property Get hBmp() As Long
  110.  hBmp = m_hBmp
  111. End Property
  112. Public Property Get Width() As Long
  113.  Width = m_lWidth
  114. End Property
  115.  
  116. Public Property Get Height() As Long
  117.  Height = m_lHeight
  118. End Property
  119.  
  120. Sub ClearUp()
  121.  If m_hBmpOld <> 0 Then
  122.   SelectObject m_hDC, m_hBmpOld
  123.   m_hBmpOld = 0
  124.  End If
  125.  If m_hBmp <> 0 Then
  126.   DeleteObject m_hBmp
  127.   m_hBmp = 0
  128.  End If
  129.  If m_hDC <> 0 Then
  130.   DeleteDC m_hDC
  131.   m_hDC = 0
  132.  End If
  133. End Sub
  134.  
  135. Private Sub Class_Terminate()
  136.  ClearUp
  137. End Sub
  138.  
  139. Private Function LoadBitmapIntoMemory(P As StdPicture) As Boolean
  140.  Dim tBM As BITMAP
  141.  Dim hBmp As Long, hBmpOld As Long
  142.  Dim hDCDesk As Long, hdcTemp As Long
  143.  On Error GoTo ProcErr
  144.   ClearUp
  145.   hBmp = P.Handle
  146.   GetObjectAPI hBmp, Len(tBM), tBM
  147.   hDCDesk = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
  148.   If (hDCDesk <> 0) Then
  149.    hdcTemp = CreateCompatibleDC(hDCDesk)
  150.    If (hdcTemp <> 0) Then
  151.     hBmpOld = SelectObject(hdcTemp, hBmp)
  152.     If (hBmpOld <> 0) Then
  153.      m_hDC = CreateCompatibleDC(hDCDesk)
  154.      If (m_hDC <> 0) Then
  155.       m_hBmp = CreateCompatibleBitmap(hDCDesk, tBM.bmWidth, tBM.bmHeight)
  156.       If (m_hBmp <> 0) Then
  157.        m_hBmpOld = SelectObject(m_hDC, m_hBmp)
  158.        If m_hBmpOld <> 0 Then
  159.         m_lWidth = tBM.bmWidth
  160.         m_lHeight = tBM.bmHeight
  161.         BitBlt m_hDC, 0, 0, m_lWidth, m_lHeight, hdcTemp, 0, 0, vbSrcCopy
  162.         LoadBitmapIntoMemory = True
  163.        Else
  164.         ClearUp
  165.        End If
  166.       Else
  167.        ClearUp
  168.       End If
  169.      Else
  170.       ClearUp
  171.      End If
  172.      SelectObject hdcTemp, hBmpOld
  173.     End If
  174.     DeleteDC hdcTemp
  175.    End If
  176.    DeleteDC hDCDesk
  177.   End If
  178.  On Error GoTo 0
  179. Exit Function
  180.  
  181. ProcErr:
  182.  RaiseError "LoadBitmapIntoMemory"
  183. Exit Function
  184. End Function
  185.  
  186.  
  187. Public Property Get hDC() As Long
  188.  '##BD Returns a handle provided by the Microsoft Windows operating environment to the device context of the memory bitmap
  189.  hDC = m_hDC
  190. End Property
  191.  
  192.  
  193. Public Function CreateByFile(ByVal FileName$) As Boolean
  194.  Dim P As StdPicture
  195.  On Error GoTo ProcErr
  196.   Set P = LoadPicture(FileName$)
  197.   If Not P Is Nothing Then
  198.    CreateByFile = LoadBitmapIntoMemory(P)
  199.   End If
  200.  On Error GoTo 0
  201. Exit Function
  202.  
  203. ProcErr:
  204.  RaiseError "CreateByFile"
  205. Exit Function
  206. End Function
  207.  
  208. Public Function CreateByPicture(ByVal Picture As StdPicture) As Boolean
  209.  On Error GoTo ProcErr
  210.   If Not Picture Is Nothing Then
  211.    If Picture.Type = vbPicTypeBitmap Then
  212.     CreateByPicture = LoadBitmapIntoMemory(Picture)
  213.    Else
  214.     RaiseErrorEx "CreateByPicture", 481, "Picture property must be of type Bitmap"
  215.    End If
  216.   End If
  217.  On Error GoTo 0
  218. Exit Function
  219.  
  220. ProcErr:
  221.  RaiseError "CreateByPicture"
  222. Exit Function
  223. End Function
  224.  
  225. Public Function CreateByResource(ByVal ResourceID As Variant) As Boolean
  226.  Dim P As StdPicture
  227.  On Error GoTo ProcErr
  228.   Set P = LoadResPicture(ResourceID, vbResBitmap)
  229.   If Not P Is Nothing Then
  230.    CreateByResource = LoadBitmapIntoMemory(P)
  231.   End If
  232.  On Error GoTo 0
  233. Exit Function
  234.  
  235. ProcErr:
  236.  RaiseError "CreateByResource"
  237. Exit Function
  238. End Function
  239.  
  240. '----------------------------------------------------------------------
  241. 'Name        : RaiseError
  242. 'Created     : 14/07/1999 19:12
  243. 'Modified    :
  244. 'Modified By :
  245. '----------------------------------------------------------------------
  246. 'Author      : Richard James Moss
  247. 'Organisation: Ariad Software
  248. '----------------------------------------------------------------------
  249. 'Description : Raises a standard Visual Basic error
  250. '            : When in Design Mode, a simple message box is displayed instead
  251. '----------------------------------------------------------------------
  252. 'Updates     : 16/09/99 - Added support for procedure names
  253. '
  254. '----------------------------------------------------------------------
  255. '------------------------------Ariad Procedure Builder Add-In 1.00.0026
  256. Private Sub RaiseError(ByVal ProcName$)
  257. ' If Ambient.UserMode Then
  258.   '"Runtime" - raise error
  259.   Err.Raise Err, App.EXEName & "." & TypeName(Me) & ":" & ProcName$
  260. ' Else
  261. '  '"Design time" - display error
  262. '  VBA.MsgBox INTERR$ & vbCr & vbCr & Err.Description & " (" & Err & ")" & vbCr & vbCr & ERRTEXT$, vbCritical, App.EXEName & "." & TypeName(Me) & ":" & ProcName$
  263. ' End If
  264. End Sub
  265.  
  266. '----------------------------------------------------------------------
  267. 'Name        : RaiseErrorEx
  268. 'Created     : 29/08/1999 16:11
  269. '----------------------------------------------------------------------
  270. 'Author      : Richard James Moss
  271. 'Organisation: Ariad Software
  272. '----------------------------------------------------------------------
  273. 'Description : Raises an extended error.
  274. '
  275. '              If the error occurs in design time, and not run time, a
  276. '              simple error message is displayed instead of raising an error.
  277. '----------------------------------------------------------------------
  278. 'Updates     : 16/09/99 - Added support for procedure names
  279. '
  280. '----------------------------------------------------------------------
  281. '------------------------------Ariad Procedure Builder Add-In 1.00.0026
  282. Private Sub RaiseErrorEx(ByVal ProcName$, ByVal ErrNum As Long, Optional ByVal ErrMsg$ = "")
  283. ' If Ambient.UserMode Then
  284.   '"Runtime" - raise error
  285.   If Len(ErrMsg$) Then
  286.    Err.Raise ErrNum, App.EXEName & "." & TypeName(Me) & ":" & ProcName$, ErrMsg$
  287.   Else
  288.    Err.Raise ErrNum, App.EXEName & "." & TypeName(Me) & ":" & ProcName$
  289.   End If
  290. ' Else
  291. '  '"Design time" - display error
  292. '  If Len(ErrMsg$) = 0 Then
  293. '   On Error Resume Next
  294. '    Error ErrNum
  295. '    ErrMsg$ = Err.Description
  296. '   On Error GoTo 0
  297. '  End If
  298. '  VBA.MsgBox INTERR$ & vbCr & vbCr & ErrMsg$ & " (" & ErrNum & ")" & vbCr & vbCr & ERRTEXT$, vbCritical, App.EXEName & "." & TypeName(Me)
  299. ' End If
  300. End Sub
  301.  
  302.  Function CreateBlank(tWidth As Long, tHeight As Long) As Boolean
  303.  Dim tBM As BITMAP
  304.  Dim hBmp As Long, hBmpOld As Long
  305.  Dim hDCDesk As Long, hdcTemp As Long
  306.  On Error GoTo ProcErr
  307.   ClearUp
  308. '  hBmp = P.Handle
  309. '  GetObjectAPI hBmp, Len(tBM), tBM
  310.   tBM.bmWidth = tWidth
  311.   tBM.bmHeight = tHeight
  312.   
  313.   hDCDesk = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
  314.   If (hDCDesk <> 0) Then
  315.    hdcTemp = CreateCompatibleDC(hDCDesk)
  316.    If (hdcTemp <> 0) Then
  317.     'hBmpOld = SelectObject(hdcTemp, hBmp)
  318.     'If (hBmpOld <> 0) Then
  319.      m_hDC = CreateCompatibleDC(hDCDesk)
  320.      If (m_hDC <> 0) Then
  321.       m_hBmp = CreateCompatibleBitmap(hDCDesk, tBM.bmWidth, tBM.bmHeight)
  322.       If (m_hBmp <> 0) Then
  323.        m_hBmpOld = SelectObject(m_hDC, m_hBmp)
  324.        If m_hBmpOld <> 0 Then
  325.         m_lWidth = tBM.bmWidth
  326.         m_lHeight = tBM.bmHeight
  327.         BitBlt m_hDC, 0, 0, m_lWidth, m_lHeight, hdcTemp, 0, 0, vbSrcCopy
  328.         CreateBlank = True
  329.        Else
  330.         ClearUp
  331.        End If
  332.       Else
  333.        ClearUp
  334.       End If
  335.      Else
  336.       ClearUp
  337.      End If
  338.     ' SelectObject hdcTemp, hBmpOld
  339.     'End If
  340.     DeleteDC hdcTemp
  341.    End If
  342.    DeleteDC hDCDesk
  343.   End If
  344.  On Error GoTo 0
  345. Exit Function
  346.  
  347. ProcErr:
  348.  RaiseError "LoadBitmapIntoMemory"
  349. Exit Function
  350. End Function
  351.  
  352.