Jump to content
Sign in to follow this  
InunoTaishou

Valentines Hearts

Recommended Posts

InunoTaishou

Credit's go to @UEZ for his post from 5 years ago for the GDI+ Heart!

Really, I was just bored... I didn't have a valentine (:'( )so I whipped this up in a couple of hours. Pretty sure I cleaned up all my resources properly but it's late and I'm tired.

$HAPPY_VALENTINES is the string that is shown in the middle of the screen

$MAX_HEART_COUNT controls how many hearts are displayed

When you first run it it will display a tooltip with the % of the string drawn (since it needs to drawn line by line). The larger the heart, the longer it'll take to draw it. Once it's been drawn once it's saved (@TempDir & "\heart.png") and will be reloaded on each startup. It will check to make sure that the heart saved is the size in the current script. If the sizes don't match it deletes the old saved heart and redraws the correct size. (I.e., if the heart saved is 350px wide and the $MAX_HEART_SIZE is 250, it deletes the old heart.png and remakes it the correct size)

; Coded by InunoTaishou
; Credits to UEZ for the code to draw a heart

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#pragma compile(Icon, "heart.ico")

Opt("MustDeclareVars", 1)
Opt("GUICloseOnEsc", 1)
Opt("TrayIconHide", 1)

_GDIPlus_Startup()

