Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WinAPIGdi.au3>
- #include <Timers.au3>
- #include <Array.au3>
- #include <GUIConstantsEx.au3>
- #include <WindowsConstants.au3>
- #include <GDIPlus.au3>
- Example()
- Func Example()
- #Region ### START Koda GUI section ### Form=
- Global $hGUI = GUICreate('Example WinAPI [DwmUpdateThumbnailProperties]', 950, 450, -1, -1, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
- GUISetBkColor(0x506070)
- GUISetState(@SW_SHOW)
- #EndRegion ### END Koda GUI section ###
- Local $aList = WinList('[REGEXPTITLE:(?i)(.*Google Chrome.*)]') ; get list handle
- Local $x = 10, $y = 10, $maxWidth = 300, $maxHeight = 169, $size = WinGetClientSize($hGUI)
- Global $aThumb[0][7], $margin = 10
- Local $vCenter = False, $hCenter = False
- For $i = 1 To $aList[0][0]
- If $i = 2 Then $vCenter = True
- If $i = 3 Then $hCenter = True
- If $i = 4 Then $vCenter = False
- $thumb = thumb_Init($hGUI, $aList[$i][1], $x, $y, $maxWidth, $maxHeight, $vCenter, $hCenter)
- _ArrayAdd1DtoArray2D($aThumb, $thumb)
- $x += $maxWidth +$margin
- If $x+$maxWidth > $size[0] Then
- $x = 10
- $y += $maxHeight +$margin
- EndIf
- Next
- ; GDIPlus draw area example
- _GDIPlus_Startup()
- Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
- _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
- Global $hPen = _GDIPlus_PenCreate(0xFFFF0000, 1)
- ; GDIPlus init draw string
- Global $hBrush = _GDIPlus_BrushCreateSolid(0xFFBFBFBF)
- Global $hFormat = _GDIPlus_StringFormatCreate()
- Global $hFamily = _GDIPlus_FontFamilyCreate("Arial")
- Global $hFont = _GDIPlus_FontCreate($hFamily, 10)
- Global $sizeGUI = [0,0]
- ; Example update
- _Timer_SetTimer($hGUI, 0, "ExampleUpdateThumbnails")
- ; Main loop
- While 1
- $nMsg = GUIGetMsg()
- Switch $nMsg
- Case $GUI_EVENT_CLOSE
- Exit
- EndSwitch
- WEnd
- ; Clean up resources
- _GDIPlus_FontDispose($hFont)
- _GDIPlus_FontFamilyDispose($hFamily)
- _GDIPlus_StringFormatDispose($hFormat)
- _GDIPlus_BrushDispose($hBrush)
- _GDIPlus_GraphicsDispose($hGraphics)
- _GDIPlus_Shutdown()
- EndFunc
- ; Example update
- Func ExampleUpdateThumbnails($hWnd, $iMsg, $iIDTimer, $iTime)
- #forceref $hWnd, $iMsg, $iIDTimer, $iTime
- Local $tREC = DllStructCreate('struct;long left;long top;long right;long bottom;endstruct')
- For $i = 0 To UBound($aThumb) - 1
- 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)
- ; response width
- If $i > 0 Then
- If $aThumb[$i-1][1] + $margin + 2*$maxWidth > $size[0] Then
- $x = $aThumb[0][1]
- $y = $aThumb[$i-1][2]+$maxHeight+$margin + 30
- Else
- $x = $aThumb[$i-1][1]+$maxWidth+$margin
- $y = $aThumb[$i-1][2]
- EndIf
- EndIf
- If $sizeGUI[0] <> $size[0] Or $sizeGUI[1] <> $size[1] Then
- ; Draw string
- Local $tLayout = _GDIPlus_RectFCreate($x, $y+$maxHeight+10, $maxWidth, 25)
- DllStructSetData($tREC, "left", DllStructGetData($tLayout, 1))
- DllStructSetData($tREC, "top", DllStructGetData($tLayout, 2))
- DllStructSetData($tREC, "right", DllStructGetData($tLayout, 1)+DllStructGetData($tLayout, 3))
- DllStructSetData($tREC, "bottom", DllStructGetData($tLayout, 2)+DllStructGetData($tLayout, 4))
- _WinAPI_RedrawWindow($hGUI, $tREC, 0, $RDW_ERASE+$RDW_INVALIDATE+$RDW_UPDATENOW+$RDW_FRAME+$RDW_ALLCHILDREN)
- _GDIPlus_GraphicsDrawStringEx($hGraphics, '$hCenter: '&$hCenter&', $vCenter: '&$vCenter, $hFont, $tLayout, $hFormat, $hBrush)
- ; Draw rect area
- _GDIPlus_GraphicsDrawRect($hGraphics, $x-1, $y-1, $maxWidth+2, $maxHeight+2, $hPen)
- EndIf
- ; Update
- $arr = thumbnail_Update($thumb, $x, $y, $maxWidth, $maxHeight, $hCenter, $vCenter)
- _ArrayUpdate1DtoArray2D($aThumb, $arr, $i)
- Next
- $sizeGUI[0] = $size[0]
- $sizeGUI[1] = $size[1]
- EndFunc ;==>ExampleUpdateThumbnails
- ; Functions --------------------------------------------------------------------------------------------------------------
- Func thumb_Init($hDestination, $hSource, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter = False, $vCenter = False)
- Local $hThumbnail = _WinAPI_DwmRegisterThumbnailFix($hDestination, $hSource)
- return thumbnail_Update($hThumbnail, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter, $vCenter)
- EndFunc
- ; params
- ; $hCenter horizontal center
- ; $vCenter vertical center
- Func thumbnail_Update($hThumbnail, $x = 0, $y = 0, $iMaxWidth = 0, $iMaxHeight = 0, $hCenter = False, $vCenter = False)
- ; Init params in array
- If IsArray($hThumbnail) Then
- $x = $hThumbnail[1]
- $y = $hThumbnail[2]
- $iMaxWidth = $hThumbnail[3]
- $iMaxHeight = $hThumbnail[4]
- $hCenter = $hThumbnail[5]
- $vCenter = $hThumbnail[6]
- $hThumbnail = $hThumbnail[0]
- EndIf
- ; Get Size of thumbnail
- Local $tSIZE = _WinAPI_DwmQueryThumbnailSourceSize($hThumbnail)
- Local $iWidth = DllStructGetData($tSIZE, 1)
- Local $iHeight = DllStructGetData($tSIZE, 2)
- ; Scale with aspect ratio
- Local $aScale = thumbnail_Scale($iWidth, $iHeight, $iMaxWidth, $iMaxHeight)
- ; Position and center
- Local $cx = $x, $cy = $y
- If $hCenter Then $cx -= ($aScale[0]-$iMaxWidth)/2
- If $vCenter Then $cy -= ($aScale[1]-$iMaxHeight)/2
- ; Rect
- Local $tDstRect = _WinAPI_CreateRectEx($cx, $cy, $aScale[0], $aScale[1])
- Local $tSrcRect = _WinAPI_CreateRectEx(0, 0, $iWidth, $iHeight)
- ; Update thumbnail to form
- _WinAPI_DwmUpdateThumbnailProperties($hThumbnail, True, True, 255, $tDstRect, $tSrcRect)
- Local $aRet[7] = [$hThumbnail, $x, $y, $iMaxWidth, $iMaxHeight, $hCenter, $vCenter]
- return $aRet
- EndFunc
- Func thumbnail_Scale($iWidth, $iHeight, $iMaxWidth = 0, $iMaxHeight = 0)
- Local $iWidthScale = $iWidth > $iMaxWidth ? $iMaxWidth : $iWidth
- Local $iHeightScale = $iHeight*($iWidthScale/$iWidth)
- If $iHeightScale > $iMaxHeight Then
- $iHeightScale = $iMaxHeight
- $iWidthScale = $iWidth*($iHeightScale/$iHeight)
- EndIf
- Local $aRet = [$iWidthScale, $iHeightScale]
- Return $aRet
- EndFunc
- Func _WinAPI_DwmRegisterThumbnailFix($hDestination, $hSource)
- ; 'dwmapi.dll' -> DllOpen('dwmapi.dll') add DllOpen to fix them
- ;Local $aCall = DllCall('dwmapi.dll', 'long', 'DwmRegisterThumbnail', 'hwnd', $hDestination, 'hwnd', $hSource, 'handle*', 0)
- Local $aCall = DllCall(DllOpen('dwmapi.dll'), 'long', 'DwmRegisterThumbnail', 'hwnd', $hDestination, 'hwnd', $hSource, 'handle*', 0)
- If @error Then Return SetError(@error, @extended, 0)
- If $aCall[0] Then Return SetError(10, $aCall[0], 0)
- Return $aCall[3]
- EndFunc ;==>_WinAPI_DwmRegisterThumbnailFix
- Func _ArrayAdd1DtoArray2D(ByRef $aArray2D, Const $aArray1D)
- Local Const $iRows = UBound($aArray2D, $UBOUND_ROWS)
- Local Const $iCols = UBound($aArray2D, $UBOUND_COLUMNS)
- ReDim $aArray2D[$iRows + 1][$iCols]
- For $i1 = 0 To $iCols - 1
- $aArray2D[$iRows][$i1] = $aArray1D[$i1]
- Next
- EndFunc
- Func _ArrayUpdate1DtoArray2D(ByRef $aArray2D, Const $aArray1D, $iIndex = -1)
- If $iIndex < 0 Then Return
- For $i = 0 To UBound($aArray1D) - 1
- $aArray2D[$iIndex][$i] = $aArray1D[$i]
- Next
- EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement