[ABAP] QR-Codes als Bitmap erzeugen

* Docking-Container für Einbettung cl_gui_picture
DATA: o_dock TYPE REF TO cl_gui_docking_container.

* Text für QR-Code
PARAMETERS: p_text TYPE string DEFAULT 'https://google.de' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  TRY.
      IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
        o_dock = NEW #( repid = sy-repid
                        dynnr = sy-dynnr
                        side  = cl_gui_docking_container=>dock_at_right
                        ratio = 50 ).
      ENDIF.

      DATA: lv_xstr_bmp TYPE xstring.

* Siehe https://me.sap.com/notes/2790500
* Siehe https://me.sap.com/notes/2030263
* i_module_size      - Größe des Barcodes in 1-32000 Pixel, wobei 1 Pixel == ein 1/600 Zoll großer Punkt
* i_barcode_text     - Textinhalt
* i_mode             - A - AUTOMATIC
*                      N - NUMERIC
*                      L - ALPHANUMERIC
*                      B - BYTE_LATIN1
*                      K - KANJI
*                      U - BYTE_UTF8
*                      1 - FNC1_POS1 (FNC1 an erster Position)
*                      2 - FNC1_POS2 (FNC1 an zweiter Position)
* i_error_correction - Fehlerkorrekturwert (L - 7%, M - 15%, Q - 25%, H - 30%)
* i_rotation         - Barcode-Ausrichtung: 0 - NORMAL, 90 - ROTATED, 180 - INVERTED, 270 - BOTTOMUP
* Process QR Code barcode
      cl_rstx_barcode_renderer=>qr_code( EXPORTING i_module_size  = 50
                                                   i_barcode_text = p_text
*                                                 i_mode             = 'A'
*                                                 i_error_correction = 'H'
*                                                 i_rotation         = 0
                                         IMPORTING e_bitmap       = lv_xstr_bmp ).

      DATA(it_bin_bmp) = cl_bcs_convert=>xstring_to_solix( lv_xstr_bmp ).

      DATA: lv_url TYPE swk_url.

* temporäre URL auf das Bild erzeugen
      CALL FUNCTION 'DP_CREATE_URL'
        EXPORTING
          type                 = 'image/bmp' " https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht
          subtype              = 'bmp'
        TABLES
          data                 = it_bin_bmp
        CHANGING
          url                  = lv_url
        EXCEPTIONS
          dp_invalid_parameter = 1
          dp_error_put_table   = 2
          dp_error_general     = 3
          OTHERS               = 4.

* Bild über die URL laden und anzeigen
      DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
      o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
      o_pic->load_picture_from_url_async( lv_url ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
      cl_gui_cfw=>flush( ).

    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Code128-Barcode als Bitmap erzeugen

* Docking-Container für Einbettung cl_gui_picture
DATA: o_dock TYPE REF TO cl_gui_docking_container.

* Text für Barcode
PARAMETERS: p_text TYPE string DEFAULT '1234567890' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  TRY.
      IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
        o_dock = NEW #( repid = sy-repid
                        dynnr = sy-dynnr
                        side  = cl_gui_docking_container=>dock_at_right
                        ratio = 50 ).
      ENDIF.

      DATA: lv_xstr_bmp TYPE xstring.

* Siehe: https://me.sap.com/notes/2790500
* Siehe: https://me.sap.com/notes/645158
* i_narrow_module_width - 1 ... 10, Breite für das schmalste Modul eines Barcodes in Pixeln
* i_height              - 1 ... 9999, Höhe der Balken des Barcodes in Pixeln (600 px == 1 Zoll)
* i_mode                - N (None), A (Automatic), U (UCC case mode)
* i_rotation            - 0 - Normal, 90 -  Rotated, 180 - Inverted, 270 - Bottomup
* Process Code 128 barcode
      cl_rstx_barcode_renderer=>code_128( EXPORTING i_narrow_module_width = 1
                                                    i_height              = 50
*                                                    i_mode                = 'A'
*                                                    i_rotation            = 0
                                                    i_barcode_text        = p_text
                                          IMPORTING e_bitmap              = lv_xstr_bmp ).

      DATA(it_bin_bmp) = cl_bcs_convert=>xstring_to_solix( lv_xstr_bmp ).

      DATA: lv_url TYPE swk_url.

* temporäre URL auf das Bild erzeugen
      CALL FUNCTION 'DP_CREATE_URL'
        EXPORTING
          type                 = 'image/bmp' " https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht
          subtype              = 'bmp'
        TABLES
          data                 = it_bin_bmp
        CHANGING
          url                  = lv_url
        EXCEPTIONS
          dp_invalid_parameter = 1
          dp_error_put_table   = 2
          dp_error_general     = 3
          OTHERS               = 4.

