• Welcome to PlanetSquires Forums.
 

WM_CLOSE - Close Window Yes or No

Started by Noble Bell, January 01, 2010, 11:55:16 PM

Previous topic - Next topic

Noble Bell

Does anyone have an example of the following:

I would like to put some code in the WM_CLOSE event that asks the user are they sure they want to exit the program. If they answer yes then it will close the window. If they answer no then the event cancels and the user can continue with the open window.

I have tried this and I cannot make the window stay open if the user selects no.

Thoughts?

Paul Squires

Hi Noble,

Try something like the following:


Function FORM2_WM_CLOSE ( _
                        hWndForm As Dword _  ' handle of Form
                        ) As Long

   Local nResult As Long
   
   nResult = MessageBox( hWndForm, "Exit the Form?", "Confirm", %MB_ICONQUESTION Or %MB_YESNO )
   
   If nResult = %IDNO Then
      Function = %TRUE
   End If
   
End Function

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Noble Bell

That did the trick.

I think I got it. Because the WM_CLOSE function returns a zero if the message is processed (thus close the window) we return back a true (non-zero value) which forces the message to not be processed and let the user continue using the form.

Roger Garstang

I haven't tested/verified it with FF3, but I know in FF2 there was internal code that forwarded the other message fired to WM_CLOSE too, so if this made it on into FF3 then only one place is needed to check for all possible ways of closing a window (aside from End Task of course which doesn't send a message).

Only thing missing is an easy way of processing the QueryEndSession message since it expects a return value backwards from the rest.  I have one app I did it by adding some custom code to the message loop / pump hook in FF, but a cleaner way would be much smoother.  Perhaps another special function or message?