Search the Community
Showing results for tags 'alpha channel'.
-
After UEZ helped me with the Countdown Timer his techniques seemed perfect for displaying individual images with transparency. One thing led to another and here's the resulting script. Download the attached zip file and place the 5 PNG files in the same folder as this script. I did not embed the images in the script, to make the script easier to read and so you can see the actual images used. In my opinion the ultimate next step would be to add the ability to play animated GIFs, still with transparency if that's possible. Enjoy! (with much gratitude to UEZ for his help) ; Functions coded by UEZ 2013-10-29 ; Updated to address issues 2013-10-31 #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #NoTrayIcon _GDIPlus_Startup() ;initiate GDI+ #region ; timmy2's simple demonstration of UEZ's code ; ; display blank chalkboard (there will be no fade-in so use default $iAlpha value of 255) $aRes1 = DisplayImage( "Wood-framed-chalkboard.png", -1, -1 ) Sleep (500) ; add first two steps $aRes2 = DisplayImage("step-1.png", -1, -1 ) Sleep(2000) $aRes3 = DisplayImage("step-2.png", -1, -1 ) ; fade in handdrawn circle (therefore start with $iAlpha = 0) $aPos = WinGetPos($aRes1[0]) ;required because the chalkboard is displayed at screen center, which varies based on resolution (doh!) $aRes4 = DisplayImage("circle.png", $aPos[0] + 290, $aPos[1] + 75, 0) sleep (500) _Fader($aRes4) sleep(1000) _Fader($aRes4, False) ReleaseResources($aRes4) sleep(500) ; add third step $aRes5 = DisplayImage("step-3.png", -1, -1 ) Sleep(2000) ; fade the three steps out in reverse order (faster than defaults) _Fader($aRes5, False, 255, 5, 5) ReleaseResources($aRes5) _Fader($aRes3, False, 255, 5, 5) ReleaseResources($aRes3) _Fader($aRes2, False, 255, 5, 5) ReleaseResources($aRes2) Sleep(500) ; fade out chockboard _Fader($aRes1, False, 255, 10, 5) ReleaseResources($aRes1) #endregion _GDIPlus_Shutdown() Exit ; Display the image at center unless otherwise specified. If Fader will not be used then $iAlpha's default of 0xFF is good. ; If Fader will be used then start with $iAlpha equal to zero. Option to make this image "Always on top" is also available. Func DisplayImage($sFile, $iPosX = -1, $iPosY = -1, $iAlpha = 0xFF, $bTopmost = True) Local Const $hBmp_Background = _GDIPlus_BitmapCreateFromFile($sFile) ;load the image If @error Then Return SetError(1, 0, 0) ;image cannot be loaded Local Const $iW = _GDIPlus_ImageGetWidth($hBmp_Background), $iH = _GDIPlus_ImageGetHeight($hBmp_Background) ;get the dimension of the background image Local Const $hGUI = GUICreate("", $iW, $iH, $iPosX, $iPosY, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST * $bTopmost, $WS_EX_TOOLWINDOW)) ;create GUI with appropriate styles and extented style (borderless transparent GUI) GUICtrlCreateLabel("", 0, 0, $iW, $iH, Default, $GUI_WS_EX_PARENTDRAG) ;create a hidden label for GUI dragging GUISetState(@SW_SHOW, $hGUI) ;show GUI Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH) ;define an empty bitmap where all the gfx stuff will copied to Local Const $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap) ;get the context to the bitmap to be able to copy / draw to the bitmap _GDIPlus_GraphicsDrawImageRect($hGfx, $hBmp_Background, 0, 0, $iW, $iH) ;draw background image to the empty bitmap ;display GDI+ with transparency on desktop Local Const $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) ;convert GDI+ image to GDI to display it on the screen using GDI functions Local Const $hScrDC = _WinAPI_GetDC($hGUI) ;get the device context (dc) handle of the GUI Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC) ;create a compatible dc handle Local Const $hOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) ;selects the GDI bitmap object into the specified device context Local Const $tSize = DllStructCreate($tagSIZE) ;create a $tagSIZE struct (x = width, y = height) DllStructSetData($tSize, "X", $iW) ;set data for width DllStructSetData($tSize, "Y", $iH) ;set data for height Local $tSource = DllStructCreate($tagPOINT) ;create a $tagPOINT struct (x = x position, y = y position) Local $tBlend = DllStructCreate($tagBLENDFUNCTION) ;create $tagBLENDFUNCTION struct -> see help file for more info DllStructSetData($tBlend, "Alpha", $iAlpha) ;set the alpha channel of the GUI -> 255 = opaque, 0 = transparent DllStructSetData($tBlend, "Format", 1) ;set the format to 1 -> bitmap has alpha channels DllCall("user32.dll", "bool", "UpdateLayeredWindow", "hwnd", $hGUI, "handle", $hScrDC, "ptr", 0, "struct*", $tSize, "handle", $hMemDC, "struct*", $tSource, "dword", 0, "struct*", $tBlend, "dword", $ULW_ALPHA) ;display bitmap on screen ;release resources otherwise memory will filled up (memory leak) _GDIPlus_GraphicsDispose($hGfx) _GDIPlus_BitmapDispose($hBitmap) Local $aResource[7] = [$hGUI, $hScrDC, $hHBitmap, $hMemDC, $tBlend, $tSize, $tSource] ;return the handle to release it later Return $aResource EndFunc ;==>DisplayImage ; This function releases the resources of a specific image and closes it Func ReleaseResources(ByRef $aResource) If Not IsArray($aResource) Then Return SetError(1, 0, 0) If UBound($aResource) <> 7 Then Return SetError(2, 0, 0) _WinAPI_ReleaseDC($aResource[0], $aResource[1]) _WinAPI_DeleteDC($aResource[3]) _WinAPI_DeleteObject($aResource[2]) GUIDelete($aResource[0]) EndFunc ;==>ReleaseResources ; To fade in an image leave $bIn at default (True); to fade out specify False. ; There are also options to set the ending transparency level (0 transparent, 255 opaque), speed and delay. ; If you change the speed or delay be sure to specify $bIn and $iTrans, otherwise you'll wonder why the fade-in or out isn't what you hoped for. ; The default values are specified in the function below. Func _Fader($res1, $bIn = True, $iTrans = 255, $speed = 3, $delay = 10 ) If Not IsArray($res1) Then Return SetError(1, 0, 0) If UBound($res1) <> 7 Then Return SetError(2, 0, 0) Switch $bIn Case True For $a = 0 To $iTrans Step $speed DllStructSetData($res1[4], "Alpha", $a) DllCall("user32.dll", "bool", "UpdateLayeredWindow", "hwnd", $res1[0], "handle", $res1[1], "ptr", 0, "struct*", $res1[5], "handle", $res1[3], "struct*", $res1[6], "dword", 0, "struct*", $res1[4], "dword", $ULW_ALPHA) ;display bitmap on screen Sleep($delay) Next Case Else For $a = $iTrans To 0 Step -$speed DllStructSetData($res1[4], "Alpha", $a) DllCall("user32.dll", "bool", "UpdateLayeredWindow", "hwnd", $res1[0], "handle", $res1[1], "ptr", 0, "struct*", $res1[5], "handle", $res1[3], "struct*", $res1[6], "dword", 0, "struct*", $res1[4], "dword", $ULW_ALPHA) ;display bitmap on screen Sleep($delay) Next EndSwitch EndFunc ;==>_FadeIn #region GDI and GDI+ functions Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iPixelFormat = $GDIP_PXF32ARGB, $iStride = 0, $pScan0 = 0) Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "handle*", 0) If @error Then Return SetError(@error, @extended, 0) If $aResult[0] Then Return SetError(10, $aResult[0], 0) Return $aResult[6] EndFunc ;==>_GDIPlus_BitmapCreateFromScan0 #endregion GDI and GDI+ functions images.zip
- 23 replies
-
- PNG
- alpha channel
-
(and 2 more)
Tagged with: