Jump to content

GUI, GDI+ and transparency


Recommended Posts

Hi Guys,

i have a little problem here. I'm trying with GDI and transparent png backgrounds for GUIs and it works :D but i want to draw a semitransparent png on the background, and this don't work.

I think it has to do with the color i set as transpareny color. Maybe u can give me a tip where i made the mistake...

Here is the Script

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

Global Const $AC_SRC_ALPHA = 1
If @DesktopHeight = 1080 Then
    $gPosX = (-1280 - 238) / 2
    $gPosY = (1024 - 471) / 2
Else
    $gPosX = -1
    $gPosY = -1
EndIf

_GDIPlus_Startup()
$bg = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\bg.png")
$icon_a = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\icon_1a.png")
$icon_b = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\icon_1b.png")

GUICtrlCreateButton("TEST", 10, 10, 50, 20)
$bgGUI = GUICreate("SysControl", 325, 425, -1, -1, $WS_POPUP, $WS_EX_LAYERED)
SetBitMap($bgGUI, $bg, 0)
GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST")
GUISetState()
WinSetOnTop($bgGUI, "", 1)

For $i = 5 To 255 Step 10
    SetBitmap($bgGUI, $bg, $i)
    Sleep(1)
Next

$realGUI = GUICreate("CONTROL_GUI", 325, 425, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $bgGUI)

GUICtrlCreatePic(@ScriptDir & "\trans1.gif", 0, 0, 325, 425)
GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState()
$ZEROGraphic = _GDIPlus_GraphicsCreateFromHWND($realGUI)
_GDIPlus_GraphicsDrawImageRectRect($ZEROGraphic, $icon_a, 0, 0, 256, 256, 15, 10, 128, 128)
_GDIPlus_GraphicsDrawImageRectRect($ZEROGraphic, $icon_b, 0, 0, 256, 256, 143, 10, 128, 128)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd


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

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", $AC_SRC_ALPHA)
    _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

And here is a picture of my problem. I want the firefox logo also transparent, but i won't work :D

post-38066-1244494902_thumb.jpg

Here the source code and graphics:

Link to comment
Share on other sites

The pink bits are not all the same colour so that is the first problem I think. Then when you change the opacity you are also changing the colour as far as transparency is concerned perhaps.

There was a similar requirement here which was solved using a different approach which might do what you want, but first you would have to get all those pink bits the same colour.

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

Thanks for your reply, but i'm not really good in this graphics stuff, so i think it would be difficult for me to remove every pink bit/pixel... or is this easy?

It's quite easy. You don't need to remove every pixel but just make them the same. It could be done with mspaint.

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

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