ABAP Control Framework(7): Picture

7.Picture

          使用cl_gui_picture类显示图片。

示例:

"event handler类
CLASS c_event_handler DEFINITION.
  PUBLIC SECTION.
    "picture event
    "图片点击
    CLASS-METHODS:event_picture_click
      FOR EVENT picture_click OF cl_gui_picture
      IMPORTING mouse_pos_x mouse_pos_y sender.
    "图片双击
    CLASS-METHODS:event_picture_dblclick
      FOR EVENT picture_dblclick OF cl_gui_picture
      IMPORTING mouse_pos_x mouse_pos_y sender.
    "图片右键菜单
    CLASS-METHODS:event_context_menu
      FOR EVENT context_menu OF cl_gui_picture
      IMPORTING sender.
    "图片右键菜单功能
    CLASS-METHODS:event_context_menu_sel
      FOR EVENT context_menu_selected OF cl_gui_picture
      IMPORTING fcode sender.
ENDCLASS.

CLASS c_event_handler IMPLEMENTATION.
  METHOD:event_picture_click.
    DATA:lv_msg TYPE C LENGTH 20.
    lv_msg = 'click,X:' && mouse_pos_x && ';Y:' && mouse_pos_y.
    MESSAGE lv_msg TYPE 'I'.
  ENDMETHOD.

  METHOD:event_picture_dblclick.
    DATA:lv_msg TYPE C LENGTH 20.
    lv_msg = 'double click,X:' && mouse_pos_x && ';Y:' && mouse_pos_y.
    MESSAGE lv_msg TYPE 'I'.
  ENDMETHOD.

  METHOD:event_context_menu.
    DATA:lv_menu TYPE REF TO cl_ctmenu.
    "创建右键菜单对象
    CREATE OBJECT lv_menu.
    "添加菜单项
    CALL METHOD lv_menu->add_function
      EXPORTING
        fcode = 'NORMAL'
        TEXT  = '正常'.
    CALL METHOD lv_menu->add_function
      EXPORTING
        FCODE = 'STRETCH'
        TEXT  = '伸展'.
    CALL METHOD lv_menu->add_function
      EXPORTING
        FCODE = 'FIT'
        TEXT  = '适应'.
    CALL METHOD lv_menu->add_function
      EXPORTING
        FCODE = 'NORMAL_CENTER'
        TEXT  = '正常居中'.
    CALL METHOD lv_menu->add_function
      EXPORTING
        FCODE = 'FIT_CENTER'
        TEXT  = '适应居中'.
    "设置右键menu
    sender->display_context_menu( lv_menu ).
  ENDMETHOD.

  METHOD:event_context_menu_sel.
    DATA:lv_display_mode TYPE I.
    IF fcode = 'NORMAL'.
      lv_display_mode = CL_GUI_PICTURE=>DISPLAY_MODE_NORMAL.
    ENDIF.
    IF fcode = 'STRETCH'.
      lv_display_mode = CL_GUI_PICTURE=>DISPLAY_MODE_STRETCH.
    ENDIF.
    IF fcode = 'FIT'.
      lv_display_mode = CL_GUI_PICTURE=>DISPLAY_MODE_FIT.
    ENDIF.
    IF fcode = 'NORMAL_CENTER'.
      lv_display_mode = CL_GUI_PICTURE=>DISPLAY_MODE_NORMAL_CENTER.
    ENDIF.
    IF fcode = 'FIT_CENTER'.
      lv_display_mode = CL_GUI_PICTURE=>DISPLAY_MODE_FIT_CENTER.
    ENDIF.
    "设置picture显示模式
    sender->set_display_mode( lv_display_mode ).
  ENDMETHOD.
ENDCLASS.

CLASS c6 DEFINITION.
  PUBLIC SECTION.
    "图片对象
    DATA:lo_picture TYPE REF TO cl_gui_picture.
    METHODS:m_init_picture.
