Jump to content

Making my program run faster


rliiack
 Share

Recommended Posts

Is there any way on making it run faster? Thanks!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#Include <WinAPI.au3>
HotKeySet("{ESC}","Quit")

Global $finalDistance, $msg, $desktop, $iconNumber, $x, $y, $pos, $oPos, $iconPos, $iconPos2, $min, $minIndexPos, $hHandle, $save2, $input2, $checkBox2, $volume2

Global $iFD = 0
Global $pFD = 0
Global $pSC_X1 = 0
Global $pSC_Y1 = 0
Global $OS = @OSVersion

If $OS = "WIN_XP" Then
    Global $oZoomHeight=76
    Global $oZoomWidth=76
    Global $zoomHeight=142
    Global $zoomWidth=142

ElseIf $OS = "WIN_VISTA" Then

     Global $oZoomHeight=86
     Global $oZoomWidth=86
     Global $zoomHeight=172
     Global $zoomWidth=172
EndIf

opt("GUIOnEventMode",1)
$SI = GUICreate("Smart Icons Control Panel", 625, 445, 192, 124)
GUISetFont(20, 800, 0, "Australian Sunrise")
$soundOptions = GUICtrlCreateGroup("Sound Options", 88, 16, 473, 137, -1, $WS_EX_TRANSPARENT)
GUICtrlSetFont(-1, 30, 800, 0, "Australian Sunrise")
$volume = GUICtrlCreateSlider(112, 88, 257, 48)
GUICtrlSetLimit ($volume, 100, 0)
GUICtrlSetData($volume,80)
$checkBox=GUICtrlCreateCheckbox("", 408, 88, 17, 25)
$mute = GUICtrlCreateLabel("Mute", 432, 88, 65, 35)
GUICtrlSetFont(-1, 20, 800, 0, "Times New Roman")
$magnifyingSize = GUICtrlCreateGroup("Magnifying Size", 88, 168, 473, 145)
GUICtrlSetFont(-1, 30, 800, 0, "Australian Sunrise")
$input=GUICtrlCreateInput("", 144, 240, 137, 50)
GUICtrlSetFont(-1, 35, 800, 0, "Australian Sunrise")
GUICtrlSetData($input,2)
$xLabel = GUICtrlCreateLabel("X", 288, 256, 23, 35)
GUICtrlSetFont(-1, 20, 800, 0, "Times New Roman")
$Save = GUICtrlCreateButton("Save", 360, 232, 153, 65, $WS_GROUP)
GUICtrlSetFont(-1, 25, 800, 0, "Australian Sunrise")
$Quit = GUICtrlCreateButton("Quit", 216, 344, 217, 81, $WS_GROUP)
GUICtrlSetFont(-1, 35, 800, 0, "Australian Sunrise")
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Quit, "Quit")

;~ If $OS = "WIN_XP" Then
;~ $hHandle=GUICreate("Smart Icon",142,142,-1,-1, $WS_SIZEBOX + $WS_SYSMENU + $WS_MINIMIZEBOX)
;~ GUISetState(@SW_SHOW)

;~ ElseIf $OS = "WIN_VISTA" Then
;~ $hHandle=GUICreate("Smart Icon",172,192,-1,-1, $WS_SIZEBOX + $WS_SYSMENU + $WS_MINIMIZEBOX)
;~ GUISetState(@SW_SHOW)
;~ EndIf

    $hHandle = ControlGetHandle(_WinAPI_FindWindow('Progman', 'Program Manager'), '', '[CLASS:SysListView32;INSTANCE:1]')

_GDIPlus_Startup()

While 1
$input2=GUICtrlRead($input)
$checkBox2=GUICtrlRead($checkBox)
$volume2=GUICtrlRead($volume)
SoundSetWaveVolume($volume2)
GUICtrlSetOnEvent($Save, "save")

getMousePosition()
getIconPositions()

Global $distance[$iconNumber]
Global $distance2[$iconNumber]
For $i=0 To UBound($iconPos) -1
    $distance[$i]=((($iconPos2[$i][0]-$x)*($iconPos2[$i][0]-$x))+(($iconPos2[$i][1]-$y)*($iconPos2[$i][1]-$y)))^(1/2)
Next
For $i=0 To UBound($distance) -1
    If  $distance[$i]<=100 Then
        $distance2[$i]=$distance[$i]
    EndIf
