Jump to content

Dan's misc. Scripts


Dan_555
 Share

Recommended Posts

Clipboard_Monitoring
Playing with my previous code I've added a few buttons to it, along with clipboard monitoring found in the Help File.
The button 0 contains a copy of the last screenshot, it will be green if it contains a picture.

The buttons 1 to x have following functions:
If they are red, left click will store the screenshot into it, and they will get a cyan color.
Clicking on the cyan button with the left mouse click will display the stored image. Right click will delete the image from it and the button will become red again.

The images are stored in memory, and when the program is closed, they will be deleted.

The window is resizable and the image will adapt to the size.

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=..\..\..\!MyApps\ClipboardMonitor\ClipBoard-ImageMonitoring.exe
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;modified code from https://www.autoitscript.com/forum/topic/57053-updating-a-picture-control-without-a-file/?do=findComment&comment=514735
;#include <ScreenCapture.au3>
#include <SendMessage.au3>
#include <Clipboard.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIHObj.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <String.au3>
#include <ListBoxConstants.au3>
#include <StructureConstants.au3>

Opt("GUIResizeMode", BitOR($GUI_DOCKSIZE, $Gui_DOCKLEFT))
Opt("TrayMenuMode", 1)
Opt("TrayAutoPause", 0)

Local $aFmt[][] = [["PNG", 0], ["JPG", 0], ["BMP", 0], ["GIF", 0], ["TIFF", 0]]
Local $DefaultMenuTypeFMT = 0

Local $aSaveSet[10][2]
Local $DefaultMenuSaveSet = 0

Global $btnmax = 30
Global $CBTN[$btnmax][2]

;Check if @scriptdir has a \, add it if not
$tmp = "\"
If StringRight(@ScriptDir, 1) = "\" Then $tmp = ""
Global $Foldername = @ScriptDir & $tmp & "IMG_"
Global $FNMax = 2
$tmp = ""

Global $hGUI = GUICreate("Clipboard Monitoring", 450, 290, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX), $WS_EX_ACCEPTFILES)

$idMenuFile = GUICtrlCreateMenu("File")
$idMenuOpenFolder = GUICtrlCreateMenuItem("Open Folder", $idMenuFile)
GUICtrlCreateMenuItem("", $idMenuFile)
$idMenuSave = GUICtrlCreateMenuItem("Save Images", $idMenuFile)

$idMenuOptions = GUICtrlCreateMenu("Options")
For $x = 0 To UBound($aFmt) - 1
    $aFmt[$x][1] = GUICtrlCreateMenuItem($aFmt[$x][0], $idMenuOptions, $idMenuSave, 1)
    If $x = $DefaultMenuTypeFMT Then GUICtrlSetState(-1, $gui_checked)
Next
GUICtrlCreateMenuItem("", $idMenuOptions)
$idMenuClearButtons = GUICtrlCreateMenuItem("Clear Buttons", $idMenuOptions)

