Date: 02-16-2022
Return to Index
created by gbSnippets
'A Window receives the WM_SysCommand message when the user chooses a command
'from the Window Menu (formerly known as the System or Control Menu) or when the
'the user chooses the maximize button, minimize button, restore button, or close button.
'So, when the WindowMenu/Close or Close Button ( "X") is selected by the user to close the application,
'the WM_SysCommand message can be used to ask the user to confirm the selection
'WM_SysCommand is also sent by Windows in response to the Alt-F4 keyboard combination, which
'is also used to close an application.
'In WM_SysCommand, return 0 to go ahead with closing the application, or return 1 to
'cancel closure and return to the application.
'If 0 is returned form WM_SysCommand, the WM_Destroy message will be sent by
'Windows. Take closing actions within WM_Destroy (save settings, etc.).
'Example#1 - OK to continue with closing. Cancel to continue running the application
Case %WM_SYSCOMMAND
Local Style&
If (CB.wParam AND &HFFF0) = %SC_Close Then 'trap Alt-F4, X-button, WindowMenu/Close
Style& = %MB_OKCancel Or %MB_IconQuestion Or %MB_TaskModal
Select Case MsgBox("Are you sure?", Style&, "Close Applicaton")
Case %IdOK
' ... take action before closing the application
Function = 0 'False - go ahead with closing the application
Case %IdCancel
Function = 1 'True - abort the close - no further processing needed
End Select
Case %WM_Destroy
' ...take actions, such as storing settings, unregister hotkeys, ...
End If
'Example#2 - YES to save data then close, NO to not save data then close, CANCEL to continue running the application
Case %WM_SYSCOMMAND
Local Style&
If (CB.wParam AND &HFFF0) = %SC_Close Then 'traps Alt-F4, X-button, WindowMenu/Close
Style& = %MB_YesNoCancel Or %MB_IconQuestion Or %MB_TaskModal
Select Case MsgBox("Save Setting Before Closing?", Style&, "Save Settings")
Case %IdYes
' ... take action before closing the application
Function = 0 'False - go ahead with closing the application
Case %IdNo
Function = 0 'Not Save, then destroy
Case %IdCancel
Function = 1 'True - abort the close - no further processing needed
End Select
End If
Case %WM_Destroy
' ...take actions, such as storing settings, unregister hotkeys, ...
'Compilable Example: (Jose Includes)
'run and compile, then press Alt-F4 or X-button to close
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_SYSCOMMAND
Local Style&
If (CB.wParam AND &HFFF0) = %SC_Close Then 'trap Alt-F4, X-button, WindowMenu/Close
Style& = %MB_YesNoCancel Or %MB_IconQuestion Or %MB_TaskModal
Select Case MsgBox("Save Setting Before Closing?", Style&, "Save Settings")
Case %IdYes
' ... take action before closing the application
Function = 0 'False - go ahead with closing the application
Case %IdNo
Function = 0 'False - go ahead with closing the application
Case %IdCancel
Function = 1 'True - abort the close - no further processing needed
End Select
End If
Case %WM_Close
MsgBox "Close" 'included as a reminder the WM_Close is sent before WM_Destroy
Case %WM_Destroy
MsgBox "Destroy" 'put your closing code here (save settings, etc.)
End Select
End Function
'gbs_00037
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm