/Structure Classes/PICTDESC.ahk

http://github.com/maul-esel/COM-Classes · AutoHotKey · 196 lines · 77 code · 14 blank · 105 comment · 40 complexity · 7b248e3d22110cccc11c3e1e6a3a6b34 MD5 · raw file

  1. /*
  2. class: PICTDESC
  3. a structure class that contains parameters to create a picture object through the OleCreatePictureIndirect function.
  4. Authors:
  5. - maul.esel (https://github.com/maul-esel)
  6. License:
  7. - *LGPL* (http://www.gnu.org/licenses/lpgl-2.1.txt)
  8. Documentation:
  9. - *class documentation* (http://maul-esel.github.com/COM-Classes/master/PICTDESC)
  10. - *msdn* (http://msdn.microsoft.com/en-us/library/windows/desktop/ms693798)
  11. Requirements:
  12. AutoHotkey - AHK v2 alpha
  13. OS - Windows 2000 Professional / Windows 2000 Server or higher
  14. Base classes - _CCF_Error_Handler_, StructBase
  15. Constant classes - PICTYPE
  16. */
  17. class PICTDESC extends StructBase
  18. {
  19. /*
  20. Field: cbSizeofstruct
  21. The size of the structure, in bytes.
  22. Remarks:
  23. - When retrieved on an instance created from a memory struct, this contains the value in this struct.
  24. - Otherwise, it'll be initially 0 as it changes depending on the other members.
  25. - Whenever you call <ToStructPtr()>, this will be calculated, based on the value of the <picType> field, used and set to the result.
  26. */
  27. cbSizeofstruct := 0
  28. /*
  29. Field: picType
  30. The type of picture described by this structure. You may use the fields of the PICTYPE class for convenience.
  31. Remarks:
  32. - This field's value is responsible for the selected union arm.
  33. */
  34. picType := 0
  35. /*
  36. Field: bmp
  37. Structure containing bitmap information if <picType> is PICTYPE.BITMAP.
  38. Nested fields:
  39. HBITMAP hbitmap - The HBITMAP handle identifying the bitmap assigned to the picture object.
  40. HPALETTE hpal - The HPALETTE handle identifying the color palette for the bitmap.
  41. */
  42. bmp := { "hbitmap" : 0, "hpal" : 0 }
  43. /*
  44. Field: wmf
  45. Structure containing metafile information if <picType> is PICTYPE.METAFILE.
  46. Nested fields:
  47. HMETAFILE hmeta - The HMETAFILE handle identifying the metafile assigned to the picture object.
  48. INT xExt - Horizontal extent of the metafile in TWIPS units.
  49. INT yExt - Vertical extent of the metafile in TWIPS units.
  50. */
  51. wmf := { "hmeta" : 0, "xExt" : 0, "yExt" : 0 }
  52. /*
  53. Field: icon
  54. Structure containing icon information if <picType> is PICTYPE.ICON.
  55. Nested fields:
  56. HICON hIcon - The HICON handle identifying the icon assigned to the picture object.
  57. */
  58. icon := { "hIcon" : 0 }
  59. /*
  60. Field: emf
  61. Structure containing enhanced metafile information if <picType> is PICTYPE.ENHMETAFILE.
  62. Nested Fields:
  63. HENHMETAFILE hemf - The HENHMETAFILE handle identifying the enhanced metafile assigned to the picture object.
  64. */
  65. emf := { "hemf" : 0 }
  66. /*
  67. Method: ToStructPtr
  68. converts the instance to a script-usable struct and returns its memory adress.
  69. Parameters:
  70. [opt] UPTR ptr - the fixed memory address to copy the struct to.
  71. Returns:
  72. UPTR ptr - a pointer to the struct in memory
  73. */
  74. ToStructPtr(ptr := 0)
  75. {
  76. if (!ptr)
  77. {
  78. ptr := this.Allocate(this.cbSizeofstruct := this.GetRequiredSize())
  79. }
  80. NumPut(this.cbSizeofstruct, 1*ptr, 00, "UInt")
  81. NumPut(this.picType, 1*ptr, 04, "UInt")
  82. if (this.picType == PICTYPE.BITMAP)
  83. {
  84. NumPut(this.bmp.hbitmap,1*ptr, 08+0*A_PtrSize, "UPtr")
  85. NumPut(this.bmp.hpal, 1*ptr, 08+1*A_PtrSize, "UPtr")
  86. }
  87. else if (this.picType == PICTYPE.METAFILE)
  88. {
  89. NumPut(this.wmf.hmeta, 1*ptr, 08+0*A_PtrSize, "UPtr")
  90. NumPut(this.wmf.xExt, 1*ptr, 08+1*A_PtrSize, "Int")
  91. NumPut(this.wmf.yExt, 1*ptr, 12+1*A_PtrSize, "Int")
  92. }
  93. else if (this.picType == PICTYPE.ICON)
  94. {
  95. NumPut(this.icon.hIcon, 1*ptr, 08+0*A_PtrSize, "UPtr")
  96. }
  97. else if (this.picType == PICTYPE.ENHMETAFILE)
  98. {
  99. NumPut(this.emf.hemf, 1*ptr, 08+0*A_PtrSize, "UPtr")
  100. }
  101. return ptr
  102. }
  103. /*
  104. Method: FromStructPtr
  105. (static) method that converts a script-usable struct into a new instance of the class
  106. Parameters:
  107. UPTR ptr - a pointer to a PICTDESC struct in memory
  108. [opt] BOOL own - false if the instance must no release the pointer (defaults to true)
  109. Returns:
  110. PICTDESC instance - the new PICTDESC instance
  111. Remarks:
  112. To retrieve any values besides <cbSizeofstruct> and <picType>, the first must match the size the later one requires. <picType> must also not be PICTYPE.NONE or PICTYPE.UNINITIALIZED. If this is the case, all other fields are left to their defaults.
  113. */
  114. FromStructPtr(ptr, own := true)
  115. {
  116. local instance := new PICTDESC()
  117. instance.SetOriginalPointer(ptr, own)
  118. instance.cbSizeofstruct := NumGet(1*ptr, 00, "UInt")
  119. instance.picType := NumGet(1*ptr, 04, "UInt")
  120. if (instance.picType == PICTYPE.BITMAP && instance.cbSizeofstruct == 8 + 2 * A_PtrSize)
  121. {
  122. instance.bmp.hbitmap:= NumGet(1*ptr, 08+0*A_PtrSize, "UPtr")
  123. instance.bmp.hpal := NumGet(1*ptr, 08+1*A_PtrSize, "UPtr")
  124. }
  125. else if (instance.picType == PICTYPE.METAFILE && instance.cbSizeofstruct == 16 + A_PtrSize)
  126. {
  127. instance.wmf.hmeta := NumGet(1*ptr, 08+0*A_PtrSize, "UPtr")
  128. instance.wmf.xExt := NumGet(1*ptr, 08+1*A_PtrSize, "Int")
  129. instance.wmf.yExt := NumGet(1*ptr, 12+1*A_PtrSize, "Int")
  130. }
  131. else if (instance.picType == PICTYPE.ICON && instance.cbSizeofstruct == 8 + A_PtrSize)
  132. {
  133. instance.icon.hIcon := NumGet(1*ptr, 08+0*A_PtrSize, "UPtr")
  134. }
  135. else if (instance.picType == PICTYPE.ENHMETAFILE && instance.cbSizeofstruct == 8 + A_PtrSize)
  136. {
  137. instance.emf.hemf := NumGet(1*ptr, 08+0*A_PtrSize, "UPtr")
  138. }
  139. return instance
  140. }
  141. /*
  142. Method: GetRequiredSize
  143. calculates the size a memory instance of this class requires.
  144. Parameters:
  145. [opt] OBJECT data - an optional data object that may contain data for the calculation.
  146. Returns:
  147. UINT bytes - the number of bytes required
  148. Remarks:
  149. - This may be called as if it was a static method.
  150. - If this method is called from an instance, the instance's <picType> value will be used. To override this or if the method is called as static method, the data object may contain a field called "pictype" which specifies the PICTYPE value for the calcuation. If none is give, PICTYPE.METAFILE is assumed, which needs most memory.
  151. */
  152. GetRequiredSize(data := "")
  153. {
  154. local picType
  155. picType := PICTYPE.METAFILE
  156. if (this != PICTDESC)
  157. picType := this.picType
  158. if (IsObject(data) && data.HasKey("pictype"))
  159. picType := data.picType
  160. return 8 + (picType == PICTYPE.ICON || picType == PICTYPE.ENHMETAFILE ? A_PtrSize
  161. : (picType == PICTYPE.BITMAP ? 2 * A_PtrSize
  162. : (picType == PICTYPE.METAFILE ? 8 + A_PtrSize : 0)))
  163. }
  164. }