Date: 02-16-2022
Return to Index
created by gbSnippets
'When ESC is pressed on a dialog, the %IDCancel notification
'message is sent to the Callback function.
'When ENTER is pressed on a dialog, the %IDOK notification
'message is sent to the Callback function.
'Programmers often give a button a control ID of %IDOK or
'%IDCancel to have the button represent the corresponding
'ENTER or ESC behavior.
'See the next snippet for exceptions to this behavior.
'Primary Code:
'Create a button with the control ID of %IDOK.
Control Add Button, hDlg, %IDOK,"OK", 20,20,70,20
'Pressing ENTER will trigger the %IDOK notification event
If CB.Msg = %WM_Command AND CB.Ctl = %IDOK AND CB.Ctlmsg = %BN_Clicked Then
'...this code triggered by ENTER
End If
'Pressing ESC will trigger the %IDCancel notification event
If CB.Msg = %WM_Command AND CB.Ctl = %IDCancel AND CB.Ctlmsg = %BN_Clicked Then
'...this code triggered by ESC
End If
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
'In this example, the %IDOK amd %IDCancel buttons are by themselves
'on a dialog. Press a button, ENTER, or ESC to send notification messages.
#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
Control Add Button, hDlg, %IDOK,"OK", 20,20,70,20
Control Add Button, hDlg, %IDCancel,"Cancel", 110,20,70,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = %IDOK AND CB.Ctlmsg = %BN_Clicked Then
MsgBox "OK Button or Enter Pressed" '...this code triggered by ENTER
End If
If CB.Msg = %WM_Command AND CB.Ctl = %IDCancel AND CB.Ctlmsg = %BN_Clicked Then
MsgBox "Cancel Button or ESC Pressed" '...this code triggered by ESC
End If
End Function
'gbs_00186
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm