Advertisement
asuswrthma

Example WinAPI [DwmUpdateThumbnailProperties]

Aug 10th, 2022 (edited)
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 7.07 KB | None | 0 0
  1. #include <WinAPIGdi.au3>
  2. #include <Timers.au3>
  3. #include <Array.au3>
  4.  
  5. #include <GUIConstantsEx.au3>
  6. #include <WindowsConstants.au3>
  7. #include <GDIPlus.au3>
  8. Example()
  9.  
  10. Func Example()
  11.     #Region ### START Koda GUI section ### Form=
  12.     Global $hGUI = GUICreate('Example WinAPI [DwmUpdateThumbnailProperties]', 950, 450, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
  13.     GUISetBkColor(0x506070)
  14.     GUISetState(@SW_SHOW)
  15.     #EndRegion ### END Koda GUI section ###
  16.  
  17.     Local $aList = WinList('[REGEXPTITLE:(?i)(.*Google Chrome.*)]') ; get list handle
  18.     Local $x = 10, $y = 10, $maxWidth = 300, $maxHeight = 169, $size = WinGetClientSize($hGUI)
  19.  
  20.     Global $aThumb[0][7], $margin = 10
  21.  
  22.     Local $vCenter = False, $hCenter = False
  23.     For $i = 1 To $aList[0][0]
  24.         If $i = 2 Then $vCenter = True
  25.         If $i = 3 Then $hCenter = True
  26.         If $i = 4 Then $vCenter = False
  27.  
  28.         $thumb = thumb_Init($hGUI, $aList[$i][1], $x, $y, $maxWidth, $maxHeight, $vCenter, $hCenter)
  29.         _ArrayAdd1DtoArray2D($aThumb, $thumb)
  30.         $x += $maxWidth +$margin
  31.         If $x+$maxWidth > $size[0] Then
  32.             $x = 10
  33.             $y += $maxHeight +$margin
  34.         EndIf
  35.     Next
  36.  
  37.     ;  GDIPlus draw area example
  38.     _GDIPlus_Startup()
  39.     Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
  40.     _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
  41.     Global $hPen = _GDIPlus_PenCreate(0xFFFF0000, 1)
  42.     ; GDIPlus init draw string
  43.     Global $hBrush = _GDIPlus_BrushCreateSolid(0xFFBFBFBF)
  44.     Global $hFormat = _GDIPlus_StringFormatCreate()
  45.     Global $hFamily = _GDIPlus_FontFamilyCreate("Arial")
  46.     Global $hFont = _GDIPlus_FontCreate($hFamily, 10)
  47.  
  48.     Global $sizeGUI = [0,0]
  49.     ; Example update
  50.     _Timer_SetTimer($hGUI, 0, "ExampleUpdateThumbnails")
  51.  
  52.     ; Main loop
  53.     While 1
  54.         $nMsg = GUIGetMsg()
  55.         Switch $nMsg
  56.             Case $GUI_EVENT_CLOSE
  57.                 Exit
  58.  
  59.         EndSwitch
  60.     WEnd
  61.  
  62.     ; Clean up resources
  63.     _GDIPlus_FontDispose($hFont)
  64.     _GDIPlus_FontFamilyDispose($hFamily)
  65.     _GDIPlus_StringFormatDispose($hFormat)
  66.     _GDIPlus_BrushDispose($hBrush)
  67.     _GDIPlus_GraphicsDispose($hGraphics)
  68.     _GDIPlus_Shutdown()
  69. EndFunc
  70.  
  71. ; Example update
  72. Func ExampleUpdateThumbnails($hWnd, $iMsg, $iIDTimer, $iTime)
  73.                    #forceref $hWnd, $iMsg, $iIDTimer, $iTime
  74.     Local $tREC = DllStructCreate('struct;long left;long top;long right;long bottom;endstruct')
  75.     For $i = 0 To UBound($aThumb) - 1
  76.         Local $thumb = $aThumb[$i][0], $x = $aThumb[$i][1], $y = $aThumb[$i][2], $maxWidth = $aThumb[$i][3], $maxHeight = $aThumb[$i][4], $hCenter = $aThumb[$i][5], $vCenter = $aThumb[$i][6], $size = WinGetClientSize($hGUI)
  77.  
  78.         ; response width
  79.         If $i > 0 Then
  80.             If $aThumb[$i-1][1] + $margin + 2*$maxWidth > $size[0] Then
  81.                 $x = $aThumb[0][1]
  82.                 $y = $aThumb[$i-1][2]+$maxHeight+$margin + 30
  83.             Else
  84.                 $x = $aThumb[$i-1][1]+$maxWidth+$margin
  85.                 $y = $aThumb[$i-1][2]
  86.             EndIf
  87.  
  88.         EndIf
  89.  
  90.         If $sizeGUI[0] <> $size[0] Or $sizeGUI[1] <> $size[1]  Then
  91.             ; Draw string
  92.             Local $tLayout = _GDIPlus_RectFCreate($x, $y+$maxHeight+10, $maxWidth, 25)
  93.             DllStructSetData($tREC, "left", DllStructGetData($tLayout, 1))
  94.             DllStructSetData($tREC, "top", DllStructGetData($tLayout, 2))
  95.             DllStructSetData($tREC, "right", DllStructGetData($tLayout, 1)+DllStructGetData($tLayout, 3))
  96.             DllStructSetData($tREC, "bottom", DllStructGetData($tLayout, 2)+DllStructGetData($tLayout, 4))
  97.             _WinAPI_RedrawWindow($hGUI, $tREC, 0, $RDW_ERASE+$RDW_INVALIDATE+$RDW_UPDATENOW+$RDW_FRAME+$RDW_ALLCHILDREN)
  98.             _GDIPlus_GraphicsDrawStringEx($hGraphics, '$hCenter: '&$hCenter&', $vCenter: '&$vCenter, $hFont, $tLayout, $hFormat, $hBrush)
  99.  
  100.             ; Draw rect area
  101.             _GDIPlus_GraphicsDrawRect($hGraphics, $x-1, $y-1, $maxWidth+2, $maxHeight+2, $hPen)
  102.         EndIf
  103.  
  104.         ; Update
  105.         $arr = thumbnail_Update($thumb, $x, $y, $maxWidth, $maxHeight, $hCenter, $vCenter)
  106.         _ArrayUpdate1DtoArray2D($aThumb, $arr, $i)
  107.     Next
  108.  
  109.     $sizeGUI[0] = $size[0]
  110.     $sizeGUI[1] = $size[1]
  111. EndFunc   ;==>ExampleUpdateThumbnails
  112.  
  113. ; Functions --------------------------------------------------------------------------------------------------------------
  114. Func thumb_Init($hDestination, $hSource, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter = False, $vCenter = False)
  115.     Local $hThumbnail = _WinAPI_DwmRegisterThumbnailFix($hDestination, $hSource)
  116.     return thumbnail_Update($hThumbnail, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter, $vCenter)
  117. EndFunc
  118.  
  119. ; params
  120. ;           $hCenter    horizontal center
  121. ;           $vCenter    vertical center
  122. Func thumbnail_Update($hThumbnail, $x = 0, $y = 0, $iMaxWidth = 0, $iMaxHeight = 0, $hCenter = False, $vCenter = False)
  123.     ; Init params in array
  124.     If IsArray($hThumbnail) Then
  125.         $x = $hThumbnail[1]
  126.         $y = $hThumbnail[2]
  127.         $iMaxWidth = $hThumbnail[3]
  128.         $iMaxHeight = $hThumbnail[4]
  129.         $hCenter = $hThumbnail[5]
  130.         $vCenter = $hThumbnail[6]
  131.         $hThumbnail = $hThumbnail[0]
  132.     EndIf
  133.  
  134.     ; Get Size of thumbnail
  135.     Local $tSIZE = _WinAPI_DwmQueryThumbnailSourceSize($hThumbnail)
  136.     Local $iWidth = DllStructGetData($tSIZE, 1)
  137.     Local $iHeight = DllStructGetData($tSIZE, 2)
  138.  
  139.     ; Scale with aspect ratio
  140.     Local $aScale = thumbnail_Scale($iWidth, $iHeight, $iMaxWidth, $iMaxHeight)
  141.  
  142.     ; Position and center
  143.     Local $cx = $x, $cy = $y
  144.     If $hCenter Then $cx -= ($aScale[0]-$iMaxWidth)/2
  145.     If $vCenter Then $cy -= ($aScale[1]-$iMaxHeight)/2
  146.  
  147.     ; Rect
  148.     Local $tDstRect = _WinAPI_CreateRectEx($cx, $cy, $aScale[0], $aScale[1])
  149.     Local $tSrcRect = _WinAPI_CreateRectEx(0, 0, $iWidth, $iHeight)
  150.     ; Update thumbnail to form
  151.     _WinAPI_DwmUpdateThumbnailProperties($hThumbnail, True, True, 255, $tDstRect, $tSrcRect)
  152.  
  153.     Local $aRet[7] = [$hThumbnail, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter, $vCenter]
  154.     return $aRet
  155. EndFunc
  156.  
  157. Func thumbnail_Scale($iWidth, $iHeight, $iMaxWidth = 0, $iMaxHeight = 0)
  158.     Local $iWidthScale = $iWidth > $iMaxWidth ? $iMaxWidth : $iWidth
  159.     Local $iHeightScale = $iHeight*($iWidthScale/$iWidth)
  160.  
  161.     If $iHeightScale > $iMaxHeight Then
  162.         $iHeightScale = $iMaxHeight
  163.         $iWidthScale = $iWidth*($iHeightScale/$iHeight)
  164.     EndIf
  165.  
  166.     Local $aRet = [$iWidthScale, $iHeightScale]
  167.     Return $aRet
  168. EndFunc
  169.  
  170. Func _WinAPI_DwmRegisterThumbnailFix($hDestination, $hSource)
  171.     ; 'dwmapi.dll' -> DllOpen('dwmapi.dll')        add DllOpen to fix them
  172.     ;Local $aCall = DllCall('dwmapi.dll', 'long', 'DwmRegisterThumbnail', 'hwnd', $hDestination, 'hwnd', $hSource, 'handle*', 0)
  173.     Local $aCall = DllCall(DllOpen('dwmapi.dll'), 'long', 'DwmRegisterThumbnail', 'hwnd', $hDestination, 'hwnd', $hSource, 'handle*', 0)
  174.     If @error Then Return SetError(@error, @extended, 0)
  175.     If $aCall[0] Then Return SetError(10, $aCall[0], 0)
  176.  
  177.     Return $aCall[3]
  178. EndFunc   ;==>_WinAPI_DwmRegisterThumbnailFix
  179.  
  180. Func _ArrayAdd1DtoArray2D(ByRef $aArray2D, Const $aArray1D)
  181.     Local Const $iRows = UBound($aArray2D, $UBOUND_ROWS)
  182.     Local Const $iCols = UBound($aArray2D, $UBOUND_COLUMNS)
  183.  
  184.     ReDim $aArray2D[$iRows + 1][$iCols]
  185.  
  186.     For $i1 = 0 To $iCols - 1
  187.         $aArray2D[$iRows][$i1] = $aArray1D[$i1]
  188.     Next
  189. EndFunc
  190.  
  191. Func _ArrayUpdate1DtoArray2D(ByRef $aArray2D, Const $aArray1D, $iIndex = -1)
  192.     If $iIndex < 0 Then Return
  193.  
  194.     For $i = 0 To UBound($aArray1D) - 1
  195.         $aArray2D[$iIndex][$i] = $aArray1D[$i]
  196.     Next
  197. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement