A very sad day for the civilized world, but still we must try to go on..
Writing own button has thaught me a lot about standard ones. Found a bit
dangerous thing about standard buttons that may be good to know: If you
use the arrow keys to move between them and press Enter when a button has
focus, you may trigger wrong button!
Don't know what MS have been thinking. If to use Tab key, no problems. Right
button will be triggered. The sample code below shows. Compile and start,
then press Right arrow to give Cancel button focus and press Enter. MSGBOX
will tell Cancel button was triggered. Okay, but repeat: when Msgbox is
closed, OK button will get focus again. Press Right arrow once more to
give Cancel focus and press Enter. MSGBOX tells OK button has been triggered!!!
Imagine question was "Format drive C?" ..
Simple fix: Don't look for CBCTL (LOWRD(wParam)) in WM_COMMAND. Instead use
GetDlgCtrlID(GetFocus). See sample below and test the different ways. Just
tell it, so you know that just because an MS button has focus, it doesn't
mean it will be triggered if user presses Enter..
------------------
Writing own button has thaught me a lot about standard ones. Found a bit
dangerous thing about standard buttons that may be good to know: If you
use the arrow keys to move between them and press Enter when a button has
focus, you may trigger wrong button!
Don't know what MS have been thinking. If to use Tab key, no problems. Right
button will be triggered. The sample code below shows. Compile and start,
then press Right arrow to give Cancel button focus and press Enter. MSGBOX
will tell Cancel button was triggered. Okay, but repeat: when Msgbox is
closed, OK button will get focus again. Press Right arrow once more to
give Cancel focus and press Enter. MSGBOX tells OK button has been triggered!!!
Imagine question was "Format drive C?" ..
Simple fix: Don't look for CBCTL (LOWRD(wParam)) in WM_COMMAND. Instead use
GetDlgCtrlID(GetFocus). See sample below and test the different ways. Just
tell it, so you know that just because an MS button has focus, it doesn't
mean it will be triggered if user presses Enter..
Code:
#COMPILE EXE #INCLUDE "WIN32API.INC" DECLARE CALLBACK FUNCTION DlgMainProc '******************************************************************** ' Main entrance - create dialog and controls '******************************************************************** FUNCTION WINMAIN (BYVAL hInst AS LONG, BYVAL hPrevInstance AS LONG, _ lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG LOCAL hDlg AS LONG DIALOG NEW 0, "Press Right arrow, then Enter - repeat",,, 200, 100, %WS_SYSMENU TO hDlg CONTROL ADD BUTTON, hDlg, %IDOK, "ID&OK", 4, 4, 50, 14 CONTROL ADD BUTTON, hDlg, %IDCANCEL, "ID&CANCEL", 54, 4, 50, 14 DIALOG SHOW MODAL hDlg, CALL DlgMainProc END FUNCTION '******************************************************************** ' Main message handling procedure '******************************************************************** CALLBACK FUNCTION DlgMainProc SELECT CASE CBMSG CASE %WM_COMMAND IF CBCTLMSG = %BN_CLICKED THEN SELECT CASE CBCTL 'CASE GetDlgCtrlID(GetFocus) fixes problem! CASE %IDCANCEL : Msgbox "%IDCANCEL" CASE %IDOK : Msgbox "%IDOK" END SELECT END IF END SELECT END FUNCTION
------------------
Comment