Jump to content

Help with transparent pic


mage123
 Share

Recommended Posts

I have a gui that uses gdi+ with a transparent png in it. I also made a blank pic control behind the png so that I could use GUIGetCursorInfo to see if you are hovering over it, and if so draw a different image on top (hover over image). It works fine, except that the png isn't square, so it displays the hover over image even when you're not hovering over it. Also, I use GUICtrlSetCursor to make a hand cursor show whenever you hover over it, and it has the same problem- displaying the hand even when you aren't hovering over the image. Anybody know how to fix this?

Link to comment
Share on other sites

I have a gui that uses gdi+ with a transparent png in it. I also made a blank pic control behind the png so that I could use GUIGetCursorInfo to see if you are hovering over it, and if so draw a different image on top (hover over image). It works fine, except that the png isn't square, so it displays the hover over image even when you're not hovering over it. Also, I use GUICtrlSetCursor to make a hand cursor show whenever you hover over it, and it has the same problem- displaying the hand even when you aren't hovering over the image. Anybody know how to fix this?

Malkey has some functions in example scripts that allow you to decide if a point, the cursor in your case, is inside a shape. So instaed of deciding if you are over the image you could decide if you were inside a certain area of the image.

You'll have to search for the functions because I don't have a link.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Link to comment
Share on other sites

#Include <GDIPlus.au3>
#Include <WinAPIEx.au3>
#Include <WindowsConstants.au3>

_GDIPlus_Startup()

$hBackground = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\bg_440x440.png')
$hReg = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\Red.png')
$hYellow = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\Yellow.png')

$State = Default

$hForm = GUICreate('', 440, 440, -1, -1, $WS_POPUPWINDOW, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
_Update(0)
GUISetState()


While 1
    $Info = GUIGetCursorInfo($hForm)
    If @error Then
        _Update(0)
    Else
        If ($Info[0] >=120) And ($Info[0] <=320) And ($Info[1] >=120) And ($Info[1] <=320) And (BitAND(_GDIPlus_BitmapGetPixel($hReg, $Info[0] - 120, $Info[1] - 120), 0xFF000000) = 0xFF000000) Then
            _Update(1)
            If $Info[2] Then
                ExitLoop
            EndIf
        Else
            _Update(0)
        EndIf
    EndIf
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            ExitLoop
    EndSwitch
WEnd

_GDIPlus_Shutdown()

Func _Update($iState)

    If $iState = $State Then
        Return
    EndIf

    Local $hGraphic, $hImage

    $hImage = _GDIPlus_ImageClone($hBackground)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    Switch $iState
        Case 0
            _GDIPlus_GraphicsDrawImageRect($hGraphic, $hYellow, 120, 120, 200, 200)
        Case 1
            _GDIPlus_GraphicsDrawImageRect($hGraphic, $hReg, 120, 120, 200, 200)
    EndSwitch
    _WinAPI_UpdateLayeredWindowEx($hForm, _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage), 255, 1)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    $State = $iState
EndFunc   ;==>_Update

Func _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)

    Local $aResult = DllCall($ghGDIPDll, 'uint', 'GdipBitmapGetPixel', 'ptr', $hBitmap, 'int', $iX, 'int', $iY, 'ptr*', 0)

    If @error Then
        Return SetError(1, 0, 0)
    Else
        If $aResult[0] Then
            Return SetError($aResult[0], 0, 0)
        EndIf
    EndIf
    Return $aResult[4]
EndFunc   ;==>_GDIPlus_BitmapGetPixel

Func _GDIPlus_ImageClone($hImage)

    Local $aResult = DllCall($ghGDIPDll, 'uint', 'GdipCloneImage', 'ptr', $hImage, 'ptr*', 0)

    If @error Then
        Return SetError(1, 0, 0)
    Else
        If $aResult[0] Then
            Return SetError($aResult[0], 0, 0)
        EndIf
    EndIf
    Return $aResult[2]