Next

Local $hit=_GUICtrlListView_HitTest($desktop,-1,-1)



accessArrayData()

If ($finalDistance[$minIndexPos][0]<300 And $finalDistance[$minIndexPos][0]>0) Or $hit[2]=True Then
    $graphics=_GDIPlus_GraphicsCreateFromHWND($hHandle)

If $oPos <> $finalDistance[$minIndexPos][1] Then
; $minIndexPos is an integer
     ; $finalDistance is a 2D array
     ;$iconPos is a 2D array
     $iFD = $finalDistance[$minIndexPos][1]
     $iSC_X1 = $iconPos[$iFD][0]
     $iSC_Y1 = $iconPos[$iFD][1]
     $iDT_X2 = $iconPos[$iFD][0] + 86
     $iDT_Y2 = $iconPos[$iFD][1] + 86

     $tRect = DllStructCreate($tagRECT)
     DllStructSetData($tRect, 1, $pSC_X1)
     DllStructSetData($tRect, 2, $pSC_Y1)
     DllStructSetData($tRect, 3, $pSC_X1+$zoomWidth)
     DllStructSetData($tRect, 4, $psc_Y1+$zoomHeight)
     _WinAPI_InvalidateRect($hHandle, $tRect, 1)

     Sleep(50)

     $hbitmap = _ScreenCapture_Capture("", $iSC_X1, $iSC_Y1, $iDT_X2, $iDT_Y2, False)

     Sleep(300)
EndIf

$oPos=$iFD
$pFD = $finalDistance[$minIndexPos][1]
$pSC_X1 = $iconPos[$pFD][0]
$pSC_Y1 = $iconPos[$pFD][1]

    $bitmap=_GDIPlus_BitmapCreateFromHBITMAP($hbitmap)

    $height=_GDIPlus_ImageGetHeight($bitmap)
    $width=_GDIPlus_ImageGetWidth($bitmap)
    _GDIPlus_GraphicsDrawImageRectRect($graphics,$bitmap,0,0,$width,$height,$iSC_X1,$iSC_Y1,$zoomWidth,$zoomHeight)

    If $checkBox2=$GUI_UNCHECKED Then
    SoundPlay(@WindowsDir & "\media\tada.wav",1)
    EndIf

    _WinAPI_DeleteObject($hbitmap)
    _WinAPI_DeleteObject($bitmap)
EndIf
WEnd

_GDIPlus_Shutdown()




Func getMousePosition()
$pos=MouseGetPos()
$x=$pos[0]
$y=$pos[1]
EndFunc





Func getIconPositions()
    $desktop=ControlGetHandle("[CLASS:Progman]","","[CLASS:SysListView32;INSTANCE:1]")
    $iconNumber=_GUICtrlListView_GetItemCount($desktop)
    If $iconNumber>0 Then
         Global $iconPos[$iconNumber][2]
         Global $iconPos2[$iconNumber][2]
     For $i=0 To UBound($iconPos) -1
         If $OS = "WIN_XP" Then
              $iconPos[$i][0]=_GUICtrlListView_GetItemPositionX($desktop, $i)-22
              $iconPos[$i][1]=_GUICtrlListView_GetItemPositionY($desktop, $i)
              $iconPos2[$i][0]=_GUICtrlListView_GetItemPositionX($desktop, $i)+38
              $iconPos2[$i][1]=_GUICtrlListView_GetItemPositionY($desktop, $i)+31

        ElseIf $OS = "WIN_VISTA" Then
              $iconPos[$i][0]=_GUICtrlListView_GetItemPositionX($desktop, $i)
              $iconPos[$i][1]=_GUICtrlListView_GetItemPositionY($desktop, $i)
              $iconPos2[$i][0]=_GUICtrlListView_GetItemPositionX($desktop, $i)+38
              $iconPos2[$i][1]=_GUICtrlListView_GetItemPositionY($desktop, $i)+34.5
        EndIf
    Next
    Else
        MsgBox(64,"Error","No icons on desktop.", 3)
        Exit
    EndIf
EndFunc




Func Quit()
_SendMessage(_WinAPI_FindWindow('Progman', 'Program Manager'), 0x0111, 0x7103, 0)
   Exit
EndFunc





