Advertisement
Ridelith

Thumbnail.ahk

May 8th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. /**************************************************************************************************************
  2. title: Thumbnail functions
  3. wrapped by maul.esel
  4.  
  5. Credits:
  6. - skrommel for example how to show a thumbnail (http://www.autohotkey.com/forum/topic34318.html)
  7. - RaptorOne & IsNull for correcting some mistakes in the code
  8.  
  9. NOTE:
  10. *This requires Windows Vista or Windows7* (tested on Windows 7)
  11. Quick-Tutorial:
  12. To add a thumbnail to a gui, you must know the following:
  13. - the hwnd / id of your gui
  14. - the hwnd / id of the window to show
  15. - the coordinates where to show the thumbnail
  16. - the coordinates of the area to be shown
  17. 1. Create a thumbnail with Thumbnail_Create()
  18. 2. Set its regions with Thumbnail_SetRegion()
  19. a. optionally query for the source windows width and height before with <Thumbnail_GetSourceSize()>
  20. 3. optionally set the opacity with <Thumbnail_SetOpacity()>
  21. 4. show the thumbnail with <Thumbnail_Show()>
  22. ***************************************************************************************************************
  23. */
  24.  
  25.  
  26. /**************************************************************************************************************
  27. Function: Thumbnail_Create()
  28. creates a thumbnail relationship between two windows
  29.  
  30. params:
  31. handle hDestination - the window that will show the thumbnail
  32. handle hSource - the window whose thumbnail will be shown
  33. returns:
  34. handle hThumb - thumbnail id on success, false on failure
  35.  
  36. Remarks:
  37. To get the Hwnds, you could use WinExist()
  38. ***************************************************************************************************************
  39. */
  40. Thumbnail_Create(hDestination, hSource) {
  41.  
  42. VarSetCapacity(thumbnail, 4, 0)
  43. if DllCall("dwmapi.dll\DwmRegisterThumbnail", "UInt", hDestination, "UInt", hSource, "UInt", &thumbnail)
  44. return false
  45. return NumGet(thumbnail)
  46. }
  47.  
  48.  
  49. /**************************************************************************************************************
  50. Function: Thumbnail_SetRegion()
  51. defines dimensions of a previously created thumbnail
  52.  
  53. params:
  54. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  55. int xDest - the x-coordinate of the rendered thumbnail inside the destination window
  56. int yDest - the y-coordinate of the rendered thumbnail inside the destination window
  57. int wDest - the width of the rendered thumbnail inside the destination window
  58. int hDest - the height of the rendered thumbnail inside the destination window
  59. int xSource - the x-coordinate of the area that will be shown inside the thumbnail
  60. int ySource - the y-coordinate of the area that will be shown inside the thumbnail
  61. int wSource - the width of the area that will be shown inside the thumbnail
  62. int hSource - the height of the area that will be shown inside the thumbnail
  63. returns:
  64. bool success - true on success, false on failure
  65. ***************************************************************************************************************
  66. */
  67. Thumbnail_SetRegion(hThumb, xDest, yDest, wDest, hDest, xSource, ySource, wSource, hSource) {
  68. dwFlags := 0x00000001 | 0x00000002
  69.  
  70. VarSetCapacity(dskThumbProps, 45, 0)
  71.  
  72. NumPut(dwFlags, dskThumbProps, 0, "UInt")
  73. NumPut(xDest, dskThumbProps, 4, "Int")
  74. NumPut(yDest, dskThumbProps, 8, "Int")
  75. NumPut(wDest+xDest, dskThumbProps, 12, "Int")
  76. NumPut(hDest+yDest, dskThumbProps, 16, "Int")
  77.  
  78. NumPut(xSource, dskThumbProps, 20, "Int")
  79. NumPut(ySource, dskThumbProps, 24, "Int")
  80. NumPut(wSource+xSource, dskThumbProps, 28, "Int")
  81. NumPut(hSource+ySource, dskThumbProps, 32, "Int")
  82.  
  83. return DllCall("dwmapi.dll\DwmUpdateThumbnailProperties", "UInt", hThumb, "UInt", &dskThumbProps) ? false : true
  84. }
  85.  
  86.  
  87. /**************************************************************************************************************
  88. Function: Thumbnail_Show()
  89. shows a previously created and sized thumbnail
  90.  
  91. params:
  92. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  93. returns:
  94. bool success - true on success, false on failure
  95. ***************************************************************************************************************
  96. */
  97. Thumbnail_Show(hThumb) {
  98. static dwFlags := 0x00000008, fVisible := 1
  99.  
  100. VarSetCapacity(dskThumbProps, 45, 0)
  101. NumPut(dwFlags, dskThumbProps, 0, "UInt")
  102. NumPut(fVisible, dskThumbProps, 37, "Int")
  103.  
  104. return DllCall("dwmapi.dll\DwmUpdateThumbnailProperties", "UInt", hThumb, "UInt", &dskThumbProps) ? false : true
  105. }
  106.  
  107.  
  108. /**************************************************************************************************************
  109. Function: Thumbnail_Hide()
  110. hides a thumbnail. It can be shown again without recreating
  111.  
  112. params:
  113. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  114. returns:
  115. bool success - true on success, false on failure
  116. ***************************************************************************************************************
  117. */
  118. Thumbnail_Hide(hThumb) {
  119. static dwFlags := 0x00000008, fVisible := 0
  120.  
  121. VarSetCapacity(dskThumbProps, 45, 0)
  122. NumPut(dwFlags, dskThumbProps, 0, "Uint")
  123. NumPut(fVisible, dskThumbProps, 37, "Int")
  124. return DllCall("dwmapi.dll\DwmUpdateThumbnailProperties", "UInt", hThumb, "UInt", &dskThumbProps) ? false : true
  125. }
  126.  
  127.  
  128. /**************************************************************************************************************
  129. Function: Thumbnail_Destroy()
  130. destroys a thumbnail relationship
  131.  
  132. params:
  133. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  134. returns:
  135. bool success - true on success, false on failure
  136. ***************************************************************************************************************
  137. */
  138. Thumbnail_Destroy(hThumb) {
  139. return DllCall("dwmapi.dll\DwmUnregisterThumbnail", "UInt", hThumb) ? false : true
  140. }
  141.  
  142.  
  143. /**************************************************************************************************************
  144. Function: Thumbnail_GetSourceSize()
  145. gets the width and height of the source window - can be used with <Thumbnail_SetRegion()>
  146.  
  147. params:
  148. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  149. ByRef int width - receives the width of the window
  150. ByRef int height - receives the height of the window
  151. returns:
  152. bool success - true on success, false on failure
  153. ***************************************************************************************************************
  154. */
  155. Thumbnail_GetSourceSize(hThumb, ByRef width, ByRef height) {
  156. VarSetCapacity(Size, 8, 0)
  157. if DllCall("dwmapi.dll\DwmQueryThumbnailSourceSize", "Uint", hThumb, "Uint", &Size)
  158. return false
  159. width := NumGet(&Size + 0, 0, "int")
  160. height := NumGet(&Size + 0, 4, "int")
  161. return true
  162. }
  163.  
  164.  
  165. /**************************************************************************************************************
  166. Function: Thumbnail_SetOpacity()
  167. sets the current opacity level
  168.  
  169. params:
  170. handle hThumb - the thumbnail id returned by <Thumbnail_Create()>
  171. int opacity - the opacity level from 0 to 255 (will wrap to the other end if invalid)
  172. returns:
  173. bool success - true on success, false on failure
  174. ***************************************************************************************************************
  175. */
  176. Thumbnail_SetOpacity(hThumb, opacity) {
  177. static dwFlags := 0x00000004
  178.  
  179. VarSetCapacity(dskThumbProps, 45, 0)
  180. NumPut(dwFlags, dskThumbProps, 0, "UInt")
  181. NumPut(opacity, dskThumbProps, 36, "UChar")
  182. return DllCall("dwmapi.dll\DwmUpdateThumbnailProperties", "Uint", hThumb, "UInt", &dskThumbProps) ? false : true
  183. }
  184.  
  185. /**************************************************************************************************************
  186. section: example
  187. This example sctript shows a thumbnail of your desktop in a GUI
  188. (start code)
  189. ; initializing the script:
  190. #SingleInstance force
  191. #NoEnv
  192. #KeyHistory 0
  193. SetWorkingDir %A_ScriptDir%
  194. #include Thumbnail.ahk
  195.  
  196. ; get the handles:
  197. Gui +LastFound
  198. hDestination := WinExist() ; ... to our GUI...
  199. hSource := WinExist("ahk_class Progman") ; ... and to the desktop
  200.  
  201. ; creating the thumbnail:
  202. hThumb := Thumbnail_Create(hDestination, hSource) ; you must get the return value here!
  203.  
  204. ; getting the source window dimensions:
  205. Thumbnail_GetSourceSize(hThumb, width, height)
  206.  
  207. ; then setting its region:
  208. Thumbnail_SetRegion(hThumb, 25, 25 ; x and y in the GUI
  209. , 400, 350 ; display dimensions
  210. , 0, 0 ; source area coordinates
  211. , width, height) ; the values from Thumbnail_GetSourceSize()
  212.  
  213. ; now some GUI stuff:
  214. Gui +AlwaysOnTop +ToolWindow
  215. Gui Add, Button, gHideShow x0 y0, Hide / Show
  216.  
  217. ; Now we can show it:
  218. Thumbnail_Show(hThumb) ; but it is not visible now...
  219. Gui Show, w450 h400 ; ... until we show the GUI
  220.  
  221. ; even now we can set the transparency:
  222. Thumbnail_SetOpacity(hThumb, 200)
  223.  
  224. return
  225.  
  226. GuiClose: ; in case the GUI is closed:
  227. Thumbnail_Destroy(hThumb) ; free the resources
  228. ExitApp
  229.  
  230. HideShow: ; in case the button is clicked:
  231. if hidden
  232. Thumbnail_Show(hThumb)
  233. else
  234. Thumbnail_Hide(hThumb)
  235.  
  236. hidden := !hidden
  237. return
  238. (end)
  239. ***************************************************************************************************************
  240. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement