Jump to content

Recommended Posts

Posted (edited)

Hey all.  I want to write a simple autoit script which will simply create a translucent, colored circle around the mouse cursor's current location that fades away over time whenever the mouse is clicked.  I would like for both left and right clicks to have their own color.  I have a feeling that the GDI plus UDF contains the functions that I would need to do this, but I have never worked with GDI before and am not sure where to start.  Any input would be awesome.

Edited by MattHiggs
Posted (edited)

@MattHiggs

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Global Const $AC_SRC_ALPHA = 1
Global $g_hGUI2, $g_idSlider, $g_hImage, $location = @ScriptDir & "\Images\tree.png"

; Create GUI
Local $hGUI1 = GUICreate("Alpha Blend", 400, 100)
GUISetState(@SW_HIDE, -1)

; Create layered child window
$g_hGUI2 = GUICreate("Test", 250, 250, -1, -1, -1, $WS_EX_LAYERED, $hGUI1)
GUISetState()
; Load layered image
_GDIPlus_Startup()
;$g_hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Images\tree.png")

GUISetState()

Local $dll = DllOpen("user32.dll")
; Loop until user exits
Do
If _IsPressed('02' $dll) Then;;right mouse click
WinSetOnTop($g_hGUI2, "", $WINDOWS_ONTOP)
        WinMove($g_hGUI2, "", MouseGetPos(0) - 50, MouseGetPos(1) - 50, 10, 10, 1)
        Local $hHBmp = _GDIPlus_ImageLoadFromFile($location)
        Local $hBitmap_Scaled = _GDIPlus_ImageResize($hHBmp, 100, 100) ;resize image
        SetBitmap($g_hGUI2, $hBitmap_Scaled, 255)
        Sleep(50)
        SetBitmap($g_hGUI2, $hBitmap_Scaled, 0)
Endif
    If _IsPressed('01', $dll) Then;;left mouse click
        WinSetOnTop($g_hGUI2, "", $WINDOWS_ONTOP)
        WinMove($g_hGUI2, "", MouseGetPos(0) - 50, MouseGetPos(1) - 50, 10, 10, 1)
        Local $hHBmp = _GDIPlus_ImageLoadFromFile($location)
        Local $hBitmap_Scaled = _GDIPlus_ImageResize($hHBmp, 100, 100) ;resize image
        SetBitmap($g_hGUI2, $hBitmap_Scaled, 255)
        Sleep(50)
        SetBitmap($g_hGUI2, $hBitmap_Scaled, 0)
    EndIf
Until GUIGetMsg() = $GUI_EVENT_CLOSE

; Release resources
_GDIPlus_ImageDispose($g_hImage)
_GDIPlus_Shutdown()

; ===============================================================================================================================
; Handle the WM_HSCROLL notificaton so that we can change the opacity in real time
; ===============================================================================================================================
Func WM_HSCROLL($hWnd, $iMsg, $iParam, $lParam)
    #forceref $hWnd, $iMsg, $iParam, $lParam
    SetBitmap($g_hGUI2, $g_hImage, GUICtrlRead($g_idSlider))
EndFunc   ;==>WM_HSCROLL

; ===============================================================================================================================
; SetBitMap
; ===============================================================================================================================
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

 

Edited by ad777

none

Posted (edited)

That seems to work :

#include <GDIPlus.au3>
#include <GUIConstants.au3>
#include <WinAPISys.au3>
#include <WinAPIConstants.au3>
#include <WinAPIProc.au3>

Global Const $tagMSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo"
Global Const $TIMER_NUMBER = 5, $DOUBLE_CLICK_DELAY = 150 ; <<<<<<< ADJUST IF NEEDED
Global $aTimer[$TIMER_NUMBER][3], $iTimer = 0
Global $hMSHook, $hMSProc

$hMSProc = DllCallbackRegister(_MouseProc, "long", "int;wparam;lparam")
$hMSHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hMSProc), _WinAPI_GetModuleHandle(0))

_GDIPlus_Startup()

OnAutoItExitRegister(_Cleanup)

While True
  Sleep(50)
WEnd