Func accessArrayData()
     Local $counter=0
     Local $counter2=0
     Global $finalDistance[UBound($distance2)][2]

    for $i=0 To UBound($distance2) -1
        If $distance2[$i]>0 Then
             $finalDistance[$counter][0]=$distance2[$i]
             $finalDistance[$counter][1]=$i
             $counter=$counter+1
         EndIf
     Next

    _ArraySort($finalDistance,1,0,0,0)

    If $finalDistance[0][0]=0 Then
        $counter2=0
    Else
    for $i=0 To UBound($finalDistance) -1
        If $finalDistance[$i][0]>0 Then
            $counter2=$counter2+1
        EndIf
    Next
    $counter2-=1
    EndIf

    $min=$finalDistance[$counter2][0]
    $minIndexPos=_ArraySearch($finalDistance,$min)
EndFunc




Func save()
    If $input2=1 Then
        $zoomHeight=$oZoomHeight
        $zoomWidth=$oZoomWidth
    Else
        $zoomHeight=$oZoomHeight*$input2
        $zoomWidth=$oZoomWidth*$input2
    EndIf
EndFunc

My Projects:Smart Icons

Link to comment
Share on other sites

I've tried to get rid of what seems to me as redundant code, you can disagree about it of course. It's not faster of course because of this BitBlt stuff and GDIPlus procedures, or it's just my display card/driver. Anyway, if you can google around for DIBSECTION and the functions the operate on this struct you'll probably find something faster. It's mainly depended on your hardware end.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ScreenCapture.au3>
Opt("GUIOnEventMode", 1)
HotKeySet("{ESC}", "Quit")

Global $finalDistance, $msg, $desktop, $iconNumber, $x, $y, $pos, $oPos, $iconPos, $iconPos2, $min, $minIndexPos, $hHandle, $save2, $checkBox2, $volume2

Global $iFD = 0
Global $pFD = 0
Global $pSC_X1 = 0
Global $pSC_Y1 = 0
Global $iSC_X1, $iSC_Y1, $iOSC_X1 = -1, $iOSC_Y1 = -1
Global $iDT_X2, $iDT_Y2
Global $OS = @OSVersion

If $OS = "WIN_XP" Then
    Global $oZoomHeight = 76
    Global $oZoomWidth = 76
    Global $zoomHeight = 142
    Global $zoomWidth = 142

ElseIf $OS = "WIN_VISTA" Then

    Global $oZoomHeight = 86
    Global $oZoomWidth = 86
    Global $zoomHeight = 172
    Global $zoomWidth = 172
EndIf

$SI = GUICreate("Smart Icons Control Panel", 625, 445, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetFont(20, 800, 0, "Australian Sunrise")
$soundOptions = GUICtrlCreateGroup("Sound Options", 88, 16, 473, 137, -1, $WS_EX_TRANSPARENT)
GUICtrlSetFont(-1, 30, 800, 0, "Australian Sunrise")
$volume = GUICtrlCreateSlider(112, 88, 257, 48)
GUICtrlSetLimit($volume, 100, 0)
GUICtrlSetData($volume, 80)
$hvolume = GUICtrlGetHandle($volume)
$checkBox = GUICtrlCreateCheckbox("", 408, 88, 17, 25)
$mute = GUICtrlCreateLabel("Mute", 432, 88, 65, 35)
GUICtrlSetFont(-1, 20, 800, 0, "Times New Roman")
$magnifyingSize = GUICtrlCreateGroup("Magnifying Size", 88, 168, 473, 145)
GUICtrlSetFont(-1, 30, 800, 0, "Australian Sunrise")
$input = GUICtrlCreateInput("", 144, 240, 137, 50)
GUICtrlSetFont(-1, 35, 800, 0, "Australian Sunrise")
GUICtrlSetData($input, 2)
$xLabel = GUICtrlCreateLabel("X", 288, 256, 23, 35)
GUICtrlSetFont(-1, 20, 800, 0, "Times New Roman")
$Save = GUICtrlCreateButton("Save", 360, 232, 153, 65, $WS_GROUP)
GUICtrlSetFont(-1, 25, 800, 0, "Australian Sunrise")
GUICtrlSetOnEvent($Save, "save")
$Quit = GUICtrlCreateButton("Quit", 216, 344, 217, 81, $WS_GROUP)
GUICtrlSetFont(-1, 35, 800, 0, "Australian Sunrise")
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Quit, "Quit")