* Bild über die URL laden und anzeigen
      DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
      o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
      o_pic->load_picture_from_url_async( lv_url ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
      cl_gui_cfw=>flush( ).

    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Höhe einer Dynpro-Zeile bzw. Breite einer Dynpro-Spalte in Pixeln ausgeben

* Höhe einer Dynpro-Zeile in Pixeln
DATA(lv_height_px) = cl_gui_cfw=>compute_metric_from_dynp( EXPORTING metric = cl_gui_control=>metric_pixel
                                                                     x_or_y = 'Y'
                                                                     in     = 1 ).

WRITE: / lv_height_px.

* Breite einer Dynpro-Spalte in Pixeln
DATA(lv_width_px) = cl_gui_cfw=>compute_metric_from_dynp( EXPORTING metric = cl_gui_control=>metric_pixel
                                                                    x_or_y = 'X'
                                                                    in     = 1 ).

WRITE: / lv_width_px.

[ABAP] SAP-Workdir ermitteln

Variante 1 (cl_gui_frontend_services)

PARAMETERS: p_path TYPE string OBLIGATORY LOWER CASE.

INITIALIZATION.

TRY.
  cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = p_path ).

  cl_gui_cfw=>flush( ).
CATCH cx_root INTO DATA(e_txt).
  WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (IW_C_GET_SAPWORKDIR)

DATA: lvsapworkdir TYPE sdok_chtrd.
DATA: lverror_msg	TYPE iwerrormsg.

* SAP Workdir aus der Registry bestimmen (Temporär)
CALL FUNCTION 'IW_C_GET_SAPWORKDIR'
  IMPORTING
    sapworkdir = lvsapworkdir
    error_msg  = lverror_msg.

WRITE: / lvsapworkdir.
WRITE: / lverror_msg.

[ABAP] URL-Aufruf mit SSO-Anmeldung

* NULL-Referenz auf Container
CONSTANTS: null TYPE REF TO cl_gui_container VALUE IS INITIAL.

* aufzurufende URL
DATA(lv_url) = |https://www.google.de|.

* Browserfenster ohne Container
DATA(o_hv) = NEW cl_gui_html_viewer( parent = null ).
* SSO
o_hv->enable_sapsso( enabled = abap_true ).
* URL anzeigen
o_hv->detach_url_in_browser( CONV swk_url( lv_url ) ).
* Führt alle gesammelten OLE-Calls aus
cl_gui_cfw=>flush( ).

[ABAP] GOS-Container (Generic Object Services) für Anzeige von beliebigen Objekten in der Titlebar nutzen

Variante 1 (cl_gui_toolbar)

* https://www.tricktresor.de/blog/hacking-sapgui/
DATA: o_cnt_gos TYPE REF TO cl_gui_gos_container.
DATA: o_toolbar TYPE REF TO cl_gui_toolbar.

* Eventhandler für Toolbar-Buttons
CLASS lcl_events DEFINITION.
  PUBLIC SECTION.

    TYPES: ty_it_events TYPE STANDARD TABLE OF cntl_simple_event WITH DEFAULT KEY.

    CLASS-METHODS:
      on_function_selected FOR EVENT function_selected OF cl_gui_toolbar
        IMPORTING
            fcode.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.
  METHOD on_function_selected.
    MESSAGE fcode TYPE 'S'.
  ENDMETHOD.
ENDCLASS.

* Checkbox zum anzeigen / verstecken des GOS-Containers
PARAMETERS: p_chk1 AS CHECKBOX USER-COMMAND cmd1.

INITIALIZATION.
* GOS-Container mit Breite = 300
  o_cnt_gos = NEW #( width = 300 ).

* Toolbar horizontal mit Buttons
  o_toolbar = NEW #( parent       = o_cnt_gos
                     display_mode = cl_gui_toolbar=>m_mode_horizontal ).

  o_toolbar->add_button( fcode     = 'BTN1'
                         icon      = icon_open
                         butn_type = cntb_btype_button ).

  o_toolbar->add_button( fcode     = ''
                         icon      = ''
                         butn_type = cntb_btype_sep ).

  o_toolbar->add_button( fcode     = 'BTN2'
                         icon      = icon_system_save
                         butn_type = cntb_btype_button ).

  DATA(it_events) = VALUE lcl_events=>ty_it_events( ( eventid    = cl_gui_toolbar=>m_id_function_selected
                                                      appl_event = abap_true ) ).

  o_toolbar->set_registered_events( events = it_events ).

  SET HANDLER lcl_events=>on_function_selected FOR o_toolbar.

