Jump to content

Gui PNG in PNG and buttons/labels


Recommended Posts

Hello everyone, 

I am trying to make a "gauge" with a transparent PNG as GUI Background (the gauge) and the needle. I have dificculties with the needle...i cannot make it to rotate...

I read a lots of topics openned on this subject but i cannot understand how the script works. :(

I am new with GUI+ so, please be gentle :)

 

This is my work so far, If someone can help me i will be grateful.

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

GUISetState()
$g_hGUI = GUICreate("Test", 356, 356, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()

$Needle     =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\needle.png")
$Background =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\back.png")

$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, 0) ;0 = opaque, -1 = transparent
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, DllStructGetPtr($tColorMatrix))

$g_back     =   _GDIPlus_ImageGetDimension($Background)
$g_Needle   =   _GDIPlus_ImageGetDimension($Needle)

$g_hImage_Background    = _GDIPlus_BitmapCreateFromScan0($g_back[0], $g_back[1])        ; Creeaza imaginea dupa dimensiunile pozei incarcate mai sus
$g_hImage_Needle        = _GDIPlus_BitmapCreateFromScan0($g_Needle[0], $g_Needle[1])    ; Adaugat

$g_hGfx_Background      = _GDIPlus_ImageGetGraphicsContext($g_hImage_Background)
$g_hGfx_needle          = _GDIPlus_ImageGetGraphicsContext($g_hImage_Needle)                ; Adaugat

_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)

$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $g_Needle[0], $g_Needle[1])


__Rotate(0)
__Rotate(5)
__Rotate(10)
__Rotate(15)



Func __Rotate($i)
    ;_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)
_GDIPlus_GraphicsClear($g_hGfx_needle, 0xFF000000)
_GDIPlus_MatrixRotate($hMatrix, $i)
_GDIPlus_GraphicsSetTransform($g_hGfx_Background, $hMatrix)
;_GDIPlus_GraphicsDrawImageRect($g_hGfx_Background, $Needle, 0, 0, -$g_Needle[0], -$g_Needle[1])
_GDIPlus_GraphicsDrawImageRect($g_hGfx_Background, $Needle, -$g_Needle[0], -$g_Needle[1], $g_Needle[0], $g_Needle[1])
EndFunc


SetBitmap($g_hGUI, $g_hImage_Background, 255)
GUISetState()


; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
_GDIPlus_ImageDispose($Needle)
_GDIPlus_ImageDispose($g_hImage_Background)
_GDIPlus_ImageDispose($Background)
_GDIPlus_GraphicsDispose($g_hGfx_Background)
_GDIPlus_Shutdown()

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

back.png

needle.png

Link to post
Share on other sites

The needle is copied to the background each time, you will also need to clear the background as well as the needle and redraw each time you rotate. Try this:

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $hAttribute_Alpha
Global $tColorMatrix
Global $g_back
Global $g_Needle
Global $g_hImage_Background
Global $g_hImage_Needle
Global $g_hGfx_Background
Global $g_hGfx_needle
Global $hMatrix


GUISetState()
$g_hGUI = GUICreate("Test", 356, 356, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()

$Needle     =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\needle.png")
$Background =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\back.png")

Func _CreateGraphic()
$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, 0) ;0 = opaque, -1 = transparent
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, DllStructGetPtr($tColorMatrix))

$g_back     =   _GDIPlus_ImageGetDimension($Background)
$g_Needle   =   _GDIPlus_ImageGetDimension($Needle)

$g_hImage_Background    = _GDIPlus_BitmapCreateFromScan0($g_back[0], $g_back[1])        ; Creeaza imaginea dupa dimensiunile pozei incarcate mai sus
$g_hImage_Needle        = _GDIPlus_BitmapCreateFromScan0($g_Needle[0], $g_Needle[1])    ; Adaugat

$g_hImage_Needle = _GDIPlus_ImageResize($g_hImage_Needle, 10, 10)

$g_hGfx_Background      = _GDIPlus_ImageGetGraphicsContext($g_hImage_Background)
$g_hGfx_needle          = _GDIPlus_ImageGetGraphicsContext($g_hImage_Needle)                ; Adaugat

_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)

$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $g_Needle[0], $g_Needle[1])
EndFunc

$Rotation = 0

; Loop until user exits
While Not GUIGetMsg() = $GUI_EVENT_CLOSE
    If $Rotation=360 Then
        $Rotation=0
        EndIf
    $Rotation += 5;
    __Rotate($Rotation)
    ConsoleWrite($Rotation & @CRLF)
WEnd


Func __Rotate($i)
_CreateGraphic()
_GDIPlus_GraphicsClear($g_hGfx_needle, 0xFF000000)
_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)
$g_hGfx_Background      = _GDIPlus_ImageGetGraphicsContext($g_hImage_Background)
_GDIPlus_MatrixRotate($hMatrix, $i)
_GDIPlus_GraphicsSetTransform($g_hGfx_Background, $hMatrix)
_GDIPlus_GraphicsDrawImageRect($g_hGfx_Background, $Needle, -$g_Needle[0], -$g_Needle[1], $g_Needle[0], $g_Needle[1])
SetBitmap($g_hGUI, $g_hImage_Background, 255)
GUISetState()
EndFunc


IF GUIGetMsg=$GUI_EVENT_CLOSE Then
_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
_GDIPlus_ImageDispose($Needle)
_GDIPlus_ImageDispose($g_hImage_Background)
_GDIPlus_ImageDispose($Background)
_GDIPlus_GraphicsDispose($g_hGfx_Background)
_GDIPlus_Shutdown()
EndIf

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

Edited by Sidley
Added code
Link to post
Share on other sites

