|
Post by larrybtoys on Nov 29, 2023 15:20:51 GMT 1
What is the easiest way to simply monitor a window and end the program whenever the window has been closed without redesigning all my programs?
Procedure Allow_Input
Clr Position_Selected_X&, Position_Selected_Y&
'
PeekEvent
(If WINDOW has been closed by hitting the X-OUT button Stop all sounds and END the program) '
Mouse x&, y&, k&
'
If k& = 1
Mouse_Button_Pressed& = 1
Position_Selected_X& = x&
Position_Selected_Y& = y&
EndIf
'
EndProcedure
|
|
|
Post by dragonjim on Nov 29, 2023 15:27:35 GMT 1
Would a variant of the code below be useful?
Local i$ OpenW 1 // The window to monitor
Ocx Command cmd = "Close", 10, 10, 200, 20 Ocx Label lbl = "", 10, 40, 400, 15
Do i$ = InKey$ If i$ <> "" Then lbl.Text = lbl.Text & i$ DoEvents Until IsNothing(Win_1)
Sub cmd_Click CloseW 1 EndSub
The bit you would be interested is the Do...Until loop
|
|
|
Post by larrybtoys on Nov 29, 2023 16:41:42 GMT 1
That would not work without major re-writes.
I need to simply monitor a window and do something if it is closed.
PeekEvent If Window# 1 is no longer open End Program Endif
This code I would be able to insert into my normal code to see if the user closed the window
|
|
webu
Full Member
Posts: 149
|
Post by webu on Nov 29, 2023 17:46:38 GMT 1
PeekEvent is a bad idea. Check system-events in sytem-functions Better: OpenW 1 Do : Sleep : Until Me Is Nothing
Sub Win_1_Close(Cancel?) If MsgBox("You want to close window 1?", MB_OKCANCEL, "") = IDOK Message "Ok, you want to close! I will do something and than will close the window..." Print "Cancel? = "; Cancel? Print "do something ..." Print "do something ..." Print "do something ..." Print "do something ..." Delay 2 // wait two seconds Return // Cancel? is not true, so close the window! EndIf Cancel? = True // Cancel? is now true, so do NOT close the window!! You know the window-number? Is it 1? than you can use this example.
In the middle of your code also will work:
If IsNothing(Win_1) End
|
|
|
Post by larrybtoys on Nov 29, 2023 21:16:11 GMT 1
My Window is # 1 but this code still does not close the program out.
PeekEvent If IsNothing(Win_1) Mci "close all" End EndIf
|
|
|
Post by (X) on Nov 29, 2023 21:21:06 GMT 1
I think the 'End' statement just ends the program un-ceremoniously, and kicks you out entirely. The 'Win1_Close' subroutine should be called by GFA automatically when the window Win1 is closed or set to Nothing - if you create Sub Win1_Close(Cancel?) - you can close the MCI channels there. Sub Win1_Close(Cancel?) MCI "close all" EndSub
There is also a rather 'new' App_Close - if the Sub routine is created by the coder - it should be called by GFA before the app goes to the back of beyond.
See: App_Close event in the Help Doc. Requires: GfaWinx.lg32
|
|
|
Post by larrybtoys on Nov 29, 2023 22:24:48 GMT 1
I think what is happening is that when the user hits the X button to close the program without going through the menu screen, Windows does indeed close the window however another window of a generic size opens up to continue running the program. I believe GFA is calling this window # 1 so the code of the game still continues to operate in the newly created oddball window. When I changed my standard window from # 1 to to # 11 throughout the program, hitting the X button in the non-compiled version still did not work however the compiled version closed out the game as hoped.
|
|
|
Post by (X) on Nov 29, 2023 22:56:07 GMT 1
Do you have multiple windows opened?
|
|
|
Post by larrybtoys on Nov 29, 2023 23:29:05 GMT 1
No, not normally. They can choose an option that will open another window for a moment but the game can't go on until they close that window. There is only 1 window opened during normal game play. I can remove the X from the upper right corner preventing them from closing the window without using the menu however I still wanted to have the program end properly even if they did hit the Windows X. This is the code I ended up using once I switched my game to use window # 2 for it's main screen.
PeekEvent If IsNothing(Win_2) Or MENU(1) = 4 Mci "close all" ShowM GraphMode 1 EndIf
|
|
|
Post by dragonjim on Nov 29, 2023 23:29:28 GMT 1
I'm not 100% sure I understand the problem you are having but it sounds like it is possible in your program to shortcut the closing of the program by hitting the X (close) button on the game window, thus missing out the menu screen. Do you display the menu screen in the same window that has been closed? This would explain the 'generic size' window appearing as GFA automatically creates a window if a program tries to output something and Me is Nothing (or there is no open window available to receive it).
If that is the case, then the easiest solution would be to nullify the closing of the Window (#1) by using the event procedure mentioned by X like so:
Sub Win_1_Close(Cancel?) Message "Please close the program properly" Cancel? = True EndSub This would force the user to go through the closing down process which you have programmed.
You could also just disable the close button like so:
OpenW 1 Trace DisableCloseButton(Win_1)
Declare Function GetSystemMenu Lib "user32" _ (ByVal hWnd As Long, _ ByVal bRevert As Long) As Long
Declare Function RemoveMenu Lib "user32" _ (ByVal hMenu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long) As Long
Const MF_BYPOSITION = &H400
Function DisableCloseButton(frm As Form) As Boolean Dim lHndSysMenu As Long Dim lAns1 As Long, lAns2 As Long lHndSysMenu = GetSystemMenu(frm.hWnd, 0) lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION) lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION) DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0) End Function [Courtesy of VBForums]
However, this will still not catch if the window is closed using Alt-F4 or through the taskbar - in both these cases the only way to determine whether the window has been closed is to use the OnAppClose functionality (also mentioned by X - see the example below from the help file).
$Library "gfawinx" $Library "UpdateRT" UpdateRuntime ' Patches GfaWin23.Ocx
OnAppClose ProcAddr(App_IsClosing) OpenW 0, 50, 50, _X / 2, _Y / 2 Do Sleep Until Me Is Nothing
Sub App_IsClosing() ' new name, executed when app closes MsgBox "Free resources" CloseW 0 ' if not terminated normally EndSub
$Group "Window Events”" Sub Win_0_Click ' Genereate some error to abruptly terminate the program. ' Note that App_Close is called. Error 3 EndSub
You could use this to run any close down code that you need in the event of unusual termination (Alt-F4 or closed from the Taskbar) or the Close event procedure to keep the window open long enough to process your close down code to prevent the orphan 'generic sized' window appearing.
I hope some of this is helpful.
|
|
|
Post by larrybtoys on Nov 30, 2023 0:41:51 GMT 1
I think you are right. I have other programs that will close fine even when using the Windows X option however they are not outputting anything. Many of my games will cause the odd size window to open in order to continue the game. GFA or Windows must be assigning it # 1. I think I will simply remove the X option from the window forcing the player to use the normal program ending option, make my main window # 2 and still monitor window # 2 for closure to end the program/game my way if it no longer sees window # 2. I'm covering all my bases.
|
|