Main program:
REPORT ztest_pillar.
*----------------------------------------------------------------------*
* CLASS event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS event_receiver DEFINITION.
PUBLIC SECTION.
METHODS event_handler_picture_dblclick
FOR EVENT picture_dblclick OF cl_gui_picture
IMPORTING mouse_pos_x mouse_pos_y sender.
METHODS event_handler_context_menu
FOR EVENT context_menu OF cl_gui_picture
IMPORTING sender.
METHODS event_handler_context_menu_sel
FOR EVENT context_menu_selected OF cl_gui_picture
IMPORTING fcode sender.
ENDCLASS. "event_receiver DEFINITION
TYPE-POOLS: cntl.
DATA gt_event_tab TYPE cntl_simple_events.
DATA gs_event_tab TYPE LINE OF cntl_simple_events.
DATA g_url TYPE c LENGTH 128
VALUE 'HTTP://QQKONGJIANLIUYANDAIMA.COM/UPLOADS/ALLIMG/101222/1-1012221u100.JPG'.
DATA ok_code TYPE sy-ucomm.
DATA gctn_pic TYPE REF TO cl_gui_custom_container.
DATA gct_pic TYPE REF TO cl_gui_picture.
DATA go_receiver TYPE REF TO event_receiver.
DATA g_result TYPE i.
START-OF-SELECTION.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS001'.
SET TITLEBAR '001'.
* Initial container and picture control--------------------------------
IF gctn_pic IS INITIAL.
* g_url = 'HTTP://PIC4.NIPIC.COM/20090727/3119090_024047072_2.JPG'.
* Create container instance
CREATE OBJECT gctn_pic
EXPORTING
container_name = 'PIC_CT'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
IF gct_pic IS INITIAL.
* Create picture control instance
CREATE OBJECT gct_pic
EXPORTING
parent = gctn_pic
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
* Register event--------------------------------------------------------
* internal table for events
gs_event_tab-eventid = cl_gui_picture=>eventid_picture_dblclick.
APPEND gs_event_tab TO gt_event_tab.
gs_event_tab-eventid = cl_gui_picture=>eventid_context_menu.
APPEND gs_event_tab TO gt_event_tab.
gs_event_tab-eventid = cl_gui_picture=>eventid_context_menu_selected.
APPEND gs_event_tab TO gt_event_tab.
* register events for picture control
CALL METHOD gct_pic->set_registered_events
EXPORTING
events = gt_event_tab
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
illegal_event_combination = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Specify handler---------------------------------------------------
CREATE OBJECT go_receiver.
SET HANDLER go_receiver->event_handler_picture_dblclick FOR gct_pic.
SET HANDLER go_receiver->event_handler_context_menu FOR gct_pic.
SET HANDLER go_receiver->event_handler_context_menu_sel FOR gct_pic.
* Initial picture
CALL METHOD gct_pic->load_picture_from_url
EXPORTING
url = g_url
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*----------------------------------------------------------------------*
* CLASS event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS event_receiver IMPLEMENTATION.
* double click event handler
METHOD event_handler_picture_dblclick.
MESSAGE i000(ok) WITH 'You click at pot(' mouse_pos_x mouse_pos_y ')'.
ENDMETHOD. "event_handler_picture_dblclick
* right click event handler
METHOD event_handler_context_menu.
DATA lo_menu TYPE REF TO cl_ctmenu.
CREATE OBJECT lo_menu.
CALL METHOD lo_menu->add_function
EXPORTING
fcode = 'NM'
text = 'Normal'.
CALL METHOD lo_menu->add_function
EXPORTING
fcode = 'STT'
text = 'Stretch'.
CALL METHOD lo_menu->add_function
EXPORTING
fcode = 'FT'
text = 'Fit'.
CALL METHOD lo_menu->add_function
EXPORTING
fcode = 'NMCT'
text = 'Normal/centered'.
CALL METHOD lo_menu->add_function
EXPORTING
fcode = 'FTCT'
text = 'Fit and centered'.
* Add menu to picture
CALL METHOD sender->display_context_menu
EXPORTING
context_menu = lo_menu
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDMETHOD. "event_handler_context_menu
* Define handler for selecting one value in menu
METHOD event_handler_context_menu_sel.
DATA l_display_mode TYPE i.
CASE fcode.
WHEN 'NM'.
l_display_mode = cl_gui_picture=>display_mode_normal.
WHEN 'STT'.
l_display_mode = cl_gui_picture=>display_mode_stretch.
WHEN 'FT'.
l_display_mode = cl_gui_picture=>display_mode_fit.
WHEN 'NMCT'.
l_display_mode = cl_gui_picture=>display_mode_normal_center.
WHEN 'FTCT'.
l_display_mode = cl_gui_picture=>display_mode_fit_center.
WHEN OTHERS.
EXIT.
ENDCASE.
CALL METHOD sender->set_display_mode
EXPORTING
display_mode = l_display_mode
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDMETHOD. "event_handler_context_menu_sel
ENDCLASS. "event_receiver IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
DATA ok_save TYPE sy-ucomm.
DATA l_display_mode TYPE i.
ok_save = ok_code.
CLEAR ok_code.
CASE ok_save.
WHEN 'NM'.
l_display_mode = cl_gui_picture=>display_mode_normal.
CALL METHOD gct_pic->set_display_mode
EXPORTING
display_mode = l_display_mode
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'FT'.
l_display_mode = cl_gui_picture=>display_mode_fit.
CALL METHOD gct_pic->set_display_mode
EXPORTING
display_mode = l_display_mode
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'NMCT'.
l_display_mode = cl_gui_picture=>display_mode_normal_center.
CALL METHOD gct_pic->set_display_mode
EXPORTING
display_mode = l_display_mode
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'FTCT'.
l_display_mode = cl_gui_picture=>display_mode_fit_center.
CALL METHOD gct_pic->set_display_mode
EXPORTING
display_mode = l_display_mode
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'DLT'.
CALL METHOD gct_pic->clear_picture
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
WHEN 'PBAK'.
CALL METHOD gct_pic->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
SET SCREEN 0.
WHEN 'PEXIT'.
CALL METHOD gct_pic->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
LEAVE PROGRAM.
WHEN 'PCF'.
CALL METHOD gct_pic->load_picture_from_url
EXPORTING
url = g_url
IMPORTING
result = g_result
EXCEPTIONS
error = 1
others = 2
.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
IF g_result = 0.
MESSAGE e000(OK) WITH 'Loading picture error!'.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
Screen:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
*
PROCESS AFTER INPUT.
MODULE user_command_0100.
GUISTATUS:
Application tool bar: NM DLT FT NMCT FTCT
Standard tool bar: PCF PBAK PEXIT
备注: cl_gui_picture还可以控制SAP里面的icons.