AT SELECTION-SCREEN.
  CASE sy-ucomm.
* Checkbox geklickt -> GOS-Container ein-/ausblenden
    WHEN 'CMD1'.
      o_cnt_gos->set_visible( COND abap_bool( WHEN p_chk1 = abap_true THEN abap_false ELSE abap_true ) ).
  ENDCASE.

START-OF-SELECTION.
  WRITE: / p_chk1.

Variante 2 (cl_gui_textedit)

DATA: o_cnt_gos TYPE REF TO cl_gui_gos_container.
DATA: o_edit TYPE REF TO cl_gui_textedit.

PARAMETERS: p_matnr type matnr.

INITIALIZATION.

* Fensterbreite des SAP-Fensters
  DATA(lv_x_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                            x_or_y = 'X'
                                                            in = sy-scols ).

* GOS-Container mit akt. Fensterbreite
  o_cnt_gos = NEW #( width = lv_x_metric ).

* Editorzeile im Titel
  o_edit = NEW #( wordwrap_mode              = cl_gui_textedit=>wordwrap_at_windowborder
                  wordwrap_to_linebreak_mode = cl_gui_textedit=>true
                  parent                     = o_cnt_gos ).

* feste Zeichenbreite
  o_edit->set_font_fixed( mode = cl_gui_textedit=>true ).
* Anzeige von Toolbar und Statusbar des Texteditors unterdrücken
  o_edit->set_toolbar_mode( toolbar_mode = cl_gui_textedit=>false ).
  o_edit->set_statusbar_mode( statusbar_mode = cl_gui_textedit=>false ).
  o_edit->set_textstream( 'Testtext' ).

START-OF-SELECTION.
  IF o_edit IS BOUND.
* Text aus Editorzeile holen
    DATA(lv_text) = ||.

    o_edit->get_textstream( IMPORTING text = lv_text ).
    cl_gui_cfw=>flush( ).

    WRITE: / lv_text.
  ENDIF.

Variante 3 (cl_gui_html_viewer)

DATA: o_cnt_gos TYPE REF TO cl_gui_gos_container.
DATA: o_html TYPE REF TO cl_gui_html_viewer.

PARAMETERS: p_matnr TYPE matnr.

INITIALIZATION.

* Zeilen und Spalten in Pixel umrechnen
  DATA(lv_x_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                            x_or_y = 'X'
                                                            in = sy-scols ).

* GOS-Container mit akt. Fensterbreite
  o_cnt_gos = NEW #( width = lv_x_metric ).

* HTML im Titlebar anzeigen
  o_html = NEW cl_gui_html_viewer( parent = o_cnt_gos ).

  DATA(it_html) = VALUE w3_htmltab( ( '<html><head><style>body { margin: 0; background-color: #00BBCC; color: #001122; font: 18px "Courier" }</style></head>' )
                                    ( '<body>Test</body></html>' ) ).
  DATA: lv_url TYPE swk_url.

  o_html->load_data( IMPORTING
                      assigned_url = lv_url
                    CHANGING
                      data_table   = it_html ).

  o_html->show_url( lv_url ).

START-OF-SELECTION.
  WRITE: / p_matnr.

[ABAP] Bilder und Icons von einem Webserver (URL) anzeigen

Variante 1 (load_picture_from_url_async)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).

    TRY.
        o_pic->load_picture_from_url_async( p_url ).
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDIF.

Variante 2 (load_picture_from_url mit Fehlerbehandlung)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
    TRY.
        DATA(lv_load_ok) = 0.

* Bild laden
        o_pic->load_picture_from_url( EXPORTING url = p_url IMPORTING result = lv_load_ok ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
        cl_gui_cfw=>flush( ).

        CASE lv_load_ok.
          WHEN 0.
* Fehler
            MESSAGE 'Bild nicht vorhanden.' TYPE 'S' DISPLAY LIKE 'E'.
          WHEN 1.
* Ok
            MESSAGE 'Bild konnte geladen werden.' TYPE 'S' DISPLAY LIKE 'I'.
        ENDCASE.
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.

  ENDIF.

Variante 3 (load_picture_from_sap_icons)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).

    TRY.
        o_pic->load_picture_from_sap_icons( icon_annotation ).
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDIF.

[ABAP] IDoc als XML-Datei speichern

PARAMETERS: p_idoc TYPE edi_docnum.

