Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Detecting if window gets closed when clicking red X.

Author
Message
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 4th Jul 2013 21:26 Edited at: 4th Jul 2013 21:29
Is there a way or a function for detecting if the window / program gets closed.

Example, if someone clicks the RED X on the top right of the window, I noticed that nothing further in my program works. Meaning, it will not carry out any further functions. I need to be able to update my INI file when the program ends. I do not want to have to take that red X option out, so is there a way to detect it if the windows gets closed because of that red X ?

EDIT UPDATE : Its not a red X, but the button in windows 7 is red with a White X.. but I said red because it came to my mind as the first color. But you should still get my meaning.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Jul 2013 21:56
Why not use your own button? Then you can add any code you like.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Jul 2013 22:22 Edited at: 4th Jul 2013 22:24
[edit] Late after GG - the is a more complex way not as neat and clean as GG's - but it may still be of interest.


Hello,

I know it can be done through a DLL. Check out the following code:



You see this block:



When your app ends and the process is detached - basically the DLL is done and being let go from your program - it'll run whatever code you place here on this event.

In the case of your ini, you could have code written to it at this stage. You could even have another EXE called that does something.

I'll attach the compiled dll that displays a message box when you leave the program. The code below loads in the DLL and then on close, creates the message box.




The idea would be for you to make your own dll that reacts to the detached process message. By using the LOAD DLL command, this activates the dll within your program. When your program closes, the dll is released thus causing the code to run.

For your ini, you could either have the dll call another exe that does the updating of your ini, or you code the updates yourself.

The other way, I think is to subclass your DB window. This may be a little more complex than running the DLL. However, you may need a DLL to actually subclass your window. Subclassing creates a means to intercept messages that normally go to the main window procedure of your window. This allows you to handle messages anyway you want - a message being some command or service related to the current window.

But there may be simpler methods within DB itself.

Enjoy your day.
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 4th Jul 2013 23:29
you can do it with bbb gui dll

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 4th Jul 2013 23:44 Edited at: 4th Jul 2013 23:45
I think this might be what you're after, but you'll need IanM's Matrix1 Utilites installed. The key to understanding this procedure is to research the "Set Message Callback" command along with the function that it calls. There are many call back messages too, search for these on the net. I've included the Windows Message Close id, 0x0010.



Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 5th Jul 2013 00:10 Edited at: 5th Jul 2013 01:02
Quote: "Why not use your own button? Then you can add any code you like."


Well I have. but the point is, if they hit the "white" X ( the red button on the top right of your window ) then I need a way to find out if they clicked that so that I can trap that and execute code I want to run. If I don't trap it, then they can loose data that is in current memory and I need that data to be saved to an INI file.

Quote: "you can do it with bbb gui dll"


I don't want to use another plugin. But thanks.


Quote: "I think this might be what you're after, but you'll need IanM's Matrix1 Utilites installed. The key to understanding this procedure is to research the "Set Message Callback" command along with the function that it calls. There are many call back messages too, search for these on the net. I've included the Windows Message Close id, 0x0010."


Your code is EXACTLY what I wanted. But, its crashing on startup. It doesn't like that code. So I am going to see what I can find out about it and hopefully get it fixed. I'll post it if I get it running.



@Latch, your DLL works. I have to figure out how to get it to send a command back to DBPro so I can call my own function inside of my DBPro compiled program. Time for me to learn my old school Win32 API programming days. Sheesh its been a while. Whats interesting is Burning Feet Man's code is what I am after. But it crashes the program at startup with his code. So I am trying to solve this with his and your code combined.
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 5th Jul 2013 02:51 Edited at: 5th Jul 2013 02:58
Well after a few hours spending time on this I got it to work.

If it wasn't for Charlze Petziod - Programming Windows 5th Edition sitting here on my office desk, I do not think I would have figured this out. GREAT BOOK.

The weird part is, I needed "restart coroutine 1" in there under the IF --> ENDIF section. WHY ? I have no idea. But it won't work without it in there. I also needed stop current message in there as well.

Thank you all for the help.

Here is the final code.

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 5th Jul 2013 03:26
In further testing, although I can't get it to crash as per your experience, when I tidy up my code and make it more realistic, using SYNC commands etc, I can't get the screen to sync/update upon calling the "End Program" routine. The only exception is if I adjust the Window location whilst the "End Program" routine is running, this then updates the screen...

The same problem occurs with your current example Jack. The screen doesn't update, BUT, the rest of the "End Program" routine executes, but you just can't see any messages printed.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 5th Jul 2013 04:26 Edited at: 5th Jul 2013 05:14
In your case you might need to put two syncs there in the end program routine. On my screen it works fine with one sync command, but I have read reports that with some video cards that you need two.

EDIT UPDATE 1 : I just tested it on my NVidia card and your right, it didn't work. It works great on INTEL and AMD video cards as per my past tests. But again, NVidia drops the ball.

EDIT UPDATE 2 : I figured out the first stop current message is needed, but the one below it is not.