ENDCLASS.


CLASS c6 IMPLEMENTATION.
  METHOD:m_init_picture.
    DATA:lo_mime_api   TYPE REF TO if_mr_api.
    DATA:lv_xstring TYPE xstring.
    DATA:lv_url TYPE C LENGTH 256.
    DATA:lv_x1024 TYPE x LENGTH 1024.
    DATA:lt_x1024 LIKE TABLE OF lv_x1024.
    DATA:lt_event TYPE cntl_simple_events.
    DATA:ls_event LIKE LINE OF lt_event.

    IF lo_picture IS INITIAL.
      CREATE OBJECT lo_picture
        EXPORTING parent = cl_gui_container=>default_screen.
    ENDIF.
    "方式1:mime repository api对象
*    lo_mime_api = cl_mime_repository_api=>get_api( ).
*    "mime中图片
*    lo_mime_api->get(
*      EXPORTING
*         i_url = '/SAP/PUBLIC/TOM_TEST/pic1.jpg'
*      IMPORTING
*         e_content = lv_xstring
*    ).
*    DO.
*      IF xstrlen( lv_xstring ) > 1024.
*        lv_x1024 = lv_xstring(1024).
*        APPEND lv_x1024 TO lt_x1024.
*        SHIFT lv_xstring BY 1024 PLACES LEFT IN BYTE MODE.
*      ELSE.
*        APPEND lv_xstring TO lt_x1024.
*        EXIT.
*      ENDIF.
*    ENDDO.
*    "获取url
*    CALL FUNCTION 'DP_CREATE_URL'
*      EXPORTING
*        type    = 'IMAGE'
*        subtype = 'GIF'
*      TABLES
*        data    = lt_x1024
*      CHANGING
*        url     = lv_url.

    "方式2:使用smw0图片ZTOM_TEST
*    CALL FUNCTION 'DP_PUBLISH_WWW_URL'
*      EXPORTING
*        OBJID    = 'ZTOM_TEST'
*        LIFETIME = cndp_lifetime_transaction
*      IMPORTING
*        URL      = lv_url
*      EXCEPTIONS
*        OTHERS   = 1.

    "加载picture
    "lo_picture->load_picture_from_url( lv_url ).
    "方式3:网络url
    lv_url = 'https://scpic.chinaz.net/files/pic/pic9/202007/apic26572.jpg'.
    "异步加载
    lo_picture->load_picture_from_url_async( lv_url ).
    "设置样式
    lo_picture->set_display_mode( cl_gui_picture=>display_mode_stretch ).

    "注册事件
    ls_event-eventid = cl_gui_picture=>eventid_picture_click.
    APPEND ls_event TO lt_event.
    ls_event-eventid = cl_gui_picture=>eventid_picture_dblclick.
    APPEND ls_event TO lt_event.
    ls_event-eventid = cl_gui_picture=>eventid_context_menu.
    APPEND ls_event TO lt_event.
    ls_event-eventid = cl_gui_picture=>eventid_context_menu_selected.
    APPEND ls_event TO lt_event.
    lo_picture->set_registered_events( lt_event ).
    "设置事件处理handler
    SET HANDLER c_event_handler=>event_picture_click FOR lo_picture.
    SET HANDLER c_event_handler=>event_picture_dblclick FOR lo_picture.
    SET HANDLER c_event_handler=>event_context_menu FOR lo_picture.
    SET HANDLER c_event_handler=>event_context_menu_sel FOR lo_picture.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  PERFORM f_show_picture.

FORM f_show_picture.
  DATA:lo_c6 TYPE REF TO c6.
  CREATE OBJECT lo_c6.
  WRITE:/ '1'.
  "显示picture
  lo_c6->m_init_picture( ).
ENDFORM.

 

 

posted @ 2021-05-08 15:49  渔歌晚唱  阅读(176)  评论(0编辑  收藏  举报