Global Const $HAPPY_VALENTINES = "Happy Valentine's Day AutoIT"
; maximum hearts to be drawn
Global Const $MAX_HEART_COUNT = 50
; bounding rectangle that encloses the above string, width and height
Global Const $VALENTINE_STRING_WIDTH = 400
Global Const $VALENTINE_STRING_HEIGHT = 150
Global Const $MIN_HEART_SIZE = 50
Global Const $MAX_HEART_SIZE = 300
Global Const $RECT_HAPPY_VALENTINES[4] = [(@DesktopWidth / 2) - ($VALENTINE_STRING_WIDTH / 2), (@DesktopHeight / 2) - ($VALENTINE_STRING_HEIGHT / 2), $VALENTINE_STRING_WIDTH, $VALENTINE_STRING_HEIGHT] ; x, y, width, height
Global Const $MIN_TIMEOUT = 3000
Global Const $MAX_TIMEOUT = 7000
Global Const $MIN_VERTICAL_SPEED = 2
Global Const $MAX_VERTICAL_SPEED = 5
Global Const $MIN_HORIZONTAL_OFFSET = 100
Global Const $MAX_HORIZONTAL_OFFSET = 175
Global Const $PNG_HEART_PATH = @TempDir & "\heart.png"
Global Const $HWND_PEN = _GDIPlus_PenCreate(0, 1)
Global Const $HWND_FONT_FAMILY = _GDIPlus_FontFamilyCreate("Segoe UI")
Global Const $HWND_FONT_VALENTINE = _GDIPlus_FontCreate($HWND_FONT_FAMILY, 32)
Global Const $RECT_VALENTINE = _GDIPlus_RectFCreate($RECT_HAPPY_VALENTINES[0], $RECT_HAPPY_VALENTINES[1], $RECT_HAPPY_VALENTINES[2], $RECT_HAPPY_VALENTINES[3])
Global Const $RECT_FPS = _GDIPlus_RectFCreate(0, 0, 50, 16)
Global Const $HWND_FONT_FPS = _GDIPlus_FontCreate($HWND_FONT_FAMILY, 8.5)
Global Const $HWND_STRING_FORMAT_VALENTINE = _GDIPlus_StringFormatCreate()
Global Const $HWND_STRING_FORMAT_FPS = _GDIPlus_StringFormatCreate()
Global Const $HWND_VALENTINE_TEXT_BRUSH = _GDIPlus_BrushCreateSolid(0xFFFF0000)
Global Const $HWND_BK_BRUSH = _GDIPlus_BrushCreateSolid(0xFF1A1A1A)
Global Const $HWND_FPS_BRUSH = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
Global Const $pi = ACos(-1)
Global Const $dy = 30
Global $dist, $p, $l, $r, $col
Global $heart_buffer[$MAX_HEART_COUNT][12]
Global $frmMain = GUICreate("<3", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
Global $fps_counter = 0
Global $fps = 0
Global $hWnd_graphic = _GDIPlus_GraphicsCreateFromHWND($frmMain)
Global $hWnd_bitmap = _GDIPlus_BitmapCreateFromGraphics(@DesktopWidth, @DesktopHeight, $hWnd_graphic)
Global $hWnd_backbuffer = _GDIPlus_ImageGetGraphicsContext($hWnd_bitmap)

GDIPValentinesDay()

Func GDIPValentinesDay()
    Local Const $hWnd_heart_bitmap = CreateHeartBitmap($MAX_HEART_SIZE, ($MAX_HEART_SIZE - 1), ($MAX_HEART_SIZE / 2))

    ; center text
    _GDIPlus_StringFormatSetAlign($HWND_STRING_FORMAT_VALENTINE, 1)
    _GDIPlus_StringFormatSetLineAlign($HWND_STRING_FORMAT_VALENTINE, 1)
    ; set smoothing
    _GDIPlus_GraphicsSetSmoothingMode($hWnd_backbuffer, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetSmoothingMode($hWnd_graphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY)

    GUISetBkColor(0x000000, $frmMain)
    _WinAPI_SetLayeredWindowAttributes($frmMain, 0x000000, 255)

    GUISetState(@SW_SHOW, $frmMain)

    SetHeartsMap()

    AdlibRegister("CalculateFps", 1000)

    Do
        _GDIPlus_GraphicsClear($hWnd_backbuffer, 0xFF000000)

        For $i = 0 To $MAX_HEART_COUNT - 1
            ; if there is a timer
            If ($heart_buffer[$i][4] <> -1) Then
                ; if the timer has reached the timeout
                If (TimerDiff($heart_buffer[$i][5]) > $heart_buffer[$i][4]) Then
                    ; shrink the heart
                    $heart_buffer[$i][2] -= 1
                    $heart_buffer[$i][3] -= 1
                EndIf
            EndIf

            ; if the heart has a horizontal move
            If ($heart_buffer[$i][7]) Then
                ; if the number of pixels moved in the x position is greater than the max pixels to move horizontally
                If (Abs($heart_buffer[$i][10]) > Abs($heart_buffer[$i][9])) Then
                    If ($heart_buffer[$i][11] > 35) Then
                        ; change direction
                        $heart_buffer[$i][8] *= -1
                        ; reset the jerk counter
                        $heart_buffer[$i][11] = 0
                    Else
                        $heart_buffer[$i][11] += 1
                    EndIf
                EndIf

                ; adjust the x offset
                $heart_buffer[$i][0] += $heart_buffer[$i][8]
                ; adjust how many pixels the heart has moved in that direction
                $heart_buffer[$i][10] += $heart_buffer[$i][8]
            EndIf

            ; if the y position of the heart is <= 0 or the heart width <= 2, reset that heart
            If ($heart_buffer[$i][1] <= ($heart_buffer[$i][2] * -1) Or $heart_buffer[$i][2] <= 2) Then SetHeartStats($i)

            _GDIPlus_GraphicsDrawImageRect($hWnd_backbuffer, $hWnd_heart_bitmap, $heart_buffer[$i][0], $heart_buffer[$i][1], $heart_buffer[$i][2], $heart_buffer[$i][3])

            ; adjust y coordinate of heart
            $heart_buffer[$i][1] -= $heart_buffer[$i][6]
        Next

        _GDIPlus_GraphicsFillRect($hWnd_backbuffer, $RECT_HAPPY_VALENTINES[0], $RECT_HAPPY_VALENTINES[1], $RECT_HAPPY_VALENTINES[2], $RECT_HAPPY_VALENTINES[3], $HWND_BK_BRUSH)
        _GDIPlus_GraphicsDrawStringEx($hWnd_backbuffer, $HAPPY_VALENTINES, $HWND_FONT_VALENTINE, $RECT_VALENTINE, $HWND_STRING_FORMAT_VALENTINE, $HWND_VALENTINE_TEXT_BRUSH)

        _GDIPlus_GraphicsFillRect($hWnd_backbuffer, 0, 0, 50, 16, $HWND_BK_BRUSH)
        _GDIPlus_GraphicsDrawStringEx($hWnd_backbuffer, "FPS: " & $fps_counter, $HWND_FONT_FPS, $RECT_FPS, $HWND_STRING_FORMAT_FPS, $HWND_FPS_BRUSH)

        _GDIPlus_GraphicsDrawImage($hWnd_graphic, $hWnd_bitmap, 0, 0)

        $fps += 1

        Sleep(10)
    Until (GUIGetMsg() = $GUI_EVENT_CLOSE)

    AdlibUnRegister("CalculateFps")

    _GDIPlus_PenDispose($HWND_PEN)
    _GDIPlus_GraphicsDispose($hWnd_graphic)
    _GDIPlus_BitmapDispose($hWnd_bitmap)
    _GDIPlus_BitmapDispose($hWnd_heart_bitmap)
    _GDIPlus_FontFamilyDispose($HWND_FONT_FAMILY)
    _GDIPlus_FontDispose($HWND_FONT_VALENTINE)
    _GDIPlus_StringFormatDispose($HWND_STRING_FORMAT_VALENTINE)
    _GDIPlus_BrushDispose($HWND_VALENTINE_TEXT_BRUSH)
    _GDIPlus_BrushDispose($HWND_BK_BRUSH)
    _GDIPlus_FontDispose($HWND_FONT_FPS)
    _GDIPlus_StringFormatDispose($HWND_STRING_FORMAT_FPS)
    _GDIPlus_BrushDispose($HWND_FPS_BRUSH)
    _GDIPlus_Shutdown()

    GUIDelete($frmMain)
    Return 0
EndFunc   ;==>GDIPValentinesDay

Func CalculateFps()
    $fps_counter = $fps
    $fps = 0
EndFunc   ;==>CalculateFps

Func SetHeartsMap()
    For $i = 0 To $MAX_HEART_COUNT - 1
        SetHeartStats($i)
    Next
EndFunc   ;==>SetHeartsMap

Func SetHeartStats(Const ByRef $iIndex)
    ; determine horizontal direction to move
    Local Const $multiply_by = (Random(0, 1, 1) ? 1 : -1)
    Local $valid_position = False
    Local $y_offset = 0

    ; width of heart
    $heart_buffer[$iIndex][2] = Ceiling(Random($MIN_HEART_SIZE, $MAX_HEART_SIZE, 1) / 10) * 10
    ; height of heart
    $heart_buffer[$iIndex][3] = $heart_buffer[$iIndex][2] - 1

    Do
        ; x position of heart
        $heart_buffer[$iIndex][0] = Random(0, @DesktopWidth, 1)
        ; y position of heart
        $heart_buffer[$iIndex][1] = Random(@DesktopHeight - $MIN_HEART_SIZE - $y_offset, @DesktopHeight, 1)

        For $i = 0 To $MAX_HEART_COUNT - 1
            If ($i = $iIndex) Then ContinueLoop

            ; make sure the heart doesn't start in the same spot as another heart
            If (_WinAPI_PtInRectEx($heart_buffer[$iIndex][0], $heart_buffer[$iIndex][1], $heart_buffer[$i][0], $heart_buffer[$i][1], $heart_buffer[$i][0] + $heart_buffer[$i][2], $heart_buffer[$i][1] + $heart_buffer[$i][3])) Then
                $valid_position = False
                $y_offset += 5
                ExitLoop
            Else
                $valid_position = True
            EndIf
        Next
    Until ($valid_position)

    If (Random(0, 3, 1) = False) Then
        ; add timeout for some hearts
        $heart_buffer[$iIndex][4] = Random($MIN_TIMEOUT, $MAX_TIMEOUT, 1)
        ; init timer for that heart
        $heart_buffer[$iIndex][5] = TimerInit()
    Else
        $heart_buffer[$iIndex][4] = -1
        $heart_buffer[$iIndex][5] = 0
    EndIf

    ; set vertical move speed
    $heart_buffer[$iIndex][6] = Random($MIN_VERTICAL_SPEED, $MAX_VERTICAL_SPEED, 1)

    If (Random(0, 4, 1)) Then
        ; horizontal moving is true
        $heart_buffer[$iIndex][7] = True
        ; set horizontal move speed
        $heart_buffer[$iIndex][8] = Random($MIN_VERTICAL_SPEED / 2, $MAX_VERTICAL_SPEED / 2, 1) * $multiply_by
        ; set the max pixels to move horizontally
        $heart_buffer[$iIndex][9] = Random($MIN_HORIZONTAL_OFFSET, $MAX_HORIZONTAL_OFFSET, 1) * $multiply_by
        ; set the pixels the heart has moved in this direction
        $heart_buffer[$iIndex][10] = 0
        ; set the counter to 0, this controls how long to wait before adjusting direction once it's reached the max horizontal offset. (makes the heart not jerk left/right)
        $heart_buffer[$iIndex][11] = 0
    Else
        ; horizontal moving is false
        $heart_buffer[$iIndex][7] = False
        $heart_buffer[$iIndex][8] = 0
        $heart_buffer[$iIndex][9] = 0
        $heart_buffer[$iIndex][10] = 0
        $heart_buffer[$iIndex][11] = 0
    EndIf
EndFunc   ;==>SetHeartStats

Func CreateHeartBitmap(Const ByRef $iW, Const ByRef $iH, Const ByRef $iRadius)
    Local $hWnd_heart_graphic = _GDIPlus_GraphicsCreateFromHWND($frmMain)
    Local $hWnd_heart_bitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hWnd_heart_graphic)
    Local $hWnd_heart_buffer = _GDIPlus_ImageGetGraphicsContext($hWnd_heart_bitmap)
    Local $percent = 0

    If (Not FileExists(@TempDir & "\heart.png")) Then
        ToolTip("Drawing heart, please wait ... 0% complete")
        _GDIPlus_GraphicsSetSmoothingMode($hWnd_heart_buffer, 2)
        _GDIPlus_GraphicsClear($hWnd_heart_buffer, 0x00000000)

        For $y = 1 To $iH
            For $x = 1 To $iW
                $dist = Sqrt((($iW / 2) - $x) ^ 2 + (($iH / 2) - $y) ^ 2)
                $p = ATan2(($iW / 2) - $x, ($iH / 2) - $y)
                $l = Abs($p) / $pi
                $r = (13 * $l - 22 * $l ^ 2 + 10 * $l ^ 3) / (6 - 5 * $l) * $iRadius
                If $dist <= $r Then
                    $col = "0xFFFF0000"
                Else
                    $col = "0x00000000"
                EndIf
                _GDIPlus_PenSetColor($HWND_PEN, $col)
                _GDIPlus_GraphicsDrawRect($hWnd_heart_buffer, $x, $y - $dy, 1, 1, $HWND_PEN)
            Next
            $percent = Round((($y * $iW) / ($iH * $iW)) * 100, 0)
            ToolTip("Drawing heart, please wait ..." & @CRLF & @TAB & @TAB & $percent & "% complete" & @CRLF & "[" & StringPercent($percent) & "]")
        Next
        ToolTip("")
        _GDIPlus_ImageSaveToFile($hWnd_heart_bitmap, $PNG_HEART_PATH)
    Else
        $hWnd_heart_bitmap = _GDIPlus_ImageLoadFromFile($PNG_HEART_PATH)

        ; if the saved image is not the proper size, remake it
        If (_GDIPlus_ImageGetWidth($hWnd_heart_bitmap) <> $MAX_HEART_SIZE) Then
            _GDIPlus_BitmapDispose($hWnd_heart_bitmap)
            _GDIPlus_GraphicsDispose($hWnd_heart_graphic)
            Do
                FileDelete($PNG_HEART_PATH)
            Until (Not FileExists($PNG_HEART_PATH))
            Return CreateHeartBitmap($iW, $iH, $iRadius)
        EndIf
    EndIf

    _GDIPlus_GraphicsDispose($hWnd_heart_graphic)

    Return $hWnd_heart_bitmap
EndFunc   ;==>CreateHeartBitmap

Func StringPercent(Const ByRef $iPercent)
    Local $string_return = ""
    For $i = 0 To $iPercent
        $string_return &= "|"
    Next
    For $i = $iPercent To 100
        $string_return &= " "
    Next
    Return $string_return
EndFunc   ;==>StringPercent

Func ATan2($y, $x)
    Return (2 * ATan($y / ($x + Sqrt($x * $x + $y * $y))))
EndFunc   ;==>ATan2

Most of it should be pretty self explanatory. There's a lot of comments too.

And since I like to see pictures of the code people post, here's mine! (The script is animated, my picture is not ;))

X4WNabQ.png

 

Edit: Moved the Drawn string and rectangle outside the for loop for each heart. Increased FPS dramatically! Lol Also included the .ico and .exe

Valentines Hearts.au3

Valentines Hearts.exe

heart.ico

Edited by InunoTaishou

Share this post


Link to post
Share on other sites
InunoTaishou

Edited post, moved the drawn string and rectangle outside my big for loop. That increased fps dramatically. Also included .ico and .exe in original post.

Edited by InunoTaishou

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • UEZ
      By UEZ
      AutoIt Windows Screenshooter
      Key Features:
      takes easily a screenshot from any visible window capture any region of the desktop incl. freehand capturing capture GUI controls and GUI menus separately capture a marked area every x seconds for a duration of y seconds create a GIF animation from saved frames (Vista or higher os required) capture to AVI file (without audio!) takes a screenshot from web sites (available only on Win7+ os and when Aero is enabled) put images to clipboard to paste to other applications easily color picker save image in different formats and also to PDF! add timestamp to saved images simple image editing options: greyscale, b&w, invert, rotate +-90° send image to printer and default email client preview of captured screens incl. zoom option multi monitor support display pixel color under mouse ruler basic image editor (paint, highlight, ellipse, rectangle, text and some graphic FX) watermark captured image no 3rd party tools or DLLs used - pure AutoIt! fully portable - no installation is needed multi language feature (Eng, Ger, Tur, Fra and Rus only) To do:
      capture content of scrollable window/control capture cascaded menus Due to DllCall("User32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0) limitation some windows cannot be captured properly (GDI+, ProgDVB, etc.) but can take screenshots of hidden windows. One workaround is to use full screen capturing (F11/F12) or "Grab Screen" function! Or try double click with rmb on listview items (beta).
      Download source code (6006 downloads previously): AutoIt Windows Screenshooter v1.80 Build 2018-07-20.7z (version 3.3.12.0+ needed!)
      You are not allowed to sell this code or just parts of it in a commercial project or modify it and distribute it with a different name!
      Download compiled Exe only: 4shared / Media Fire / Softpedia (1.54mb)
      Distributing copies of the program in compiled format (exe) must be free of any fee!
      -----> click here to Donate!  
       
      (Current donators: 1. Cuong N.) 
      It is designed for Win7+ operating systems with AERO enabled! E.g. on WinXP machines some functions are not working properly and might crash the application!
      AV scanners may have a negative impact the execution of compiled exe and might report any malware. I guarantee that there is no malicious code in the source code / exe!!! 
      Main GUI:

       
       
      About Intro:

       
       
      Basic Image Editor:

       
       
      Watermark:

       
       
      Click link for an enhanced version of Watermark Image.
      Credits:
      main code by UEZ additional code (alphabetical order) by Authenticity, AutoItObject Team, Eemuli, Eukalyptus, funkey, _Kurt, martin, monoceres, ProgAndy, taietel, trancexx, Ward, wolf9228 and Yashied! mesale0077 for turkish translation wakillon for french translation AZJIO for russian translation Keys:
      Main GUI:
      User your mouse to scroll preview window or
      Numpad 8: Scroll preview window up
      Numpad 2: Scroll preview window down
      Numpad 4: Scroll preview window left
      Numpad 6: Scroll preview window right
      Numpad +: zoom in preview window or mousewheel down
      Numpad -: zoom out preview window or mousewheel up
      F1: capture again on last position
      F5: refresh Windows Name list
      PRINTSCREEN: take screenshot from whole screen
      ALT+PRINTSCR: take a screenshot from active window
      F10: Undo made changes with Image Editing function
      F11: take screenshot from whole screen incl mouse cursor
      F12: take screenshot from whole screen
      Ctrl+Alt+F9 start "Grab Screen" mode
      Ctrl+Alt+F12: take a screenshot from active window using alternative screenshot functionality (beta)!
      Ctrl+r: call ruler
      Ctrl+s: save current displayed image
      Ctrl+x: exit program
      ctrl+w: call web grab input field (availabe only when Aero is enabled)
      Ctrl+i: call image editor
      Ctrl+m: call watermark editor
      Ctrl+z: undo
      Only available on Vista+ os: double click with rmb on list items to use alternative screenshot functionality (beta)!
      When 'Grab Screen' is clicked you can hold down the ctrl key to switch to 'grab controls' mode. Control under mouse will be framed red.
      ctrl + shift will take the screenshot of appropriate control. To capture GUI menues you can press rmb which simulates the lmb. When a menu is opened press shift additionaly to capture it.
      Press and hold only the shift key to capture any region on the desktop using freehand capturing - release it so capture marked regions!
      Or just mark resizeable area which you want to grab. Press CTRL key to grab marked area or right mouse button to capture the marked area every x seconds for a duration of y seconds.
      When saving the image just enter the extension you whish to use (*.jpg;*.png;*.bmp;*.gif;*.tif;*.pdf). Big thanks to taietel for his PDF UDF!
      Image Editor:
      s: save
      c: copy
      n: send
      h: highlighter
      p: pen
      r: rectangle
      e: ellipse
      a: arrow
      o: color
      t: text
      g: text config
      Ctrl+z: undo
      Watermark editor:
      Ctrl+z: undo
       
      To start the app minimized just call it "Windows Screenshooter.exe /min"
      Maybe it is useful for someone...
      Any kind of comment is welcome.
      Br,
      UEZ
      Change log:
       
    • Sven-Seyfert
      By Sven-Seyfert
      Hi Community,

      I'm looking for a way to do a Video Overlay GUI or something like that. The idea is to create a GUI which plays a video loop (with transparency/alpha channel) in front of an other GUI. Before you asking why - because I don't believe that GDIPlus can do it out of the box. My skillset for that kind of graphical things isn't good enough to do that, but here are some specialist like @UEZ maybe who can help.

      Example alpha channel video (visualized as animated *.gif):

      I tried to do the light rays effect directly with GDIPlus, but honestly that's a bit too difficult for me. I would be very glad and grateful if there are some suggestions, ideas or recommendations.

      Code for the Video play:
       
      Example video "End.mpeg":
       
      The next challenge is that the overlay GUI should be not clickable. If I hover over the overlay area, I want to have the possibility to control the GUI or what ever, in the background. But if there is any chance to make it with GDIPlus as a Video Overlay for light rays, I would prefer that approach instead of my crazy work-around idea.

      Thanks for any suggestion - I'm grateful!
      Sven
    • aiter
      By aiter
      I am trying to get an image showing through a edit box.  I am only successful in making the edit box totally transparent
      #include-once #include <GUIConstants.au3> #include <GDIPlus.au3> #include <WinAPISys.au3> #include <colorconstants.au3> ;WS_EX_TRANSPARENT $gui = GUICreate("", 1000, 800, -1, -1, -1 , $WS_EX_LAYERED) ; use layered to get _winapi_setlay... to work $pic = GUICtrlCreatePic("c:\Program Files (x86)\AutoIt3\Examples\GUI\Merlin.gif", 0,0,1000, 800) GUICtrlSetState(-1, $GUI_DISABLE) $edit = GUICtrlCreateEdit("First line" & @CRLF, 176, 32,200,600) GUICtrlSetBkColor(-1,$COLOR_YELLOW) _WinAPI_SetLayeredWindowAttributes($gui,$COLOR_YELLOW,199) ; 199 is alpha (transparency level) GUISetState(@SW_SHOW,$gui) While 1 $msg = GuiGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case Else EndSelect WEnd Exit Func Terminate() exit(0) EndFunc So I am making the edit box's background yellow then using the _WINAPI_SetLayeredWIndowAttributes command to make the yellow disappear (which it does), but the alpha level is supposed to give a bit of opaqueness to it, but its not, just making it totally transparent.  The alpha level is in fact affecting the window itself and not the edit box. I only want the edit box to be partially transparent.
      Help appreciated.
       
    • ISI360
      By ISI360
      Hi!
      I am searching for a way to display a transparent image (or Icon) over an Button. And the image should always stay on top.

      Here is what i have so far:
      At script start the picture is over the button..but if you move the mouse over the button it overlaps the image.
      And if i add $GUI_ONTOP to the picture, the transparency is gone

      Maybe someone has an idea..

      Thanks in advance!
       
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WinAPIIcons.au3> #include <WinAPIShellEx.au3> GUICreate("GUI", 300, 300) $hbutton = GUICtrlCreateButton("This is a button", 20, 40, 150, 30,$WS_CLIPSIBLINGS) $hpic = GUICtrlCreatePic("", 10, 10, 128, 128) _SetIconAlpha($hpic,"shell32.dll", 12,128,128) GUICtrlSetState(-1, $GUI_DISABLE) ;~ GUICtrlSetState(-1,$GUI_ONTOP) ;If you uncomment this..the picture is on top...but the transparency is gone :( GUISetState() While 1 $Msg = GUIGetMsg() If $Msg = $GUI_EVENT_CLOSE Then ExitLoop WEnd Func _SetIconAlpha($hWnd, $sIcon, $iIndex, $iWidth, $iHeight) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) If $hWnd = 0 Then Return SetError(1, 0, 0) EndIf EndIf If $iIndex <> 0 Then $iIndex = $iIndex - 1 Local $hIcon = _WinAPI_ShellExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) If $hIcon = 0 Then Return SetError(1, 0, 0) EndIf Local $hBitmap, $hObj, $hDC, $hMem, $hSv $hDC = _WinAPI_GetDC($hWnd) $hMem = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight) $hSv = _WinAPI_SelectObject($hMem, $hBitmap) _WinAPI_DrawIconEx($hMem, 0, 0, $hIcon, $iWidth, $iHeight, 0, 0, 2) _WinAPI_ReleaseDC($hWnd, $hDC) _WinAPI_SelectObject($hMem, $hSv) _WinAPI_DeleteDC($hMem) _WinAPI_DestroyIcon($hIcon) _WinAPI_DeleteObject(_SendMessage($hWnd, 0x0172, 0, 0)) _SendMessage($hWnd, 0x0172, 0, $hBitmap) $hObj = _SendMessage($hWnd, 0x0173) If $hObj <> $hBitmap Then _WinAPI_DeleteObject($hBitmap) EndIf Return 1 EndFunc ;==>_SetIconAlpha  
    • c.haslam
      By c.haslam
      I think that _GDIPlus_ImageGetPropertyItem should return a Property Item that can readily be set as a property of another image. Why would one want to do this? A script might (as one of mine does) edit an image of a .jpg file then write the result to a .jpg file. In editing it, in my case, the number of horizontal and vertical pixels change, and the date/time edited should be set as a property. All other property items remain the same. Other GDI+ users may change other properties.
      So an approach is to copy all the property items from image1 to image2, then update a few properties.
      M$ provides a Property Item class (id, length, pointer to an array of values) but _GDIPlus_ImageGetPropertyItem returns something different.
      I think that, wherever reasonable, UDFs that are wrappers for M$ methods should work like M$'s methods. _GDIPlus_ImageGetPropertyItem does not do this:
      While M$ returns 2 values for a value that is a ratio of 2 numbers,  _GDIPlus_ImageGetPropertyItem returns numerator/denominator as a single value (often a Double). _GDIPlus_ImageSetPropertyItem (when included in GDIPlus.au3) will be unable to set ratio property items properly because it cannot know what the numerator and denominator are. So copying such property items will not work properly. M$ has a Void* buffer where the buffer is an array of values but _GDIPlus_ImageGetPropertyItem() returns the values in the same 1-d array as id, length and type. M$'s approach produces a 1-d array with the same number of elements for all property items while _GDIPlus_ImageGetPropertyItem produces a 1-d array with various numbers of elements. To me, this is not good programming practice. For a photo from a Sony camera, the worst case has 1-d array with 67 elements. When combined into a 2-d array with the property items of all properties, there are 66 rows and 67 columns, with many elements not used. So I suggest that _GDIPlus_ImageGetPropertyItem look like this:
      ; #FUNCTION# ==================================================================================================================== ; Name ..........: cGDIPlus_ImageGetPropertyItem ; Description ...: Gets a specified property item (piece of meta data) from an Image object ; Syntax ........: cGDIPlus_ImageGetPropertyItem($hImage, $iPropID) ; Parameters ....: $hImage - Pointer to an image object ; $iPropID - Identifier of the property item to be retrieved ; Return values .: Success: Array containing the values of the property item ; [0] - identifier ; [1] - size, in bytes, of the value array ; [2] - type of value(s) in the value array ; [3] - value array ; Failure: Sets the @error flag to non-zero, @extended may contain GPSTATUS error code ($GPID_ERR*). ; Author ........: Eukalyptus ; Modified ......: c.haslam ; Remarks .......: types: unsigned byte = 1, ASCII string = 2, unsigned short = 3, unsigned long = 4, ; unsinged rational = 5, undefined = 7, signed long = 9, signed rational = 10 ; Related .......: _GDIPlus_ImageGetPropertyIdList ; Link ..........: https://msdn.microsoft.com/en-us/library/windows/desktop/ms535390(v=vs.85).aspx, ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms534493(v=vs.85).aspx, ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms534414(v=vs.85).aspx ; Example .......: Yes ; =============================================================================================================================== Func cGDIPlus_ImageGetPropertyItem($hImage, $iPropID) Local $iSize = __GDIPlus_ImageGetPropertyItemSize($hImage, $iPropID) If @error Then Return SetError(@error, @extended, -1) Local $tBuffer = DllStructCreate("byte[" & $iSize & "];") Local $pBuffer = DllStructGetPtr($tBuffer) Local $aResult = DllCall($__g_hGDIPDll, "int", "GdipGetPropertyItem", "handle", $hImage, "uint", _ $iPropID, "uint", $iSize, "struct*", $tBuffer) If @error Then Return SetError(@error, @extended, -1) If $aResult[0] Then Return SetError(10, $aResult[0], False) Local $tPropertyItem = DllStructCreate("int id; int length; short type; ptr pvalue;", $pBuffer) Local $iBytes = DllStructGetData($tPropertyItem, "length") Local $pValue = DllStructGetData($tPropertyItem, "pvalue") Local $aRet[4] Local $type = DllStructGetData($tPropertyItem,'type') Local $tValues, $iValues Switch $type Case 2 ;ASCII String $iValues = 1 $tValues = DllStructCreate("char[" & $iBytes & "];", $pValue) Case 3 ;Array of UShort $iValues = Int($iBytes / 2) $tValues = DllStructCreate("ushort[" & $iValues & "];", $pValue) Case 4, 5 ;Array of UInt / Fraction $iValues = Int($iBytes / 4) $tValues = DllStructCreate("uint[" & $iValues & "];", $pValue) Case 9, 10 ;Array of Int / Fraction $iValues = Int($iBytes / 4) $tValues = DllStructCreate("int[" & $iValues & "];", $pValue) Case Else ;Array of Bytes $iValues = 1 $tValues = DllStructCreate("byte[" & $iBytes & "];", $pValue) EndSwitch $aRet[0] = DllStructGetData($tPropertyItem,'id') $aRet[1] = $iBytes $aRet[2] = $type Local $aVals[$iValues] If $type=2 Or $type=7 Then ; ASCII string or undefined $aVals[0] = DllStructGetData($tValues,1) Else For $i = 0 To $iValues-1 $aVals[$i] = DllStructGetData($tValues,1,$i+1) Next EndIf $aRet[3] = $aVals Return $aRet EndFunc And here is an example:
      #include <GDIPlus.au3> #include <Array.au3> Example() Func Example() _GDIPlus_Startup() Local $hImage = _GDIPlus_ImageLoadFromFile(RegRead((@AutoItX64 = True ? "HKLM\SOFTWARE\Wow6432Node\AutoIt v3\AutoIt" : "HKLM\SOFTWARE\AutoIt v3\AutoIt"), "InstallDir") & "\Examples\GUI\Torus.png") If @error Then _GDIPlus_Shutdown() MsgBox(16, "", "An error has occured - unable to load image!", 30) Return False EndIf Local $ar = _GDIPlus_ImageGetPropertyIdList($hImage) Local $vPropNbrs[UBound($ar,1)-1] ; Extract ID numbers For $i = 1 To UBound($ar,1)-1 $vPropNbrs[$i-1] = $ar[$i][0] Next ; Get all property items Local $aPropItems[UBound($vPropNbrs)][4],$vPropItem For $i = 0 To UBound($vPropNbrs)-1 $vPropItem = cGDIPlus_ImageGetPropertyItem($hImage,$vPropNbrs[$i]) For $j = 0 To 3 $aPropItems[$i][$j] = $vPropItem[$j] Next Next ; Collapse values arrays so _ArrayDisplay can display them Local $ar = $aPropItems For $i = 0 To UBound($aPropItems,1)-1 $ar[$i][0] = '0x'&Hex($ar[$i][0],4) $ar[$i][3] = '' For $j = 0 To UBound($aPropItems[$i][3])-1 $ar[$i][3] &= ($aPropItems[$i][3])[$j]&'|' Next $ar[$i][3] = StringTrimRight($ar[$i][3],1) Next _ArrayDisplay($ar) _GDIPlus_Shutdown() EndFunc Unfortunately this example (based on one now in the Help) does not exercise most of the item types, but I do not know of a file with EXIF metadata that is on most PCs. I have tested this code by updating and adding property items to torus.jpg and to a photo taken by a Sony camera. The code for implementing and calling _GDIPlus_ImageSetPropertyItem will be fairly simple [see below].
       
      Your thoughts?
       
×