This is a non-documented feature but in a modal dialog box (popup), the top-right button to close the popup is assigned the F12
key, so you must assign this key to a function code and process it as any other function code.
Step-by-step procedure:
1) Create the ABAP program (transaction code SE38
or SE80
)
REPORT.
CALL SCREEN 100 STARTING AT 10 10 ENDING AT 60 20.
MODULE status_0100 OUTPUT. " <=== called "before output"
SET PF-STATUS '0100'. " <=== choose the GUI status
ENDMODULE.
MODULE user_command_0100 INPUT. " <=== called "after input" (after user action)
IF sy-ucomm = 'CANCEL'. " <=== the function code you chose in your GUI status
SET SCREEN 0. " <=== 0 is a special number which ends "CALL SCREEN"
ENDIF.
ENDMODULE.
Note: SET SCREEN 0
is to close the dialog box (0
means "the current dynpro sequence is ended") ; if you have a complex screen you may also use LEAVE TO SCREEN
(which is equivalent to the 2 statements SET SCREEN + LEAVE SCREEN).
2) Create the screen 0100 (transaction code SE51
or double-click 0100
behind CALL SCREEN
)
Screen type: modal dialog box
Flow logic:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
PROCESS AFTER INPUT.
MODULE user_command_0100.
3) Create the GUI status 0100 (transaction code SE41
or double-click 0100
behind SET PF-STATUS
)
Status type: dialog box
Assign the F12 key to an arbitrary function code (I chose the name CANCEL
), and activate this function code (button "Function Code"):

4) Test
Run the program, you may now click the top-right button (or press F12 if you prefer) which closes the modal dialog box: