Jump to content
Sign in to follow this  
HarryBeasant

Help to get a script to run on button click

Recommended Posts

HarryBeasant

Hello,

I would like this function;

Func _ReduceMemory($i_PID = -1)

If $i_PID <> -1 Then

Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)

$ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])

DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])

Else

$ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)

EndIf

Return $ai_Return[0]

EndFunc ;==>_ReduceMemory

to be ran when a button it pressed;

$Button_1 = GUICtrlCreateButton("Clean RAM", 105, 65, 120)

Please help?!

Share this post


Link to post
Share on other sites
Melba23

HarryBeasant,

Welcome to the AutoIt forum. :)

But please do not bump your posts within 24 hours. :mellow:

Remember this is not a 24/7 support forum - those who answer are only here because they like helping others and have some time to spare. You just have to wait until someone who knows something about your particular problem, and is willing to help, comes online. Be patient and someone will answer eventually - like this.

You will need to create a GUI to hold the $Button_1 control. Then you will either have to look for the event message sent by the button press within a GUIGetMsg loop or else use the OnEvent mode to intercept it directly.

If all that sounds like Chinese, then reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at the excellent tutorials that you will find here and here - you will find other tutorials in the Wiki (the link is at the top of the page).

There are even video tutorials on http://www.youtube.com/results?search_qu...oit+tutorial&search_type=&aq=0 if you prefer watching to reading.

You know where we are if you get into difficulties. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
HarryBeasant

Thanks for the reply, i have this code at the top, i really can't figure these "case statements" it will really help if you could give me an example of a button when clicked running any command in code please

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

$GUI = GUICreate("RAM Monitor", 321, 121, 270, 173)

$menumenu = GUICtrlCreateMenu("Menu")

$menuitem = GUICtrlCreateMenuItem("Help", $menumenu)

GUICtrlSetState(-1, $GUI_DEFBUTTON)

$Button_1 = GUICtrlCreateButton("Clean RAM", 105, 65, 120)

$Progress = GUICtrlCreateProgress(0, 1, 320, 27)

GUISetState(@SW_SHOW)

Share this post


Link to post
Share on other sites
Melba23

HarryBeasant,

You read those tutorials very quickly! :mellow:

Seriously, this is absolutely basic stuff - if you cannot get your controls to action than there is little point in coding. This example shows how to do it:

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 200, 200)
$hButton = GUICtrlCreateButton("Test", 60, 80, 80, 30)
GUISetState()
While 1
Switch GUIGetMsg()
  Case $GUI_EVENT_CLOSE
   Exit
  Case $hButton
   MsgBox(0, "Pressed", "Test")
EndSwitch
WEnd

Now go and read those tutorials! :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
HarryBeasant

thanks! last question, that ram cleaning script, where abouts in that code you just wrote would it go?

Share this post


Link to post
Share on other sites
Melba23

HarryBeasant,

You can put it at the end like this: :mellow:

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 200, 200)
$hButton = GUICtrlCreateButton("Test", 60, 80, 80, 30)
GUISetState()
While 1
Switch GUIGetMsg()
  Case $GUI_EVENT_CLOSE
   Exit
  Case $hButton
   _DoIt() ; Call the function here
EndSwitch
WEnd
Func _DoIt() ; Define the function here
MsgBox(0, "Pressed", "Test")
EndFunc

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
HarryBeasant

Bare with me here mate.

While 1
Switch GUIGetMsg()
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button_1
   _DoIt($Button_1) ; Call the function here
EndSwitch
WEnd
Func _DoIt(_ReduceMemory()
Func _ReduceMemory($i_PID = -1)
        If $i_PID <> -1 Then
                Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
                Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
                DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
        Else
                Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
        EndIf
        Return $ai_Return[0]
EndFunc;==> _ReduceMemory()
While 1
        Sleep(100)
WEnd)
MsgBox(0, "Pressed", "Test")
EndFunc
Edited by HarryBeasant

Share this post


Link to post
Share on other sites
Melba23

HarryBeasant,

Close, but no cigar! :)

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 200, 200)
$hButton = GUICtrlCreateButton("Test", 60, 80, 80, 30)
GUISetState()
While 1
Switch GUIGetMsg()
  Case $GUI_EVENT_CLOSE
   Exit
  Case $hButton
   _ReduceMemory() ; Call the function here
EndSwitch
WEnd
Func _ReduceMemory($i_PID = -1)
If $i_PID <> -1 Then
  Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
  $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
  DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
Else
  $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndIf
Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

But I am not sure you should be playing with DllCalls if you cannot get them to run without so much help..... :mellow:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Melba23

HarryBeasant,

Here is a cleaned-up version of your script - take a good look and you will see where I have changed it and added comments to explain why:

#include <GUIConstantsEx.au3>
;
$GUI = GUICreate("RAM Monitor", 321, 121, 270, 173)
$menumenu = GUICtrlCreateMenu("Menu")
$menuitem = GUICtrlCreateMenuItem("Help", $menumenu)
$Button_1 = GUICtrlCreateButton("Clean RAM", 105, 65, 120)
$Progress = GUICtrlCreateProgress(0, 1, 320, 27)
GUICtrlCreateLabel("Current Memory Usage: ", 4, 32, 120, 20)
$Label1 = GUICtrlCreateLabel("", 124, 32, 200, 20) ; Create the label here so you can update it later
GUISetState(@SW_SHOW)
;
; Start a timer with a value to gurantee a quick response
$iBegin = 0
;
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button_1
   _ReduceMemory() ; Call the function here
EndSwitch
If TimerDiff($iBegin) > 5000 Then ; Do this every 5 secs - the intial 0 value means we do it as sson as we get here the first time
  $mem = MemGetStats()
  GUICtrlSetData($Progress, $mem[0] + 1) ; A trick to get Vista/Win7 progress bars to fill instantly
  GUICtrlSetData($Progress, $mem[0])
  ; Update the label - do not create a new one each time
  GUICtrlSetData($Label1, Round(($mem[1] / 100 * $mem[0]) / 1024) & " MB / " & Round($mem[1] / 1024, 0) & " MB " & " (" & $mem[0] & "% Load)")
  ; Reset the timer
  $iBegin = TimerInit()
EndIf
WEnd
;
Func _ReduceMemory($i_PID = -1)
SplashTextOn("RAM Cleaning", "In progress")
Sleep(1000) ; So you can see the splash screen
If $i_PID - 1 Then
  Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
  $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
  DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
Else
  $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndIf
SplashOff()
Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

    • krasnoshtan
      By krasnoshtan
      Hello forum users!
      At some point I had a need to restart my script. Is it possible to do this with autoit or do I need to connect additional scripts like .cmd or .bat?
      For example: i have only script.au3 and it's running. Wich code shoud i use to exit from the script and start it again?
    • kcvinu
      By kcvinu
      Hi all,
      I have a button created with _GUICtrlButton_Create function. I know that this function returns a handle of button instead of an ID. So i have obtain control ID by calling _WinAPI_GetDlgCtrlID function. After that i have tried the GUICtrlSetBkColor function. But it didnt worked. Any suggestions will be appreciated. Thanks in advance. 
      Note : - The window which carries this button is not created with native gui create function. Instead, it is created with CreateWindowEx api function.
    • tintin92
      By tintin92
      Hello,

      How can I automatically download a list of zip files, like the ones in the quote, using a AutoIt script?
      Is it possible with AutoIt script?
       
    • Robdog1955
      By Robdog1955
      I'm trying to click a button on a web page. I have added a couple of MsgBox lines to allow me to watch what happens on the page. As you can see the first half of my script enters data into text boxes on the page. I have no problem there. I just cannot click on the region buttons. The "set focus" line causes an outline to appear around the EU button and the "click button" line causes the "Pick a Region" text to disappear. Here is the code I have so far.
      #include <IE.au3> Local $oIE = _IECreate("http://questchecker.com/") Local $iQuestID = "123456" Local $sCharacterName = "CharacterName" Local $colForms = _IEFormGetCollection($oIE) $iCount = 0 For $oForm In $colForms $oFormElements = _IEFormElementGetCollection($oForm) For $oFormElement In $oFormElements $iCount = $iCount + 1 Local $sTagName = StringLower($oFormElement.tagName) Local $sElementType = $oFormElement.type Local $sElementName = $oFormElement.name Switch $iCount Case 6 _IEFormElementSetValue($oFormElement, "MyRealm", 0) ; realm Case 7 _IEFormElementSetValue($oFormElement, $sCharacterName, 0) Case 8 _IEFormElementSetValue($oFormElement, $iQuestID, 0) EndSwitch Next Next Local $oButtons = _IEGetObjByName($oIE, "questForm") For $oButton In $oButtons If _IEFormElementGetValue($oButton) = "US" Then MsgBox(0, "", "Click Okay to set focus") _IEAction($oButton, "focus") MsgBox(0, "", "Click Okay to click button") _IEAction($oButton, "click") ExitLoop EndIf Next MsgBox(0, "", "Click Okay to quit") _IEQuit($oIE) Exit  
    • Eggsplorer
      By Eggsplorer
      Hi,
      Ich hab ein Script für Windows Updates, dass nach dem OOBE aufgerufen werden soll. Run/RunOnce ist daher nicht möglich.
      Stattdessen möchte ich das Script in den Autostart schreiben lasse. Wenn die OOBE beendet wird und der Administrator angemeldet wird, soll das Update-Script einmalig aufgerufen werden und sich anschl. selbst löschen.
      Lang rede, kurzer Sinn:
      Wie kann ich folgendes Script in den Autostart schreiben?
      RunWait(@ComSpec & " /c " & "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File C:\UA\Scripts\win-updates.ps1 -MaxUpdatesPerCycle 100 -RestartRequired 1", "") Ich würde ungern eine BAT oder EXE reinkopieren, da ich es unsauber finde. Die müsste ja auch zusätzlich irgendwo liegen.
      Danke
      Eggsplorer
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.