Jump to content

Using a png in my program


Recommended Posts

  • Replies 81
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Try this:

#include <GUIConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>

Opt("MustDeclareVars", 1)

Global $hGUI, $hImage, $hGraphic, $hImage1

$hGUI = GUICreate("Show PNG", 260, 260,-1,-1,0x80000000)

_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\MyPic.png")
$hGraphic = _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($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_ShutDown()


; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0,0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

Ok. It works relatively the same as szhlopps version does but i dont know what to change in his version that will allow me to make more than one png on the screen.

What exactly do you want to do?

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

This is what I want to do:

I have multiple guis in a separate program that are all really child guis. They are dragged around on the screen and I want to be able to have a png image file layed on each of them. (Just like a bmp or gif)

So if I have 6 little draggable child guis i want to create a png file on each of them just like you would load a bmp or gif file on them.

The reason Im using pngs is because they are the best quality and I also want parts of the little draggable gui to look transparent.

Edited by IWantIt
Link to comment
Share on other sites

Bump

Such bumping is pointless.

In post #22 you were given nice solution.

If you don't understand how to use it then go to www.rentacoder.com - somebody will make it for you.

Or you can post here simplified version of your child-GUI script and somebody can accomodate WM_PAINT solution for it.

Link to comment
Share on other sites

Mr. Zedna:

this is what i posted in reply to post 22:

Ok. It works relatively the same as szhlopps version does but i dont know what to change in his version that will allow me to make more than one png on the screen.

i want to be able to have multiple pngs... instead of one and i dont know how?

Link to comment
Share on other sites

  • 3 weeks later...

I don't see the problem, the solution is GDI+, and frankly, it's not that hard considering you have got examples on how to do it.

Here's the steps:

When you create a GUI (parent, child whatever) you get a window handle, AKA hWnd.

With this handle you can create a Graphics object, this gives you the ability to draw whatever you like on the window (like for example images loaded by the appropriate GDI+ functions).

Up to now everything is great in Windows Land, but there is still a villain on the loose, he is called "repainting" and there is no escape from him, he shows himself every time a window hovers over your window*, your window is dragged outside the screen or when your window is being restored from a minimized state.

Every window that lives in Windows Land must deal with him and the only possible solutions are 1) Constantly redrawing the window or 2) waiting for windows to send a message to let you know that repainting must be done. This message is called WM_PAINT and to register this msg in AutoIt3 you simply include WindowsConstants.au3 and call GUIRegisterMsg() with $WM_PAINT as a parameter.

In the function that is called you simply reuse the code you used to draw the stuff in the first place and that's pretty much it. So if you drawed your png image with _GDIPlus_GraphicsDrawImageRect($Graphics,$Image,10,10,100,100), you just copy & paste that line into the redrawing function.

Now it's your turn to do some work, I've just done my best to explain how to use multiple png images on multiple child GUI's, go write some code that uses this method, you have Andreik's post as a backup when you don't understand something, and if you get stuck with anything else just post the code your not getting to work and I will be more than happy to help you fix it.

Good luck!

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

  • 3 weeks later...

Long time no see. Anyway I've been working and my code structure as far as GDI+ goes I think is fine but when i click the button to create a png image it doesnt appear?

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

Global $ahInput[2][5];Changed 4 to 5
Global $GUI, $hImage, $hGraphic, $hImage1

$GUI = GUICreate("Dragable Controls", 300, 300)

$Create_DIP_Button = GUICtrlCreatePic(@SystemDir & "\oobe\images\merlin.gif", 32, 58, 26, 26)

_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\background.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($ahInput[$ahInput[0][0]][1])

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")

GUISetState()

