在这一篇中将学习一下 cl_gui_picture这个Control,以及实现如何添加context_menu.
ci_gui_picture实现起来很简单,具体为:创建一个cl_gui_picture的实例,然后调用 LOAD_PICTURE方法用来显示图片。具体来说又三个load_picture的方法,第一个是 LOAD_PICTURE_FROM_SAP_ICONS,第二个是 LOAD_PICTURE_FROM_URL以及第三个 LOAD_PICTURE_FROM_URL_ASYNC。
在Picture中添加Context_menu实现起来也比较简单。首先定义一个 cl_gui_picture的 context_menu,在该事件中创建一个 cl_ctmenu的实例,然后调用 cl_ctmenu的 ADD_FUNCTION方法来添加需要显示的menu。再定义一个 context_menu_selected事件,该时间用来实现选择menu之后的后续动作。
接下来将以一个实例来说明实现上述操作。在该实例中首先显示一种图片,然后再该图片上添加context menu,选择context menu可以对该图片进行拉伸处理,此外还实现了图片的double click事件。具体步骤如下:
一,创建一个屏幕号为100的screen,在该screen中添加一个名称为" PIC_CON"的custom container.
二,定义 cl_gui_custom_container对象 container_1 以及 cl_gui_picture对象 PIC_CON。代码如下:
data PIC_CON type ref to cl_gui_picture.
data container_1 type ref to cl_gui_custom_container.
三,定义一个名为event_receiver1的class,该class中包括了三个方法 event_handler_context_menu以及 event_handler_context_menu_sel和event_handler_picture_dblclick。具体代码为:
class event_receiver1 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
四,实现class中定义的方法。在方法 event_handler_context_menu中首先创建一个 cl_ctmenu的实例menu,然后调用menu的 ADD_FUNCTION方法添加menu,最后调用 DISPLAY_CONTEXT_MENU方法显示context menu 。在方法 event_handler_context_menu_sel中首先获取选中menu的function code,然后调用picture的 SET_DISPLAY_MODE方法来对图片进行拉伸或者正常显示处理。在方法 event_handler_picture_dblclick方法中将获取鼠标双击的问题,然后以message的方式显示位置。具体代码如下:
class event_receiver1 IMPLEMENTATION.
METHOD event_handler_picture_dblclick.
* for event picture_dblclick of c_picture_control
* importing mouse_pos_x mouse_pos_y.
DATA pos_x(5) type c.
DATA pos_y(5) type c.
pos_x = mouse_pos_x.
pos_y = mouse_pos_y.
IF SENDER = PIC_CON.
MESSAGE I000(0K) WITH
'DoubleClick' 'Upper Picture' POS_X POS_Y. "#EC NOTEXT
endif.
endmethod. "event_handler_picture_dblclick
method event_handler_context_menu.
DATA menu type REF TO cl_ctmenu.
CREATE OBJECT menu.
CALL METHOD menu->ADD_FUNCTION
EXPORTING
FCODE = 'NORMAL'
TEXT = 'NORMAL'
* ICON =
* FTYPE =
* DISABLED =
* HIDDEN =
* CHECKED =
* ACCELERATOR =
.
CALL METHOD menu->ADD_FUNCTION
EXPORTING
FCODE = 'STRETCH'
TEXT = 'STRETCH'
* ICON =
* FTYPE =
* DISABLED =
* HIDDEN =
* CHECKED =
* ACCELERATOR =
.
CALL METHOD sender->DISPLAY_CONTEXT_MENU
EXPORTING
CONTEXT_MENU = menu
* EXCEPTIONS
* ERROR = 1
* others = 2
.
endmethod. "event_handler_picture_dblclick
method event_handler_context_menu_sel.
if fcode = 'STRETCH'.
CALL METHOD PIC_CON->SET_DISPLAY_MODE
EXPORTING
DISPLAY_MODE = CL_GUI_PICTURE=>DISPLAY_MODE_STRETCH.
endif.
if fcode = 'NORMAL'.
CALL METHOD PIC_CON->SET_DISPLAY_MODE
EXPORTING
DISPLAY_MODE = CL_GUI_PICTURE=>DISPLAY_MODE_NORMAL.
endif.
endmethod. "event_handler_context_menu_sel
ENDCLASS. "event_receiver IMPLEMENTATION
五,定义 event_receiver1对象,以及 cntl_simple_events和 cntl_simple_event对象。主要用于事件注册时候使用。为什么是这么使用我还没有弄清楚。
data url type cndp_url.
data event_receiver TYPE REF TO event_receiver1.
data event_tab type cntl_simple_events.
data event_tab_line type cntl_simple_event.
六,在PBO的module中创建custom container以及 cl_gui_custom_container的实例,然后调用 LOAD_PICTURE_FROM_URL_ASYNC方法显示图片,并进行事件注册。具体代码为:
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'MAIN0001'.
* SET TITLEBAR 'xxx'.
IF PIC_CON IS INITIAL.
CREATE OBJECT container_1
exporting container_name = 'PIC_CON'.
CREATE OBJECT PIC_CON exporting parent = container_1.
CALL FUNCTION 'DP_PUBLISH_WWW_URL'
EXPORTING
OBJID = 'HTMLCNTL_TESTHTM2_SAP_AG'
LIFETIME = cndp_lifetime_transaction
IMPORTING
URL = url
EXCEPTIONS
OTHERS = 1.
* Load the picture by using the url generated by the data provider.
if sy-subrc = 0.
CALL METHOD PIC_CON->LOAD_PICTURE_FROM_URL_ASYNC
EXPORTING
url = url.
endif.
EVENT_TAB_LINE-EVENTID = CL_GUI_PICTURE=>EVENTID_PICTURE_DBLCLICK.
append EVENT_TAB_LINE to EVENT_TAB.
EVENT_TAB_LINE-EVENTID = CL_GUI_PICTURE=>EVENTID_CONTEXT_MENU.
append EVENT_TAB_LINE to EVENT_TAB.
EVENT_TAB_LINE-EVENTID = CL_GUI_PICTURE=>EVENTID_CONTEXT_MENU_SELECTED.
append EVENT_TAB_LINE to EVENT_TAB.
CALL METHOD PIC_CON->SET_REGISTERED_EVENTS
EXPORTING
EVENTS = event_tab.
create object event_receiver.
set handler event_receiver->event_handler_picture_dblclick
FOR PIC_CON.
set handler event_receiver->event_handler_context_menu
FOR PIC_CON.
set handler event_receiver->event_handler_context_menu_sel
FOR PIC_CON.
EndIF.
ENDMODULE. " STATUS_0100 OUTPUT
ok.大功告成,可以F8了!