【ABAP】 屏幕图片显示Demo

在ABAP屏幕容器中显示图片用到的类是“CL_GUI_PICTURE”,下面则以一个简单的小例子说明其用法,个人学习收藏使用,不代表任何意义。

【效果图】

【ABAP】 屏幕图片显示Demo_第1张图片

【样例代码】

REPORT ZABAP_SCREEN01.

DATA  URL(132).
TYPE-POOLS CNDP.
* custom container
DATA CON1 TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA CON2 TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA CON3 TYPE REF TO CL_GUI_CUSTOM_CONTAINER.

* picture Control.
DATA PICTURE1 TYPE REF TO CL_GUI_PICTURE.
DATA PICTURE2 TYPE REF TO CL_GUI_PICTURE.
DATA PICTURE3 TYPE REF TO CL_GUI_PICTURE.

* Definition of Control Framework
CLASS CL_GUI_CFW DEFINITION LOAD.

* Ps count
DATA PS1 TYPE I.
DATA PS2 TYPE I.
DATA PS3 TYPE I.
DATA ZNAM TYPE CHAR20.

DATA  INIT.
DATA OK_CODE TYPE SY-UCOMM.

CALL SCREEN 100.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
  SET TITLEBAR 'STATUS_0100'.

  ZNAM = SY-UNAME.

  IF INIT IS INITIAL.
* create the custom container
    CREATE OBJECT CON1
      EXPORTING
        CONTAINER_NAME = 'CON1'.
    CREATE OBJECT CON2
      EXPORTING
        CONTAINER_NAME = 'CON2'.
    CREATE OBJECT CON3
      EXPORTING
        CONTAINER_NAME = 'CON3'.
* create the picture control
    CREATE OBJECT PICTURE1
      EXPORTING
        PARENT = CON1.
    CREATE OBJECT PICTURE2
      EXPORTING
        PARENT = CON2.
    CREATE OBJECT PICTURE3
      EXPORTING
        PARENT = CON3.

* Request an URL from the data provider by exporting the pic_data.
    CLEAR URL.
    PERFORM LOAD_PIC_FROM_DB USING 'ZHZ' CHANGING URL .
* load picture
    "设置图片自适应
    CALL METHOD PICTURE1->SET_DISPLAY_MODE
      EXPORTING
        DISPLAY_MODE = CL_GUI_PICTURE=>ADUST_DESIGN_TRUE
      EXCEPTIONS
        ERROR        = 1
        OTHERS       = 2.
    "设置边框
    CALL METHOD PICTURE1->SET_3D_BORDER
      EXPORTING
        BORDER = 1
      EXCEPTIONS
        ERROR  = 1
        OTHERS = 2.

    CALL METHOD PICTURE1->LOAD_PICTURE_FROM_URL
      EXPORTING
        URL = URL.
    INIT = 'X'.

* Request an URL from the data provider by exporting the pic_data.
    CLEAR URL.
    PERFORM LOAD_PIC_FROM_DB USING 'ZWJD' CHANGING URL .
* load picture
    "设置图片自适应
    CALL METHOD PICTURE2->SET_DISPLAY_MODE
      EXPORTING
        DISPLAY_MODE = CL_GUI_PICTURE=>ADUST_DESIGN_TRUE
      EXCEPTIONS
        ERROR        = 1
        OTHERS       = 2.
    "设置边框
    CALL METHOD PICTURE2->SET_3D_BORDER
      EXPORTING
        BORDER = 1
      EXCEPTIONS
        ERROR  = 1
        OTHERS = 2.
    CALL METHOD PICTURE2->LOAD_PICTURE_FROM_URL
      EXPORTING
        URL = URL.
    INIT = 'X'.

* Request an URL from the data provider by exporting the pic_data.
    CLEAR URL.
    PERFORM LOAD_PIC_FROM_DB USING 'ZCDZJ' CHANGING URL .
* load picture
    "设置图片自适应
    CALL METHOD PICTURE3->SET_DISPLAY_MODE
      EXPORTING
        DISPLAY_MODE = CL_GUI_PICTURE=>ADUST_DESIGN_TRUE
      EXCEPTIONS
        ERROR        = 1
        OTHERS       = 2.
    "设置边框
    CALL METHOD PICTURE3->SET_3D_BORDER
      EXPORTING
        BORDER = 1
      EXCEPTIONS
        ERROR  = 1
        OTHERS = 2.
    CALL METHOD PICTURE3->LOAD_PICTURE_FROM_URL
      EXPORTING
        URL = URL.
    INIT = 'X'.

    CALL METHOD CL_GUI_CFW=>FLUSH
      EXCEPTIONS
        CNTL_SYSTEM_ERROR = 1
        CNTL_ERROR        = 2.
    IF SY-SUBRC <> 0.