While 1
    $nMsg = GUIGetMsg(1)
   
    Switch $nMsg[0]
        Case $GUI_Event_Close
            Exit
        Case $Create_DIP_Button
            CreateDragIP()
        Case $ahInput[1][1] To $ahInput[$ahInput[0][0]][1];$GUI_EVENT_PRIMARYDOWN
            If $nMsg[0] = 0 Then ContinueLoop
           
            Local $aMouse_Pos = MouseGetPos()
            Local $aCursorInfo = GUIGetCursorInfo($nMsg[1]), $aCurrent_Mouse_Pos, $aInputGUI_Pos
           
            $aInputGUI_Pos = WinGetPos($nMsg[1])
            If Not IsArray($aInputGUI_Pos) Then ContinueLoop
           
            While IsArray($aCursorInfo) And $aCursorInfo[2] = 1
                $aCursorInfo = GUIGetCursorInfo($nMsg[1])
                $aCurrent_Mouse_Pos = MouseGetPos()
               
                WinMove($nMsg[1], "", _
                    $aInputGUI_Pos[0] - $aMouse_Pos[0] + $aCurrent_Mouse_Pos[0], _
                    $aInputGUI_Pos[1] - $aMouse_Pos[1] + $aCurrent_Mouse_Pos[1])
               
                Sleep(10)
            WEnd
    EndSwitch
   
   ;Changed the all loop (to work more effective with more than one message)
    For $i = 1 To $ahInput[0][0]
        Switch $nMsg[0]
            Case $ahInput[$i][3];This is the "Delete" item message
                DeleteDragIP($nMsg[0])
                ExitLoop
            Case $ahInput[$i][4];This is the "Delete All" item message
                DeleteAllDragIP()
                ExitLoop
        EndSwitch
    Next
WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_ShutDown()


; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($ahInput[$ahInput[0][0]][1], 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0,0)
    _WinAPI_RedrawWindow($ahInput[$ahInput[0][0]][1], 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc

Func CreateDragIP()
    Local $aMainGUI_Pos = WinGetPos($GUI)
   
    $ahInput[0][0] += 1
    ReDim $ahInput[$ahInput[0][0]+1][5];4 changed to 5
   
    $ahInput[$ahInput[0][0]][0] = GUICreate("", 26, 26, $aMainGUI_Pos[0] + 65, $aMainGUI_Pos[1] + 60 + 25, _
        $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_LAYERED))
   
    $ahInput[$ahInput[0][0]][1] = GUICtrlCreatePic($hImage, 0, 0, 26, 26, -1)
   
    $ahInput[$ahInput[0][0]][2] = GUICtrlCreateContextMenu($ahInput[$ahInput[0][0]][1])
    $ahInput[$ahInput[0][0]][3] = GUICtrlCreateMenuItem("&Delete", $ahInput[$ahInput[0][0]][2])
    $ahInput[$ahInput[0][0]][4] = GUICtrlCreateMenuItem("&Delete All", $ahInput[$ahInput[0][0]][2]);Added
   
    GUISetState(@SW_SHOW, $ahInput[$ahInput[0][0]][0])
   
    WinSetOnTop($ahInput[$ahInput[0][0]][0], "", 1)
EndFunc

Func DeleteDragIP($iCtrlID)
    Local $aTmpArr[2][5];Changed 4 to 5
   
    For $i = 1 To $ahInput[0][0]
        If $ahInput[$i][3] = $iCtrlID Then
            GUIDelete($ahInput[$i][0])
        Else
            $aTmpArr[0][0] += 1
            ReDim $aTmpArr[$aTmpArr[0][0]+1][5];Changed 4 to 5
           
            $aTmpArr[$aTmpArr[0][0]][0] = $ahInput[$i][0]
            $aTmpArr[$aTmpArr[0][0]][1] = $ahInput[$i][1]
            $aTmpArr[$aTmpArr[0][0]][2] = $ahInput[$i][2]
            $aTmpArr[$aTmpArr[0][0]][3] = $ahInput[$i][3]
            $aTmpArr[$aTmpArr[0][0]][4] = $ahInput[$i][4];This line was added
        EndIf
    Next
   
    $ahInput = $aTmpArr
EndFunc

;Added new function
Func DeleteAllDragIP()
    For $i = 1 To $ahInput[0][0]
        GUIDelete($ahInput[$i][0])
    Next
   
    $ahInput = 0
    Dim $ahInput[2][5]
EndFunc
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...