Your matrix is rotating around the bottom right corner, set the matrix to rotate around the centre (half the image width, half the image height, line 42). You will also need to offset the drawn graphic by that much to compensate (line 66). Test label on line 63. You've also got a memory leak, but that's for you to track ;)

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $hAttribute_Alpha
Global $tColorMatrix
Global $g_back
Global $g_Needle
Global $g_hImage_Background
Global $g_hImage_Needle
Global $g_hGfx_Background
Global $g_hGfx_needle
Global $hMatrix


GUISetState()
$g_hGUI = GUICreate("Test", 356, 356, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()

$Needle     =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\needle.png")
$Background =   _GDIPlus_ImageLoadFromFile(@ScriptDir & "\back.png")

Func _CreateGraphic()
$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, 0) ;0 = opaque, -1 = transparent
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, DllStructGetPtr($tColorMatrix))

$g_back     =   _GDIPlus_ImageGetDimension($Background)
$g_Needle   =   _GDIPlus_ImageGetDimension($Needle)

$g_hImage_Background    = _GDIPlus_BitmapCreateFromScan0($g_back[0], $g_back[1])        ; Creeaza imaginea dupa dimensiunile pozei incarcate mai sus
$g_hImage_Needle        = _GDIPlus_BitmapCreateFromScan0($g_Needle[0], $g_Needle[1])    ; Adaugat

$g_hGfx_Background      = _GDIPlus_ImageGetGraphicsContext($g_hImage_Background)
$g_hGfx_needle          = _GDIPlus_ImageGetGraphicsContext($g_hImage_Needle)                ; Adaugat

_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)

$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $g_Needle[0]/2, $g_Needle[1]/2)
EndFunc
; Loop until user exits
$Rotation = 0
;Do
While Not GUIGetMsg() = $GUI_EVENT_CLOSE
    If $Rotation=365 Then
        $Rotation=0
        EndIf
    __Rotate($Rotation)
    $Rotation += 5;
    ConsoleWrite($Rotation & @CRLF)
    Sleep(1000)
WEnd


Func __Rotate($i)
_CreateGraphic()
_GDIPlus_GraphicsClear($g_hGfx_needle, 0xFF000000)
_GDIPlus_GraphicsDrawImageRectRect($g_hGfx_Background, $Background, 0, 0, $g_back[0], $g_back[1], 0, 0, $g_back[0], $g_back[1], $hAttribute_Alpha)
$g_hGfx_Background      = _GDIPlus_ImageGetGraphicsContext($g_hImage_Background)
_GDIPlus_GraphicsDrawString($g_hGfx_Background, "Test", $g_back[0]/2, 30)
_GDIPlus_MatrixRotate($hMatrix, $i)
_GDIPlus_GraphicsSetTransform($g_hGfx_Background, $hMatrix)
_GDIPlus_GraphicsDrawImageRect($g_hGfx_Background, $Needle, -$g_Needle[0]/2, -$g_Needle[1]/2, $g_Needle[0], $g_Needle[1])
SetBitmap($g_hGUI, $g_hImage_Background, 255)
GUISetState()
EndFunc


IF GUIGetMsg=$GUI_EVENT_CLOSE Then
_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
_GDIPlus_ImageDispose($Needle)
_GDIPlus_ImageDispose($g_hImage_Background)
_GDIPlus_ImageDispose($Background)
_GDIPlus_GraphicsDispose($g_hGfx_Background)
_GDIPlus_Shutdown()
EndIf

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

Link to post
Share on other sites
#include <GDIPlus.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

$hImgBack = _GDIPlus_ImageLoadFromFile("back.png")
If @error Then Exit MsgBox(0, "Error", "Can't load back.png")
$hImgNeedle = _GDIPlus_ImageLoadFromFile("needle.png")
If @error Then Exit MsgBox(0, "Error", "Can't load needle.png")
_GDIPlus_BitmapSetResolution($hImgBack, 96, 96)
_GDIPlus_BitmapSetResolution($hImgNeedle, 96, 96)

$aSize = _GDIPlus_ImageGetDimension($hImgBack)
$hGUI = GUICreate("", $aSize[0], $aSize[1], -1, -1, -1, BitOR($WS_EX_LAYERED, $WS_EX_TOOLWINDOW, $WS_EX_CONTROLPARENT))
GUISetState()

$hImgBuff = _GDIPlus_BitmapCreateFromScan0($aSize[0], $aSize[1])
$hGraphBuff = _GDIPlus_ImageGetGraphicsContext($hImgBuff)
$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $aSize[0] / 2, $aSize[1] / 2)

AdlibRegister("Update", 100)

Do
Until GUIGetMsg() = -3 ; $GUI_EVENT_CLOSE

_GDIPlus_ImageDispose($hImgBack)
_GDIPlus_ImageDispose($hImgNeedle)
_GDIPlus_ImageDispose($hImgBuff)
_GDIPlus_GraphicsDispose($hGraphBuff)
_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_Shutdown()

Func Update()
  _GDIPlus_GraphicsClear($hGraphBuff, 0x00000000)
  _GDIPlus_GraphicsResetTransform($hGraphBuff)
  _GDIPlus_GraphicsDrawImage($hGraphBuff, $hImgBack, 0, 0)
  _GDIPlus_MatrixRotate($hMatrix, 5)
  _GDIPlus_GraphicsSetTransform($hGraphBuff, $hMatrix)
  _GDIPlus_GraphicsDrawImage($hGraphBuff, $hImgNeedle, -$aSize[0] / 2, -$aSize[1] / 2)
  $hBMP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImgBuff)
  _WinAPI_UpdateLayeredWindowEx($hGUI, -1, -1, $hBMP, 255, True)
EndFunc

 

Edited by InnI
refactoring
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...