EndFunc   ;==>_GDIPlus_ImageClone

post-42455-12863069481894_thumb.png post-42455-12863069543414_thumb.png post-42455-1286306959401_thumb.png

WinAPIEx.au3

Edited by Yashied
Link to comment
Share on other sites

Another possibility: instead checking for transparency you can calculate the distance from the middle of the buttom to its border.

In this case (round objects with same diameter) it is the radius. That means when the mouse distance to the middle of the buttom is less then

the radius then change the image.

Example (images used from pevious post by Yashied):

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()

Local $hBackground = _GDIPlus_ImageLoadFromFile("bg_440x440.png")
Local $Width = _GDIPlus_ImageGetWidth($hBackground)
Local $Height = _GDIPlus_ImageGetHeight($hBackground)

Local $center_w = $Width / 2
Local $center_h = $Height / 2

Local $hRed = _GDIPlus_ImageLoadFromFile("Red.png")
Local $hRed_w = _GDIPlus_ImageGetWidth($hRed)
Local $hRed_h = _GDIPlus_ImageGetHeight($hRed)

Local $hYellow = _GDIPlus_ImageLoadFromFile("Yellow.png")
Local $hYellow_w = _GDIPlus_ImageGetWidth($hYellow)
Local $hYellow_h = _GDIPlus_ImageGetHeight($hYellow)

Local $hGUI = GUICreate("GDI+ Mouse Hover by UEZ", $Width, $Height, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
GUISetState()

Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $hGraphics)
Local $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Local $mpos, $pd, $radius = ($hYellow_w - 22) / 2

While Sleep(30)
    _GDIPlus_GraphicsClear($hBackbuffer, 0x00000000)
    _GDIPlus_GraphicsDrawImageRect($hBackbuffer, $hBackground, 0, 0, $Width, $Height)

    $mpos = GUIGetCursorInfo($hGUI)
    $pd = Pixel_Distance($mpos[0], $mpos[1], $center_w, $center_h)
    If $mpos[2] And $pd < $radius Then _Exit()
    If $pd < $radius Then
        _GDIPlus_GraphicsDrawImage($hBackbuffer, $hRed, $Width / 2 - $hRed_w / 1.5, $Height / 2 - $hRed_w / 1.5)
    Else
        _GDIPlus_GraphicsDrawImage($hBackbuffer, $hYellow, $Width / 2 - $hYellow_w / 1.5, $Height / 2 - $hYellow_w / 1.5)
    EndIf

    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $Width, $Height)
    _WinAPI_UpdateLayeredWindowEx($hGUI, _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap), 255, 0)
WEnd

Func Pixel_Distance($x1, $y1, $x2, $y2) ;Pythagoras theorem
    Local $a, $b, $c
    If $x2 = $x1 And $y2 = $y1 Then Return 0
    $a = $y2 - $y1
    $b = $x2 - $x1
    $c = Sqrt($a * $a + $b * $b)
    Return $c
EndFunc   ;==>Pixel_Distance

Func _Exit()
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If ($hWnd = $hGui) And ($iMsg = $WM_NCHITTEST) Then Return $HTCAPTION
EndFunc ;==>WM_NCHITTEST

Thanks to Yasied for WinAPIEx.au3 and the images! ;)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • 5 months later...

I downloaded the images of the original code, however, the GUI is still not view.

I tried to remove $ WS_EX_LAYERED, the GUI is show, but not pictures.

The same thing happens with the GDI version of UEZ

It may be that I downloaded an older version of WinAPIEx?

Edited by bub
Link to comment
Share on other sites

I downloaded the images of the original code, however, the GUI is still not view.

I tried to remove $ WS_EX_LAYERED, the GUI is show, but not pictures.

The same thing happens with the GDI version of UEZ

It may be that I downloaded an older version of WinAPIEx?

For me it is still working with the latest WinAPIEx version - tested yesterday!

Probably the images cannot be loaded properly. Try @scriptdir & "\image.jpg"

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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