$hMenuSet = GUICtrlCreateMenu("Image Set")
For $x = 0 To UBound($aSaveSet) - 1

    $tmp = _StringRepeat("0", $FNMax - StringLen($x)) & $x
    $aSaveSet[$x][0] = $tmp
    $tmp = ""
    If FileExists($Foldername & $aSaveSet[$x][0] & "\") = 0 Then $tmp = " (New)"
    $aSaveSet[$x][1] = GUICtrlCreateMenuItem($aSaveSet[$x][0] & $tmp, $hMenuSet, -1, 1)
    If $x = $DefaultMenuSaveSet Then GUICtrlSetState(-1, $gui_checked)

Next
$tmp = ""

;Create Buttons
For $x = 0 To ($btnmax - 1)
    $CBTN[$x][0] = GUICtrlCreateButton($x, 1 + ($x * 21), 1, 21, 25)
    GUICtrlSetBkColor(-1, "0xFF7070")
Next

GUICtrlSetTip($CBTN[0][0], "Copy of the last Captured image")

$hListBox = GUICtrlCreateList("", 1, 30, 80, 222, BitOR($LBS_NOTIFY, $WS_VSCROLL, $WS_BORDER))
GUICtrlSetResizing(-1, BitOR($GUI_DOCKLEFT, $GUI_DOCKTOP, $GUI_DOCKBOTTOM, $GUI_DOCKWIDTH))
$hWndListBox = GUICtrlGetHandle($hListBox)

Local $idLabel = GUICtrlCreateLabel("", 85, 30, 360, 214, $WS_BORDER)
GUICtrlSetResizing(-1, BitOR($GUI_DOCKLEFT, $GUI_DOCKTOP, $GUI_DOCKBOTTOM, $GUI_DOCKBORDERS))
GUICtrlSetState($idLabel, $GUI_DROPACCEPTED)

GUISetState()

Global $hImage
Local $anmsg

;~ If Not _ClipBoard_IsFormatAvailable($CF_BITMAP) Then
;~     Send("{printscreen}")
;~ EndIf

Global $g_hNext = _ClipBoard_SetViewer($hGUI)
GUIRegisterMsg($WM_CHANGECBCHAIN, "WM_CHANGECBCHAIN")
GUIRegisterMsg($WM_DRAWCLIPBOARD, "WM_DRAWCLIPBOARD")

_GDIPlus_Startup()

ClipDrawBitmap()
PopulateListBox($DefaultMenuSaveSet)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
$sLastSel = ""
$fAction = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_DROPPED
            ; If the value of @GUI_DropId is $idLabel, then set the label of the dragged file.
            If @GUI_DropId = $idLabel Then LoadDroppedImage(@GUI_DragFile)
        Case $idMenuClearButtons
            $WPos = WinGetPos($hGUI)
            $iMsgBoxAnswer = MsgBoxButton("Warning ! ", " Do you really want to clear all buttons?", "Yes|No", 200, 80, $WPos[0] + 130, $WPos[1] + 50, 2)
            If $iMsgBoxAnswer = 1 Then ;Yes
                For $x = 1 To ($btnmax - 1)
                    _GDIPlus_ImageDispose($CBTN[$x][1])
                    $CBTN[$x][1] = 0
                    GUICtrlSetBkColor($CBTN[$x][0], "0xFF7070")
                Next
            EndIf
            $Wpos=""
        Case $idMenuOpenFolder
            $tmp = $Foldername & $aSaveSet[$DefaultMenuSaveSet][0] & "\"
            If FileExists($tmp) Then ShellExecute($tmp)
            $tmp = ""
        Case $idMenuSave
            SaveImages($DefaultMenuSaveSet, $DefaultMenuTypeFMT)
            PopulateListBox($DefaultMenuSaveSet)
        Case $GUI_EVENT_CLOSE
            $WPos = WinGetPos($hGUI)
            $iMsgBoxAnswer = MsgBoxButton("Warning ! ", " Do you really want to end the program ?", "Yes|No", 200, 80, $WPos[0] + 130, $WPos[1] + 50, 2)
            If $iMsgBoxAnswer = 1 Then ;Yes
                _GDIPlus_ImageDispose($hImage)
                For $x = 0 To ($btnmax - 1)
                    _GDIPlus_ImageDispose($CBTN[$x][1])
                Next
                _GDIPlus_Shutdown()
                ; Shut down clipboard viewer
                _ClipBoard_ChangeChain($hGUI, $g_hNext)
                Exit
            EndIf
            $WPos = ""
        Case $GUI_EVENT_RESIZED, $GUI_EVENT_RESTORE, $GUI_EVENT_MAXIMIZE
            ReDrawBitmap()
        Case $GUI_EVENT_SECONDARYUP
            $anmsg = GUIGetCursorInfo($hGUI)
            If IsArray($anmsg) Then
                For $x = 0 To ($btnmax - 1)
                    If $x > 0 Then
                        If $anmsg[4] = $CBTN[$x][0] Then
                            If $CBTN[$x][1] > 0 Then
                                _GDIPlus_ImageDispose($CBTN[$x][1])
                                GUICtrlSetBkColor($CBTN[$x][0], "0xFF7070")
                                $CBTN[$x][1] = 0
                            EndIf
                        EndIf
                    EndIf
                Next
            EndIf
            $anmsg = ""
        Case Else
            If $nMsg > 0 Then
                ;Loop through the Buttons
                For $x = 0 To ($btnmax - 1)
                    If $nMsg = $CBTN[$x][0] Then
                        If $x > 0 Then
                            If $CBTN[$x][1] = 0 And $hImage > 0 Then
                                $CBTN[$x][1] = _GDIPlus_ImageClone($hImage)
                                GUICtrlSetBkColor($CBTN[$x][0], "0x70FFFF")
                            EndIf
                        EndIf
                        If $CBTN[$x][1] > 0 Then
                            _GDIPlus_ImageDispose($hImage)
                            $hImage = _GDIPlus_ImageClone($CBTN[$x][1])
                        EndIf
                        ReDrawBitmap()
                    EndIf
                Next

                ;Loop throgh the Set menu
                For $x = 0 To (UBound($aSaveSet) - 1)
                    If $nMsg = $aSaveSet[$x][1] Then
                        $DefaultMenuSaveSet = $x
                        PopulateListBox($x)
                    EndIf
                Next

                ;Loop throgh Option menu
                For $x = 0 To (UBound($aFmt) - 1)
                    If $nMsg = $aFmt[$x][1] Then
                        $DefaultMenuTypeFMT = $x
                    EndIf
                Next

            EndIf
    EndSwitch

    If GUICtrlRead($hListBox) <> $sLastSel Then                ;Code by melba23 - autoit forum
        $sLastSel = GUICtrlRead($hListBox)
        Sleep(150)
        If $fAction = 1 Then
            ;Listbox Double Click
            LoadImage($sLastSel, $DefaultMenuSaveSet)
        Else
            ;Listbox Single Click
            LoadImage($sLastSel, $DefaultMenuSaveSet)
        EndIf
        $fAction = 0

    Else
        If $fAction = 1 Then
            ;Listbox Double Click
            LoadImage($sLastSel, $DefaultMenuSaveSet)
            $fAction = 0
        EndIf
    EndIf

WEnd

Func LoadImage($FNa, $Nr)
    Local $tmp1 = $aSaveSet[$Nr][0]
    Local $tmpF = $Foldername & $tmp1 & "\" & $FNa
    Local $tmpH = 0
    If FileExists($tmpF) Then
        $tmpH = _GDIPlus_ImageLoadFromFile($tmpF)
        If $tmpH > 0 Then
            If $hImage > 0 Then _GDIPlus_ImageDispose($hImage)
            $hImage = _GDIPlus_ImageClone($tmpH)
            _GDIPlus_ImageDispose($tmpH)
            ReDrawBitmap()
        EndIf
    EndIf
EndFunc   ;==>LoadImage

Func LoadDroppedImage($Filename)
    If FileExists($Filename) Then
        $tmpH = _GDIPlus_ImageLoadFromFile($Filename)
        If $tmpH > 0 Then
            If $hImage > 0 Then _GDIPlus_ImageDispose($hImage)
            $hImage = _GDIPlus_ImageClone($tmpH)
            _GDIPlus_ImageDispose($tmpH)
            ReDrawBitmap()
        EndIf
    EndIf
EndFunc   ;==>LoadDroppedImage

Func PopulateListBox($Nr = 0)
    Local $tmp = $aFmt[1][0]
    Local $tmp1 = $aSaveSet[$Nr][0]
    Local $tmpF = $Foldername & $tmp1 & "\"
    Local $tmpFN = "", $tmpFNa = ""
    Local $tmpfiles
    Local $x, $y
    Local $tmpBox = ""
    GUICtrlSetData($hListBox, $tmpBox)

    For $y = 0 To UBound($aFmt) - 1
        $tmp = $aFmt[$y][0]
        For $x = 0 To $btnmax - 1
            $tmpFNa = _StringRepeat("0", 2 - StringLen($x)) & $x & "." & $tmp
            $tmpFN = $tmpF & $tmpFNa
            If FileExists($tmpFN) Then
                $tmpBox = $tmpBox & $tmpFNa & "|"
            EndIf
        Next
    Next

    GUICtrlSetData($hListBox, $tmpBox)
EndFunc   ;==>PopulateListBox

Func SaveImages($Nr = 0, $typ = 0)

    Local $tmp = $aFmt[$typ][0]
    Local $tmp1 = $aSaveSet[$Nr][0]
    Local $tmpF = $Foldername & $tmp1 & "\"
    Local $tmpFN = ""
    Local $tmpfiles

    If FileExists($tmpF) = 0 Then DirCreate($tmpF)

    $tmpexist = 0

    For $x = 1 To UBound($CBTN) - 1
        $tmpFNa = _StringRepeat("0", 2 - StringLen($x)) & $x & "." & $tmp
        $tmpFN = $tmpF & $tmpFNa
        If FileExists($tmpFN) And $CBTN[$x][1] > 0 Then
            $tmpexist = $tmpexist + 1
            $tmpfiles = $tmpfiles & $tmpFNa & @CRLF
        EndIf
    Next


    If $tmpexist > 0 Then
        $WPos = WinGetPos($hGUI)
        If MsgBoxButton("Warning: File(s) exist", "Following files may be deleted from:" & @CRLF & $tmpF & @CRLF & @CRLF & $tmpfiles & "Do you wish to continue ?", "Yes|No", 220, 100, $WPos[0] + 130, $WPos[1] + 50, 2) = 1 Then $tmpexist = 0
    EndIf

    If $tmpexist = 0 Then
        For $x = 1 To UBound($CBTN) - 1
            $tmpFN = $tmpF & _StringRepeat("0", 2 - StringLen($x)) & $x & "." & $tmp
            If $CBTN[$x][1] > 0 Then _GDIPlus_ImageSaveToFile($CBTN[$x][1], $tmpFN)
        Next
    EndIf

EndFunc   ;==>SaveImages

Func ReDrawBitmap()
    $WPos = WinGetPos($hGUI)
    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_DrawImagePoints($hGraphics, $hImage, 85, 30, $WPos[2] - 20, 30, 85, $WPos[3] - 62)
    _GDIPlus_GraphicsDispose($hGraphics)
EndFunc   ;==>ReDrawBitmap

Func ClipDrawBitmap()
    Local $WPos, $hBitmap, $hGraphics
    $WPos = WinGetPos($hGUI)

    _ClipBoard_Open($hGUI)
    $hBitmap = _ClipBoard_GetDataEx($CF_BITMAP)
    _ClipBoard_Close()

    If $hBitmap > 0 Then
        If $hImage > 0 Then _GDIPlus_ImageDispose($hImage)
        If $CBTN[0][1] > 0 Then _GDIPlus_ImageDispose($CBTN[0][1])

        $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
        $CBTN[0][1] = _GDIPlus_ImageClone($hImage)
        GUICtrlSetBkColor($CBTN[0][0], "0x00FF00")
        _WinAPI_DeleteObject($hBitmap)
        ReDrawBitmap()
    EndIf

EndFunc   ;==>ClipDrawBitmap

; Handle $WM_CHANGECBCHAIN messages
Func WM_CHANGECBCHAIN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; If the next window is closing, repair the chain
    If $wParam = $g_hNext Then
        $g_hNext = $lParam
        ; Otherwise pass the message to the next viewer
    ElseIf $g_hNext <> 0 Then
        _SendMessage($g_hNext, $WM_CHANGECBCHAIN, $wParam, $lParam, 0, "hwnd", "hwnd")
    EndIf
EndFunc   ;==>WM_CHANGECBCHAIN

; Handle $WM_DRAWCLIPBOARD messages
Func WM_DRAWCLIPBOARD($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    ; Display any image from clipboard
    ClipDrawBitmap()
    ; Pass the message to the next viewer
    If $g_hNext <> 0 Then _SendMessage($g_hNext, $WM_DRAWCLIPBOARD, $wParam, $lParam)
EndFunc   ;==>WM_DRAWCLIPBOARD


Func MsgBoxButton($title = "MsgBox", $txt = "", $buttons = "Ok|Cancel", $width = 250, $height = 100, $xpos = -1, $ypos = -1, $defbtn = 1)
    ;By Dan_555
    ;Displays a custom message box with variable number of buttons
    ;$title = Title text,  $txt=info text
    ;$buttons = Add any number of buttons needed by entering their names. Buttons are separated by |  E.g. 'yes|no' will create two buttons. Button numbers start from 1
    ;$Width, $height minimum of 250,100. Height is expanded by the amount of buttons.
    ;Button width is dependant on the Gui-Width
    ;$defbtn sets a button to act as default button - Acted upon return
    Local $hParentGui = WinGetHandle("", "")
    GUISetState(@SW_DISABLE, $hParentGui)
    Local $nMsg
    Local $btnnr = -1
    If StringLeft($buttons, 1) <> "|" Then $buttons = "|" & $buttons
    If StringRight($buttons, 1) <> "|" Then $buttons = $buttons & "|"
    Local $a_btn = _StringBetween($buttons, "|", "|")

    If @error = 0 Then
        For $x = UBound($a_btn) - 1 To 0 Step -1
            If $a_btn[$x] = "" Then _ArrayDelete($a_btn, $x)
        Next
    Else                                                            ;Error creating button array
        Local $a_btn[] = ["ok"]
    EndIf

    If $height < (UBound($a_btn)) * 20 Then $height = (UBound($a_btn)) * 20
    If $width < 250 Then $width = 250
    If $height < 100 Then $height = 100

    Local $ahBtn[UBound($a_btn)]

    Local $hMsgBoxGui = GUICreate($title, $width, $height, $xpos, $ypos, BitOR($WS_DLGFRAME, $WS_BORDER, $WS_VISIBLE), BitOR($WS_EX_TOPMOST, $WS_EX_APPWINDOW), $hParentGui)
    Local $hMsgBoxEdi = GUICtrlCreateEdit($txt, 0, 0, 195, $height, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY))
    ;Local $hMsgBoxEdi = GUICtrlCreateEdit($txt, 0, 0, 195, $height, BitOR($WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY))
    ;Local $hMsgBoxEdi = GUICtrlCreateLabel($txt, 2, 2, 195, $height-4,$SS_SUNKEN)
    Local $xs = $height - (20 * (UBound($a_btn) - 1)) - 20
    For $x = 0 To UBound($a_btn) - 1
        $deftyp = -1
        If $defbtn = $x + 1 Then $deftyp = $BS_DEFPUSHBUTTON
        $ahBtn[$x] = GUICtrlCreateButton($a_btn[$x], 198, $xs + ($x * 20), $width - 199, 20, $deftyp)
;~      $ahBtn[$x] = GUICtrlCreateButton($a_btn[$x], 198, (UBound($a_btn)-1 = 0) ? (($height/2)-10)+($x * 20) : (($height/UBound($a_btn))-20)+($x * 20) , $width-199, 20,$deftyp)
    Next
    GUISetState(@SW_SHOW)
    Do
        $nMsg = GUIGetMsg()
        If $nMsg > 0 Then
            For $x = 0 To UBound($ahBtn) - 1
                If $nMsg = $ahBtn[$x] Then $btnnr = $x + 1
            Next
        EndIf
    Until $btnnr > -1
    GUIDelete($hMsgBoxGui)
    GUISetState(@SW_ENABLE, $hParentGui)
    WinActivate($hParentGui)
    Return $btnnr
EndFunc   ;==>MsgBoxButton


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    ;code from https://www.autoitscript.com/forum/topic/119058-how-to-set-a-handler-function-for-listbox-double-click-action/?do=findComment&comment=827814
    #forceref $hWnd, $iMsg

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)

    $hWndFrom = $lParam
    $iCode = BitShift($wParam, 16) ; Hi Word

    Switch $hWndFrom
        Case $hWndListBox
            Switch $iCode
                Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
                    $fAction = 1
            EndSwitch
    EndSwitch

EndFunc   ;==>WM_COMMAND

Edit: 27.11.2024 : Added the missing    _ClipBoard_ChangeChain($hGUI, $g_hNext)  before exiting the script.
Edit #2:  Added a custom Message box when exiting the program. 

Edit #3; Added some menus and a Listbox.
               You can open the Folder of a set (if it exists). Save all the images which are in the button, into the set folder. You can choose a filetype (png,jpg,bmp,gif,tiff)

 under which the files will be saved). You can choose a set number (will display the set name and "(new)" if the folder does not exists). Listbox will display the files from the set, the filenames will be added 1-29 for each filetype. (double)Clicking on the filename in the listbox will display the  image.

Edit #4: Because why not: Just after posting the last update i thought to add loading and diplaying files with Drag&Drop ... You can drag and drop one image file from the explorer into the area where the pictures are usually displayed.

Edit #5; Probably the last one for today: Added a menu item (under options) to clear all buttons.

Edited by Dan_555

Some of my script sourcecode

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