Jump to content

GDI+ swap image?


Recommended Posts

Okay based on an example in the help file I've got this.

It draws two images, one on top of the other.

1) How can I change the image order so I can control which one is on top?

2) If possible how can I query which image is currently on top?

The other option of course is just to delete one object and create another in its place but I can't figure that out either.

Hopefully this isn't painfully simple like it seems to end up being for my posts most of the time lol

Thanks,

Kenny

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>

Global $hGUI, $hImage1, $hGraphic1, $hImage2, $hGraphic2

; Create GUI
$hGUI = GUICreate("Show PNG", 350, 301)

; Load PNG image
_GDIPlus_StartUp()
$hImage1   = _GDIPlus_ImageLoadFromFile("red.png")
$hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hImage2   = _GDIPlus_ImageLoadFromFile("green.png")
$hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic1)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_GraphicsDispose($hGraphic2)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_ShutDown()

; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
    _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Okay based on an example in the help file I've got this.

It draws two images, one on top of the other.

1) How can I change the image order so I can control which one is on top?

2) If possible how can I query which image is currently on top?

The other option of course is just to delete one object and create another in its place but I can't figure that out either.

Hopefully this isn't painfully simple like it seems to end up being for my posts most of the time lol

Thanks,

Kenny

#include <GUIConstantsEx.au3>
   #include <WindowsConstants.au3>
   #include <GDIPlus.au3>
   #Include <WinAPI.au3>
   
   Global $hGUI, $hImage1, $hGraphic1, $hImage2, $hGraphic2
   
  ; Create GUI
   $hGUI = GUICreate("Show PNG", 350, 301)
   
  ; Load PNG image
   _GDIPlus_StartUp()
   $hImage1   = _GDIPlus_ImageLoadFromFile("red.png")
   $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
   $hImage2   = _GDIPlus_ImageLoadFromFile("green.png")
   $hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
   
   GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
   GUISetState()
   
  ; Loop until user exits
   do
   until GUIGetMsg() = $GUI_EVENT_CLOSE
   
  ; Clean up resources
   _GDIPlus_GraphicsDispose($hGraphic1)
   _GDIPlus_ImageDispose($hImage1)
   _GDIPlus_GraphicsDispose($hGraphic2)
   _GDIPlus_ImageDispose($hImage2)
   _GDIPlus_ShutDown()
   
  ; Draw PNG image
   Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
       _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
       _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
       _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
       _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
       Return $GUI_RUNDEFMSG
   EndFunc
This is one way of changing the top image.

Click on the GUI to change the top image.

#include <GUIConstantsEx.au3>
 #include <WindowsConstants.au3>
 #include <GDIPlus.au3>
 #include <WinAPI.au3>
 #include <Misc.au3>
 
 Global $hGUI, $hImage1, $hGraphic1, $hImage2, $hGraphic2, $swap = True
 
; Create GUI
 $hGUI = GUICreate("Show PNG", 350, 301)
 
; Load PNG image
 _GDIPlus_Startup()
 $hImage1 = _GDIPlus_ImageLoadFromFile("red.png")
 $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
 $hImage2 = _GDIPlus_ImageLoadFromFile("green.png")
 $hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
 
 GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
 GUISetState()
 
; Loop until user exits
 Do
     If _IsPressed("01") Then
         Do
         Until Not _IsPressed("01")
         $swap = Not $swap
         If $swap Then
             _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
             _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
         Else
             _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
             _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
         EndIf
         $atPosArr = WinGetPos($hGUI)
         WinMove($hGUI, "", $atPosArr[0], $atPosArr[1] + @DesktopHeight)
         WinMove($hGUI, "", $atPosArr[0], $atPosArr[1])
     EndIf
 
 Until GUIGetMsg() = $GUI_EVENT_CLOSE
 
; Clean up resources
 _GDIPlus_GraphicsDispose($hGraphic1)
 _GDIPlus_ImageDispose($hImage1)
 _GDIPlus_GraphicsDispose($hGraphic2)
 _GDIPlus_ImageDispose($hImage2)
 _GDIPlus_Shutdown()
 
; Draw PNG image
 Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
     _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
     If $swap Then
         _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
         _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
     Else
         _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
         _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
     EndIf
     _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
     Return $GUI_RUNDEFMSG
 EndFunc  ;==>MY_WM_PAINT<guiconstantsex.au3><windowsconstants.au3><gdiplus.au3><winapi.au3><misc.au3>
Link to comment
Share on other sites

cool thanks :P

I thought about that about that I thought it might keep generating new objects, but I don't know anything about gdi+ lol

So I don't really need to issue both draw image commands just whatever one I want visible.

One thing, anyway to avoid hardcoding this function?

Basically these are status indicators for a list I'll generate when the app executes.

So I have no way of knowing the coordinates for each instance of this image until it runs.

Thanks,

Kenny

Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
     _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
     If $swap Then
         _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
         _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
     Else
         _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0)
         _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 0, 0)
     EndIf
     _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
     Return $GUI_RUNDEFMSG
EndFunc
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Here's an example of what I ended up doing. Thanks to the info you gave me

I just added the array $Images to store the state of each instance and process the array using a loop.

Thanks for the help,

Kenny

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Global $hGUI, $hImage1, $hGraphic1, $hImage2, $hGraphic2, $swap = True


Global $Images[3]
$Images[1] = 1
$Images[2] = 0


; Create GUI
$hGUI = GUICreate("Show PNG", 350, 301)
$BTN  = GUICtrlCreateButton("Swap", 250, 250)

; Load PNG image
_GDIPlus_Startup()
$hImage1 = _GDIPlus_ImageLoadFromFile("red.png")
$hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hImage2 = _GDIPlus_ImageLoadFromFile("green.png")
$hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

; Loop until user exits
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BTN
            If $Images[1] = 0 Then
                $Images[1] = 1
                $Images[2] = 0
            Else
                $Images[1] = 0
                $Images[2] = 1
            EndIf
            For $i = 1 To UBound($Images) - 1
                If $Images[$i] = 0 Then
                    _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 23, 51 * $i)
                Else
                    _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 23, 51 * $i)
                EndIf
        Next
    EndSwitch
WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic1)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_GraphicsDispose($hGraphic2)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_Shutdown()

; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
     _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
     For $i = 1 To UBound($Images) - 1
        If $Images[$i] = 0 Then
            _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage1, 23, 51 * $i)
        Else
            _GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 23, 51 * $i)
        EndIf
     Next
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
     Return $GUI_RUNDEFMSG
 EndFunc

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...