Jump to content

Help to get a script to run on button click


Recommended Posts

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?!

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

×
×
  • Create New...