EDIT UPDATE 3 : OK, I was reading here on this site http://msdn.microsoft.com/en-us/library/windows/desktop/ff381396%28v=vs.85%29.aspx and it explains why. IT destroys the window when you click that X. Some computers allow you to see the messages, some do not. This is how the OS works I guess. Read that site it talks about DestroyWindow(0) command. So yea, I would say its pretty weird that it works on some computers and not on others.

EDIT UPDATE 4 :



I took this out just to see and sure enough my code works without this. BUT.. it ONLY works if I use it on the computer that can't print the text. The other computer that CAN SEE the text, I have to have this in there. Figure this one out. It's confusing to be honest.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 7th Jul 2013 12:09
@JD

I was messing around with the DLL and got it to dump a memblock to a file when the app is closed, but for some weird reason, the first 8 bytes get scrambled. But, back to the idea of maintaining control of your own app, you can deactivate the close button to remove that option from the user:



Enjoy your day.
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Jul 2013 16:20 Edited at: 7th Jul 2013 16:29
Quote: " But, back to the idea of maintaining control of your own app, you can deactivate the close button to remove that option from the user"


Hey that could come in handy sometime. I need the close button to be there, but I might need that for some other project. Thanks for that.

Quote: "I was messing around with the DLL and got it to dump a memblock to a file when the app is closed, but for some weird reason, the first 8 bytes get scrambled."


Hmmm, yea I am not having that trouble. In fact it's saving my INI as it should be. Its been working. Crazy as it seems. Also, I found that I do not need that extra bit of code I had in there. So here is the updated code with bare minimum of code needed.



the line :

sleep 1000

is not needed. But its there so you can see the delay before the window closes .. thus showing that that function is getting called. Compare that with hitting escape and you'll see the difference in speed when it comes to closing the window.

https://soundcloud.com/toraktu
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 7th Jul 2013 17:37
Quote: "...back to the idea of maintaining control of your own app, you can deactivate the close button to remove that option from the user:"


If its a single windowed video game, then this is a terrible thing to do. Sure it helps the problem being discussed partially but it causes a bunch of other problems.

The game should maintain consistent windows application behavior. Having a window with no close button is a sophisticated but ill conceived strategy.

If there were several windows (or forms if you prefer) then then removing the close button from only some of them would be entirely realistic.

Given we are talking about PC games, the button should be there. The game should detect the button, and handle a quick and neat shutdown. You can sometimes get away with a confirmation prompt, particularly if the game is entirely windowed and not full screen in any shape or form.

Hide Window
I will point out one detail nobody mentioned. It's preferable to hide the window first before before performing any sort of shutdown 'work'. Say if your game tells a server you are disconnecting, or quickly saves progress. Hiding the window will give the player a better impression of performance, and provides better flow. Nobody wants to sit there for several seconds or more watching a frozen app churn away when they want it turned off.

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 7th Jul 2013 20:44
Quote: "Having a window with no close button is a sophisticated but ill conceived strategy.
"


Ill conceived? You're funny.

Enjoy your day.
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Jul 2013 21:21
Quote: "Hide Window
I will point out one detail nobody mentioned. It's preferable to hide the window first before before performing any sort of shutdown 'work'. Say if your game tells a server you are disconnecting, or quickly saves progress. Hiding the window will give the player a better impression of performance, and provides better flow. Nobody wants to sit there for several seconds or more watching a frozen app churn away when they want it turned off."


Thanks for this tip. I would not have thought of that.

https://soundcloud.com/toraktu
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 7th Jul 2013 21:31
Turning the button off like that certainly takes programming skill to think up and implement. Though the general idea of turning the button off in the first place isn't that good an idea where single window of games are concerned.

A program should follow the user interface scheme of the operating system. Disabling the close button on the sole active window is just the wrong way to approach the problem.

Could you imagine if every windowed app strictly enforced its own method to shutdown? It would be a bloody inconsistent mess.

I want to give you credit here since this is a more advanced technique. However presentation and user experience are a different matter from programmer skill.

JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 20th Jul 2013 14:39 Edited at: 20th Jul 2013 14:43
After a few problems I kept running into, I realized that different windows reacted differently to how that X gets closed. So I finally had to redo the code a little bit. I had mentioned this code earlier in my postings, but when I had posted my last message I didn't include everything.

Turns out that this code

if whatevervariableyouwanthere = 1
restart coroutine 1
endif

is needed in the loop. The rn variable in the code is needed if you add other routines. So I am keeping that in there as well. I am starting to think that there is a difference between the 32Bit and 64Bit windows when it comes to the WM_CLOSE and how windows handles messages with this.

So here is the final code for those who want it. It works in XP and higher now.

The ini file is not needed, but I threw that in there because it goes to show that it will create that ONLY if you hit the X. It basically just proves that it is working.



https://soundcloud.com/toraktu

Login to post a reply

Server time is: 2026-07-07 02:39:50
Your offset time is: 2026-07-07 02:39:50