﻿#include-once
#include <FontConstants.au3>
#include <SendMessage.au3>
#include <StaticConstants.au3>
#include <StructureConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIGdiInternals.au3>
#include <WinAPIHObj.au3>
#include <WinAPIInternals.au3>
#include <WinAPISysInternals.au3>
#include <WinAPITheme.au3>
#include <WindowsConstants.au3>

Global Enum $PBST_NORMAL = 0, $PBST_ERROR, $PBST_PAUSED, $PBST_MIXED, $PBST_PERCENT

Func _WinAPI_ThemeSetProgress($hWnd, $iPercent, $iState = $PBST_PERCENT, $iFontSize = 14, $sFont = 'Verdana')
	Local $tRect, $tClip, $iW, $iH, $hDev, $hDC, $hSource, $hSv, $hFont, $hOldFont, $hTheme, $hBitmap, $hObj
	If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
	$tRect = _WinAPI_GetClientRect($hWnd)
	If @error Then Return
	$iW = $tRect.Right - $tRect.Left
	$iH = $tRect.Bottom - $tRect.Top
	$tRect.Left = 0
	$tRect.Top = 0
	$tRect.Right = $iW
	$tRect.Bottom = $iH
	$tClip = DllStructCreate($tagRECT)
	$tClip.Left = 1
	$tClip.Top = 1
	$tClip.Right = $iW - 1
	$tClip.Bottom = $iH - 1
	$hDev = _WinAPI_GetDC($hWnd)
	$hDC = _WinAPI_CreateCompatibleDC($hDev)
	$hSource = _WinAPI_CreateCompatibleBitmap($hDev, $iW, $iH)
	$hSv = _WinAPI_SelectObject($hDC, $hSource)
	$hFont = _WinAPI_CreateFont(-$iFontSize, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $sFont)
	_WinAPI_SetBkMode($hDC, $TRANSPARENT)
	$hOldFont = _WinAPI_SelectObject($hDC, $hFont)
	$hTheme = _WinAPI_OpenThemeData($hWnd, 'Progress')
	If Not @error Then
		_WinAPI_DrawThemeBackground($hTheme, 1, 1, $hDC, $tRect)
		$tRect.Right = $iW / 100 * $iPercent
		_WinAPI_DrawThemeBackground($hTheme, 5, 1 + BitAND($iState, 3), $hDC, $tRect, $tClip)
		If BitAND($iState, $PBST_PERCENT) Then _WinAPI_DrawThemeText($hTheme, 1, 1, $hDC, $iPercent & '%', $tClip, BitOR($DT_CENTER, $DT_SINGLELINE, $DT_VCENTER))
		_WinAPI_CloseThemeData($hTheme)
	EndIf
	_WinAPI_SelectObject($hDC, $hOldFont)
	_WinAPI_DeleteObject($hFont)
	$hBitmap = _WinAPI_CreateCompatibleBitmap($hDev, $iW, $iH)
	_WinAPI_SelectObject($hDC, $hBitmap)
	_WinAPI_DrawBitmap($hDC, 0, 0, $hSource, $MERGECOPY)
	_WinAPI_ReleaseDC($hWnd, $hDev)
	_WinAPI_SelectObject($hDC, $hSv)
	_WinAPI_DeleteObject($hSource)
	_WinAPI_DeleteDC($hDC)
	$hObj = _SendMessage($hWnd, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap)
	If $hObj Then _WinAPI_DeleteObject($hObj)
EndFunc   ;==>_WinAPI_ThemeSetProgress
