Jump to content
Sign in to follow this  
penTa

Is there a way to monitor if a program is being closed?

Recommended Posts

penTa

Hi all,

I'm really sorry if it is already been asked.

I want to create a program to monitor if a program is being closed. For example, if a notepad is being closed, I want some popup msgbox to come up. Is it possible?

Share this post


Link to post
Share on other sites
BuckMaster

You can either do something like this:

Run("Notepad.exe")
$Exists = 0

While 1
    If WinExists("Untitled -") Then
        $Exists = 1
    Else
        If $Exists = 1 Then
            MsgBox( 0,"Closed","Notepad was closed.")
            $Exists = 0
        EndIf
    EndIf
    Sleep(100);~ to make the CPU usage less
WEnd

or you can do something like this:

Run("Notepad.exe")
Sleep(50) ;~ to make sure that notepad was opened before it checks

WinWaitClose("Untitled -")
MsgBox( 0,"Closed","Notepad was closed.")

Share this post


Link to post
Share on other sites
omikron48

ProcessExists using WinGetProcess result?

Share this post


Link to post
Share on other sites
bo8ster

Hi all,

I'm really sorry if it is already been asked.

I want to create a program to monitor if a program is being closed. For example, if a notepad is being closed, I want some popup msgbox to come up. Is it possible?

The examples given will tell you if the program has been closed. Its a different thing if you want to know if the program is going to be closed.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
penTa

Thanks all. OK, I've got a really bad skill of explaining things. ;)

The situation is let's say we have a program (I'll call it in this "X") that have no function on "confirm on exit" and a lot of users misclicked into close button a lot. The reason I don't want it to be closed is because it take likes hour to startup. So I need to create a script to monitor if this "X" is going to be closed. If any user misclicked at close button, a confirmed box will appeared if he really wanna close.

Sorry for the wrong explaination at first. B)

The examples given will tell you if the program has been closed. Its a different thing if you want to know if the program is going to be closed.

You actually read my mind on the later one. :)

Edited by penTa

Share this post


Link to post
Share on other sites
water

You can get the information that the close button for application "x" has been pressed in an AutoIt script. But then it's too late.

The button has been pressed and application "x" is closing.

What I can think of: Write a script that overlays the close button of "x" with its own button (same screen position and always stays in front). When this button is pressed ask the user if he is sure and then trigger the close button of "x". You will have to write some code that checks when the user moves the window of "x" and you have to disable the windows close icon in the upper right corner.

I think educating the user might be easier.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Melba23

penTa,

Covering the [X] of another app is not that difficult - here is a proof of concept script using Notepad. Try it and see what you think - I have a number of refinements we could put in place if you think this might help. :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$iLast_X = 0
$iLast_Y = 0

Run("Notepad.exe")
WinWaitActive("Untitled - Notepad")

$hNotePad_Handle = WinGetHandle("Untitled - Notepad")

$hGUI = GUICreate("My [X]", 30, 15, -1, -1, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

$hPic = GUICtrlCreatePic("C:\Program Files\AutoIt3\Examples\GUI\Advanced\Images\Red.bmp", 0, 0, 30, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hPic
            MsgBox(0, "Warning", "Someone is trying to exit!")
    EndSwitch

    Local $aNotePad_Pos = WinGetPos($hNotePad_Handle)
    If $aNotePad_Pos[0] <> $iLast_X Or $aNotePad_Pos[1] <> $iLast_Y Then
        $iLast_X = $aNotePad_Pos[0]
        $iLast_Y = $aNotePad_Pos[1]
        WinMove($hGUI, '', $aNotePad_Pos[0] + $aNotePad_Pos[2] - 40, $aNotePad_Pos[1] + 10)
    EndIf

WEnd

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
boogieoompa

Another thing to do is use the MouseSetOnEvent_UDF and basically trap all the primary clicks. Everytime a user clicks the mouse you can have it check the position. Here is a skeletal structure but I didnt test it at all and I did not actually put in a y check or any variance (as is you would have a 1 pixel range to intercept). Hopefully you get the idea.

#include <MouseSetOnEvent_UDF.au3>
#include <GuiConstants.au3>
#include <windowsconstants.au3>

HotKeySet("^q", "_Quit")
$win = "youapptitle"
_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "MousePrimaryDown_Event", "", "", 0, -1)

While 1
    sleep(50)
WEnd



Func MousePrimaryDown_Event()
    If WinExists($win) Then
        $winpos = WinGetPos($win)
        $mousepos = MouseGetPos()
        If $mousepos[0] = $winpos[0]+$winpos[3] Then
            ;trap and do whatever
            Return 1
        Else
            Return 0
        EndIf
        
    Else
        Return 0
    Endif
EndFunc

Func _Quit()
    Exit
EndFunc

MouseSetOnEvent_UDF.au3

Share this post


Link to post
Share on other sites
penTa

You can get the information that the close button for application "x" has been pressed in an AutoIt script. But then it's too late.

The button has been pressed and application "x" is closing.

What I can think of: Write a script that overlays the close button of "x" with its own button (same screen position and always stays in front). When this button is pressed ask the user if he is sure and then trigger the close button of "x". You will have to write some code that checks when the user moves the window of "x" and you have to disable the windows close icon in the upper right corner.

Yup, that's what try at first. It's like it's too late and then msgbox will just come up lol.

I think educating the user might be easier.

Well, I've tried that. But everyone can make mistakes (tho I don't want to happen). It's human's nature ... imperfection.

