Jump to content

form with image background with several .png image buttons


Go to solution Solved by Andreik,

Recommended Posts

Hi my friend

How can I make a form that has an image background with several .png image buttons
When you mouse over them they enlarge for a second

I need to create a form like the attached image.
Each green icon will be a .png transparent button

 

2024-07-05_12-40-15.jpg

Link to comment
Share on other sites

3 hours ago, Jos said:

What exactly is that image from?

Is a Tour Virtual of any construction.

I need to create an background image and a lot of button like .png images

Link to comment
Share on other sites

  • Solution
Posted (edited)

Not really a button but something that might work well in your case.

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>

Global $Info, $ButtonHover = False

_GDIPlus_Startup()

Local $hMain = GUICreate("Image", 400, 200)
Local $cBackground = GUICtrlCreatePic(@ScriptDir & '\background.jpg', 0, 0, 400, 200)   ; Background image
GUICtrlSetState($cBackground, $GUI_DISABLE)
Local $cButton = GUICtrlCreatePic('', 50, 50, 50, 50)
ImageToCtrl(@ScriptDir & '\button.png', $cButton)
GUISetState(@SW_SHOW, $hMain)

While True
    Switch GUIGetMsg()
        Case $cButton
            MsgBox(0, '', 'You clicked me?')
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
    $Info = GUIGetCursorInfo($hMain)
    If Not IsArray($Info) Then ContinueLoop
    If $Info[4] = $cButton Then
        If Not $ButtonHover Then
            $ButtonHover = True
;~          You can upscale control here
            ImageToCtrl(@ScriptDir & '\hover.png', $cButton)    ; Hovered button image
        EndIf
    Else
        If $ButtonHover Then
            $ButtonHover = False
;~          You can downscale control here
            ImageToCtrl(@ScriptDir & '\button.png', $cButton)   ; Normal button image
        EndIf
    EndIf
WEnd

Func ImageToCtrl($sImage, $cCtrl)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sImage)
    Local $iWidth = _GDIPlus_ImageGetWidth($hImage)
    Local $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $iWidth, $iHeight)
    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP))
    _WinAPI_DeleteObject($hHBITMAP)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)
EndFunc

If you want multiple "buttons" you can create a multidimensional array to store the control ID and current hover state for each control.

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

1 hour ago, Andreik said:

Not really a button but something that might work well in your case.

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>

Global $Info, $ButtonHover = False

_GDIPlus_Startup()

Local $hMain = GUICreate("Image", 400, 200)
Local $cBackground = GUICtrlCreatePic(@ScriptDir & '\background.jpg', 0, 0, 400, 200)   ; Background image
GUICtrlSetState($cBackground, $GUI_DISABLE)
Local $cButton = GUICtrlCreatePic('', 50, 50, 50, 50)
ImageToCtrl(@ScriptDir & '\button.png', $cButton)
GUISetState(@SW_SHOW, $hMain)

While True
    Switch GUIGetMsg()
        Case $cButton
            MsgBox(0, '', 'You clicked me?')
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
    $Info = GUIGetCursorInfo($hMain)
    If Not IsArray($Info) Then ContinueLoop
    If $Info[4] = $cButton Then
        If Not $ButtonHover Then
            $ButtonHover = True
;~          You can upscale control here
            ImageToCtrl(@ScriptDir & '\hover.png', $cButton)    ; Hovered button image
        EndIf
    Else
        If $ButtonHover Then
            $ButtonHover = False
;~          You can downscale control here
            ImageToCtrl(@ScriptDir & '\button.png', $cButton)   ; Normal button image
        EndIf
    EndIf
WEnd

Func ImageToCtrl($sImage, $cCtrl)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sImage)
    Local $iWidth = _GDIPlus_ImageGetWidth($hImage)
    Local $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $iWidth, $iHeight)
    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP))
    _WinAPI_DeleteObject($hHBITMAP)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)
EndFunc

If you want multiple "buttons" you can create a multidimensional array to store the control ID and current hover state for each control.

WOW, fantastic and you did it on the first try.

Thanks a lot

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...