;~ If $OS = "WIN_XP" Then
;~ $hHandle=GUICreate("Smart Icon",142,142,-1,-1, $WS_SIZEBOX + $WS_SYSMENU + $WS_MINIMIZEBOX)
;~ GUISetState(@SW_SHOW)

;~ ElseIf $OS = "WIN_VISTA" Then
;~ $hHandle=GUICreate("Smart Icon",172,192,-1,-1, $WS_SIZEBOX + $WS_SYSMENU + $WS_MINIMIZEBOX)
;~ GUISetState(@SW_SHOW)
;~ EndIf

$hHandle = ControlGetHandle(_WinAPI_FindWindow('Progman', 'Program Manager'), '', '[CLASS:SysListView32;INSTANCE:1]')
$desktop = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]")
$tRect = DllStructCreate($tagRECT)

_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hHandle)

While 1
    $pos = MouseGetPos()
    $hit = _GUICtrlListView_HitTest($desktop, $pos[0], $pos[1])
    
    If $hit[0] > -1 Then
        $aItemRect = _GUICtrlListView_GetItemRect($desktop, $hit[0], 0)
        
        $iSC_X1 = $aItemRect[0]
        $iSC_Y1 = $aItemRect[1]
        $iDT_X2 = $aItemRect[2]
        $iDT_Y2 = $aItemRect[3]

        DllStructSetData($tRect, 1, $iOSC_X1)
        DllStructSetData($tRect, 2, $iOSC_Y1)
        DllStructSetData($tRect, 3, $iOSC_X1 + $zoomWidth)
        DllStructSetData($tRect, 4, $iOSC_Y1 + $zoomHeight)
        _WinAPI_InvalidateRect($hHandle, $tRect, 1)

        $iOSC_X1 = $iSC_X1
        $iOSC_Y1 = $iSC_Y1
        $iW = $iDT_X2-$iSC_X1
        $iH = $iDT_Y2-$iSC_Y1
        
        $hbitmap = _ScreenCapture_Capture("", $iSC_X1, $iSC_Y1, $iDT_X2, $iDT_Y2, False)
        $bitmap = _GDIPlus_BitmapCreateFromHBITMAP($hbitmap)
        _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $bitmap, 0, 0, $iW, $iH, $iSC_X1, $iSC_Y1, $zoomWidth, $zoomHeight)

        If  GUICtrlRead($checkBox) = $GUI_UNCHECKED Then SoundPlay(@WindowsDir & "\media\tada.wav", 1)

        _WinAPI_DeleteObject($hbitmap)
        _WinAPI_DeleteObject($bitmap)
        
    ElseIf ($pos[0] < $iOSC_X1 Or $pos[0] > $iOSC_X1+$zoomWidth) And ($pos[1] < $iOSC_Y1 Or $pos[1] > $iOSC_Y1+$zoomHeight) And _
        ($iOSC_X1 <> -1 And $iOSC_Y1 <> -1) Then
        DllStructSetData($tRect, 1, $iOSC_X1)
        DllStructSetData($tRect, 2, $iOSC_Y1)
        DllStructSetData($tRect, 3, $iOSC_X1 + $zoomWidth)
        DllStructSetData($tRect, 4, $iOSC_Y1 + $zoomHeight)
        _WinAPI_InvalidateRect($hHandle, $tRect, 1)
        $iOSC_X1 = -1
        $iOSC_Y1 = -1
    EndIf
    
    Sleep(20)
WEnd

_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    
    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)

    If DllStructGetData($tNMHDR, "hWndFrom") = $hvolume And _
       DllStructGetData($tNMHDR, "Code") = $NM_RELEASEDCAPTURE Then _
        SoundSetWaveVolume(GUICtrlRead($volume))
    
    Return $GUI_RUNDEFMSG
EndFunc

Func Quit()
    _SendMessage(_WinAPI_FindWindow('Progman', 'Program Manager'), 0x0111, 0x7103, 0)
    Exit
EndFunc   ;==>Quit


Func save()
    Local $input2 = Number(GUICtrlRead($input))
    
    If $input2=1 Then
        $zoomHeight=$oZoomHeight
        $zoomWidth=$oZoomWidth
    Else
        $zoomHeight=$oZoomHeight*$input2
        $zoomWidth=$oZoomWidth*$input2
    EndIf
EndFunc   ;==>save
Edited by Authenticity
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...