The two responses I got to a previous question were not too helpful, primarily because of my "knowledge level" (read "ignorance"). So forgive me if I ask again:
In my QuickWin application I have a callback routine used when the user selects File > Exit from the program's menu. I use this to remind the user to save files if needed. Works just fine.
However when the user clicks on the Title Bar X, or shuts down the computer from the task bar, my routine is bypassed, and the program shuts down with out giving the user the option of saving files!
How does one direct Title bar x or computer shutdown to transfer control to my routine??
In my QuickWin application I have a callback routine used when the user selects File > Exit from the program's menu. I use this to remind the user to save files if needed. Works just fine.
However when the user clicks on the Title Bar X, or shuts down the computer from the task bar, my routine is bypassed, and the program shuts down with out giving the user the option of saving files!
How does one direct Title bar x or computer shutdown to transfer control to my routine??
链接已复制
9 回复数
You need to handle a WM_CLOSE on the frame window. Here is a simple example:
program simple use dflib use user32 use kernel32 integer(LONG) f common /wininfo/ f external CatchClose !DEC$ attributes stdcall :: CatchClose print *, "Hello" f = SetWindowLong (GETHWNDQQ(QWIN$FRAMEWINDOW), GWL_WNDPROC, LOC(CatchClose)) if (f == 0) print *, "Error - unable to install close handler" pause end integer(4) function CatchClose (hwnd, msg, wparam, lparam) !DEC$ attributes stdcall :: CatchClose use user32 integer(4) hwnd, msg, wparam, lparam integer(LONG) f common /wininfo/ f if (msg == WM_CLOSE) then call MessageBox (NULL, "Put your close code here"c, "Closing..."c, MB_OK) end if CatchClose = CallWindowProc (f, hwnd, msg, wparam, lparam) return endJames
Very Good!!
The post by James indeed works if the user calls for an exit via the X on the title bar. THANKS
BUT If the user does a computer shutdown the "save File?" warning is not invoked. This is not a show stopper but it would be nice if the Shutdown were trapped. Any suggestions?
PAUL
The post by James indeed works if the user calls for an exit via the X on the title bar. THANKS
BUT If the user does a computer shutdown the "save File?" warning is not invoked. This is not a show stopper but it would be nice if the Shutdown were trapped. Any suggestions?
PAUL
Paul,
You can catch a shutdown by adding something like
You can also "vote down" the shutdown to make sure you get your question answered, then either allow the user to re-initiate shutdown manually or do a shutdown yourself. Depends on how important you think your interaction with the user is.
James
You can catch a shutdown by adding something like
else if (msg == WM_ENDSESSION) then call MessageBox (NULL, "Put your end session code here"c, "Closing..."c, MB_OK)after the WM_CLOSE branch in the example I provided. However note that Windows will bring up the "Program is not responding" message if you don't complete whatever you are doing within a few seconds, not wonderful for user input.
You can also "vote down" the shutdown to make sure you get your question answered, then either allow the user to re-initiate shutdown manually or do a shutdown yourself. Depends on how important you think your interaction with the user is.
James
I think WM_QUERYENDSESSION is the better message to handle -- it is sent before WM_ENDSESSION to "ask the application about its opinion" (the WndProc should return 1 if it agrees). However, I didn't verify whether it has timeout period.
Jugoslav
Jugoslav