START-OF-SELECTION.

  TRY.
      DATA(o_idoc_xml) = NEW cl_idoc_xml1( docnum = p_idoc ).

      DATA: lv_xml TYPE string.

      o_idoc_xml->get_xmldata_as_string( IMPORTING data_string = lv_xml ).

      DATA(it_xml) = VALUE stringtab( ( |{ lv_xml }| ) ).

      DATA: lv_temp_dir TYPE string.

* Temp-Directory holen
      cl_gui_frontend_services=>get_desktop_directory( CHANGING desktop_directory = lv_temp_dir ).
* Note 1442303, sonst ist lv_temp_dir leer
      cl_gui_cfw=>flush( ).

* Dateinamen zusammenbauen
      DATA(lv_filename) = |{ lv_temp_dir }\\my_xml.xml|.

* Datei im Zielverzeichnis erzeugen
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename = lv_filename
                                                filetype = 'ASC'
                                              CHANGING
                                                data_tab = it_xml ).

      WRITE: / lv_filename.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] VBA-Code ausführen

* VBA-Code
DATA(it_vba_code) = VALUE stringtab( ( |Dim app| )
                                     ( |Set app = CreateObject("Access.Application")| )
                                     ( |Dim msg| )
                                     ( |msg = MsgBox ("My Message.", 0, "Warning")| ) ).

DATA: lv_temp_dir TYPE string.

* Temp-Directory holen
cl_gui_frontend_services=>get_desktop_directory( CHANGING desktop_directory = lv_temp_dir ).
* Note 1442303, sonst ist lv_temp_dir leer
cl_gui_cfw=>flush( ).

* Dateinamen zusammenbauen
DATA(lv_filename) = |{ lv_temp_dir }\\my_script.vbs|.

* Datei im Zielverzeichnis erzeugen
cl_gui_frontend_services=>gui_download( EXPORTING
                                          filename = lv_filename
                                          filetype = 'ASC'
                                        CHANGING
                                          data_tab = it_vba_code ).

LOOP AT it_vba_code ASSIGNING FIELD-SYMBOL(<fs_line>).
  WRITE: / <fs_line>.
ENDLOOP.

SKIP.

WRITE: / lv_filename.

* VBA über Scripting Host ausführen
* "" für lv_filename ergänzen, sonst wird der Pfad unter parameter nicht korrekt übergeben,
* da Leerzeichen im Pfad als Trennung in einzelne Parameter erkannt wird
cl_gui_frontend_services=>execute( application = 'WSCRIPT.EXE'
                                   parameter   = |"{ lv_filename }"| ).

[ABAP] Höhe und Breite des aktuellen SAP-Fensters ermitteln

Variante 1 (sy-scols / sy-srows)

* Zeilen und Spalten in Pixel umrechnen
DATA(lv_x_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                          x_or_y = 'X'
                                                          in = sy-scols ).

DATA(lv_y_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                          x_or_y = 'Y'
                                                          in = sy-srows ).

WRITE: / |X: { lv_x_metric }|.
WRITE: / |Y: { lv_y_metric }|.

Variante 2 (Kernel-Methode ‚GET_WINDOW_DATA‘)

DATA: lv_begin_r TYPE i.
DATA: lv_end_r TYPE i.

DATA: lv_begin_c TYPE i.
DATA: lv_end_c TYPE i.

* Kernel-Methode 'GET_WINDOW_DATA' aufrufen, gibt Position des Fensters in Dynprozeilen und -spalten zurück
CALL 'GET_WINDOW_DATA' ID 'BEGROW' FIELD lv_begin_r ID 'BEGCOL' FIELD lv_begin_c ID 'ENDROW' FIELD lv_end_r ID 'ENDCOL' FIELD lv_end_c.

WRITE: / |From col: { lv_begin_c } to row: { lv_end_c }|.
WRITE: / |From row: { lv_begin_r } to row: { lv_end_r }|.

* Zeilen und Spalten gesamt ausrechnen
DATA(lv_cols) = lv_end_c - lv_begin_c.
DATA(lv_rows) = lv_end_r - lv_begin_r.

WRITE: / |Cols: { lv_cols }|.
WRITE: / |Rows: { lv_rows }|.

* Zeilen und Spalten in Pixel umrechnen
DATA(lv_x_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                          x_or_y = 'X'
                                                          in = lv_cols ).

DATA(lv_y_metric) = cl_gui_cfw=>compute_metric_from_dynp( metric = cl_gui_control=>metric_pixel
                                                          x_or_y = 'Y'
                                                          in = lv_rows ).

* Breite des Fensters in Pixel
WRITE: / |X: { lv_x_metric }|.
* Höhe des Fensters in Pixel
WRITE: / |Y: { lv_y_metric }|.