penTa,

Covering the [X] of another app is not that difficult - here is a proof of concept script using Notepad. Try it and see what you think - I have a number of refinements we could put in place if you think this might help. :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$iLast_X = 0
$iLast_Y = 0

Run("Notepad.exe")
WinWaitActive("Untitled - Notepad")

$hNotePad_Handle = WinGetHandle("Untitled - Notepad")

$hGUI = GUICreate("My [X]", 30, 15, -1, -1, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

$hPic = GUICtrlCreatePic("C:\Program Files\AutoIt3\Examples\GUI\Advanced\Images\Red.bmp", 0, 0, 30, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hPic
            MsgBox(0, "Warning", "Someone is trying to exit!")
    EndSwitch

    Local $aNotePad_Pos = WinGetPos($hNotePad_Handle)
    If $aNotePad_Pos[0] <> $iLast_X Or $aNotePad_Pos[1] <> $iLast_Y Then
        $iLast_X = $aNotePad_Pos[0]
        $iLast_Y = $aNotePad_Pos[1]
        WinMove($hGUI, '', $aNotePad_Pos[0] + $aNotePad_Pos[2] - 40, $aNotePad_Pos[1] + 10)
    EndIf

WEnd

M23

Wow, very nice idea ;) I've tried and liked a lot. Works great, too! Thanks a lot, M23.

Another thing to do is use the MouseSetOnEvent_UDF and basically trap all the primary clicks. Everytime a user clicks the mouse you can have it check the position. Here is a skeletal structure but I didnt test it at all and I did not actually put in a y check or any variance (as is you would have a 1 pixel range to intercept). Hopefully you get the idea.

#include <MouseSetOnEvent_UDF.au3>
#include <GuiConstants.au3>
#include <windowsconstants.au3>

HotKeySet("^q", "_Quit")
$win = "youapptitle"
_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "MousePrimaryDown_Event", "", "", 0, -1)

While 1
    sleep(50)
WEnd



Func MousePrimaryDown_Event()
    If WinExists($win) Then
        $winpos = WinGetPos($win)
        $mousepos = MouseGetPos()
        If $mousepos[0] = $winpos[0]+$winpos[3] Then
            ;trap and do whatever
            Return 1
        Else
            Return 0
        EndIf
        
    Else
        Return 0
    Endif
EndFunc

Func _Quit()
    Exit
EndFunc

Thanks, boogieoompa. I've tried this but it's like the msgbox comes out but the app still exiting . However, the MouseSetOnEvent is so much interesting. I'll tried to looked into this more. B)

Share this post


Link to post
Share on other sites
boogieoompa

Well my sample code is probably wrong since I typed it directly into the reply field but it should work in theory since you intercept ALL mouse signals. Good luck

Share this post


Link to post
Share on other sites
bo8ster

Well, when they make a mistake, get them to make everyone coffee - ppl learn after that.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

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  

×