Func ShowCircle($iColor)
  Local Const $iWidth = 100, $iHeight = 100, $iBgColor = 0xFFFF00
  Local $hGUI = GUICreate("", $iWidth, $iHeight, MouseGetPos(0) - $iWidth / 2, MouseGetPos(1) - $iHeight / 2, $WS_POPUP, _
      BitOR($WS_EX_TOPMOST, $WS_EX_LAYERED, $WS_EX_NOACTIVATE))
  GUISetBkColor($iBgColor, $hGUI)
  GUISetState(@SW_SHOWNOACTIVATE)

  Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
  Local $hBmp = _GDIPlus_BitmapCreateFromGraphics(100, 100, $hGraphics)
  Local $hGfx = _GDIPlus_ImageGetGraphicsContext($hBmp)

  Local $hBrush = _GDIPlus_BrushCreateSolid($iColor)
  _GDIPlus_GraphicsFillEllipse($hGfx, 0, 0, 100, 100, $hBrush)
  _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBmp, 0, 0, 100, 100)

  Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)
  _GDIPlus_BrushDispose($hBrush)
  _GDIPlus_GraphicsDispose($hGraphics)
  _GDIPlus_GraphicsDispose($hGfx)
  _GDIPlus_BitmapDispose($hBmp)
  StartTimer($hGUI, $hHBitmap)
EndFunc   ;==>ShowCircle

Func UpdateGUI(ByRef $hHBitmap, ByRef $hGUI, $iOpacity)
  Local $tDim = DllStructCreate($tagBITMAP)
  _WinAPI_GetObject($hHBitmap, DllStructGetSize($tDim), $tDim)
  Local $tSize = DllStructCreate($tagSIZE), $tSource = DllStructCreate($tagPOINT), $tBlend = DllStructCreate($tagBLENDFUNCTION)
  Local $hScrDC = _WinAPI_GetDC($hGUI), $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC), $hOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
  $tSize.X = $tDim.bmWidth
  $tSize.Y = $tDim.bmHeight
  $tBlend.Alpha = $iOpacity
  $tBlend.Format = 1
  _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $tSize, $hMemDC, $tSource, 0, $tBlend, $ULW_ALPHA)
  _WinAPI_ReleaseDC($hGUI, $hScrDC)
  _WinAPI_SelectObject($hMemDC, $hOld)
  ;_WinAPI_DeleteObject($hOld)
  _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>UpdateGUI

Func _Cleanup()
  _GDIPlus_Shutdown()
  _WinAPI_UnhookWindowsHookEx($hMSHook)
  DllCallbackFree($hMSProc)
EndFunc   ;==>_Cleanup

Func StartTimer($hWnd, ByRef $hBitmap)
  $aTimer[$iTimer][0] = $hWnd
  $aTimer[$iTimer][1] = $hBitmap
  $aTimer[$iTimer][2] = 0
  AdlibRegister("Timer" & $iTimer, 10)
  $iTimer = Mod($iTimer + 1, $TIMER_NUMBER)
EndFunc   ;==>StartTimer

Func Timer0()
  Fade(0)
EndFunc   ;==>Timer0

Func Timer1()
  Fade(1)
EndFunc   ;==>Timer1

Func Timer2()
  Fade(2)
EndFunc   ;==>Timer2

Func Timer3()
  Fade(3)
EndFunc   ;==>Timer3

Func Timer4()
  Fade(4)
EndFunc   ;==>Timer4

Func Fade($iTmr)
  If $aTimer[$iTmr][2] = 15 Then
    GUIDelete($aTimer[$iTmr][0])
    _WinAPI_DeleteObject($aTimer[$iTmr][1])
    AdlibUnRegister("Timer" & $iTmr)
    Return
  EndIf
  $aTimer[$iTmr][2] += 1
  UpdateGUI($aTimer[$iTmr][1], $aTimer[$iTmr][0], 150 - ($aTimer[$iTmr][2] * 10))
EndFunc   ;==>Fade

Func _MouseProc($nCode, $wParam, $lParam)
  If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hMSHook, $nCode, $wParam, $lParam)
  Switch $wParam
    Case $WM_LBUTTONUP
      AdlibRegister(LeftClick, $DOUBLE_CLICK_DELAY)
    Case $WM_RBUTTONUP
      AdlibRegister(RightClick, 80)
  EndSwitch
  Return _WinAPI_CallNextHookEx($hMSHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_MouseProc

Func LeftClick()
  AdlibUnRegister(LeftClick)
  ShowCircle(0xFFE0A0FF)
EndFunc   ;==>LeftClick

Func RightClick()
  AdlibUnRegister(RightClick)
  ShowCircle(0xFF10E010)
EndFunc   ;==>RightClick

 

Edited by Nine
Issues correction

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...