* error handling
    ENDIF.
  ENDIF.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE OK_CODE .
    WHEN 'BACK' OR 'EXIT'.
      LEAVE TO SCREEN 0.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
    WHEN 'XH1'.
      PS1 = PS1 + 1.
    WHEN 'XH2'.
      PS2 = PS2 + 1.
    WHEN 'XH3'.
      PS3 = PS3 + 1.
  ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  EXIT  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE EXIT INPUT.
  CALL METHOD PICTURE1->FREE.
  CALL METHOD PICTURE2->FREE.
  CALL METHOD PICTURE3->FREE.
  CALL METHOD CON1->FREE.
  CALL METHOD CON2->FREE.
  CALL METHOD CON3->FREE.
  FREE PICTURE1.FREE PICTURE2.FREE PICTURE3.
  FREE CON1. FREE CON2. FREE CON3.
  LEAVE PROGRAM.
ENDMODULE.                             " EXIT  INPUT

*&---------------------------------------------------------------------*
*&      Form  LOAD_PIC_FROM_DB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_URL  text
*----------------------------------------------------------------------*
FORM LOAD_PIC_FROM_DB  USING NAME CHANGING URL.

  DATA: GRAPHIC_SIZE TYPE I.
  DATA: BEGIN OF GRAPHIC_TABLE OCCURS 0,
          LINE(255) TYPE X,
        END OF GRAPHIC_TABLE.
  DATA: L_GRAPHIC_XSTR TYPE XSTRING,
        L_GRAPHIC_CONV TYPE I,
        L_GRAPHIC_OFFS TYPE I.

  CALL METHOD CL_SSF_XSF_UTILITIES=>GET_BDS_GRAPHIC_AS_BMP
    EXPORTING
      P_OBJECT  = 'GRAPHICS'
      P_NAME    = NAME
      P_ID      = 'BMAP'
      P_BTYPE   = 'BCOL'  "(BMON = black&white, BCOL = colour)
    RECEIVING
      P_BMP     = L_GRAPHIC_XSTR
    EXCEPTIONS
      NOT_FOUND = 1
      OTHERS    = 2.

  GRAPHIC_SIZE = XSTRLEN( L_GRAPHIC_XSTR ).
  CHECK GRAPHIC_SIZE > 0.
  L_GRAPHIC_CONV = GRAPHIC_SIZE.
  L_GRAPHIC_OFFS = 0.

  WHILE L_GRAPHIC_CONV > 255.
    GRAPHIC_TABLE-LINE = L_GRAPHIC_XSTR+L_GRAPHIC_OFFS(255).
    APPEND GRAPHIC_TABLE.
    L_GRAPHIC_OFFS = L_GRAPHIC_OFFS + 255.
    L_GRAPHIC_CONV = L_GRAPHIC_CONV - 255.
  ENDWHILE.

  GRAPHIC_TABLE-LINE = L_GRAPHIC_XSTR+L_GRAPHIC_OFFS(L_GRAPHIC_CONV).
  APPEND GRAPHIC_TABLE.

  CALL FUNCTION 'DP_CREATE_URL'
    EXPORTING
      TYPE                 = 'image'               "#EC NOTEXT
      SUBTYPE              = CNDP_SAP_TAB_UNKNOWN " 'X-UNKNOWN'
      SIZE                 = GRAPHIC_SIZE
      LIFETIME             = CNDP_LIFETIME_TRANSACTION  "'T'
    TABLES
      DATA                 = GRAPHIC_TABLE
    CHANGING
      URL                  = URL
    EXCEPTIONS
      DP_INVALID_PARAMETER = 1
      DP_ERROR_PUT_TABLE   = 2
      DP_ERROR_GENERAL     = 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.

ENDFORM.

这里主要用到了其几个方法:

设置图片自适应
    CALL METHOD PICTURE1->SET_DISPLAY_MODE
      EXPORTING
        DISPLAY_MODE = CL_GUI_PICTURE=>ADUST_DESIGN_TRUE
      EXCEPTIONS
        ERROR        = 1
        OTHERS       = 2.

设置边框
    CALL METHOD PICTURE1->SET_3D_BORDER
      EXPORTING
        BORDER = 1
      EXCEPTIONS
        ERROR  = 1
        OTHERS = 2.

显示图片
    CALL METHOD PICTURE1->LOAD_PICTURE_FROM_URL
      EXPORTING
        URL = URL.

你可能感兴趣的:(SAP,ABAP,SAP,ERP,SAP,ABAP,容器,屏幕)