1.1 例子
好了,说了这么多,我们举一个小例子,来使用这三个类,关于dragdrop类,我们会在后面介绍其具体的使用方法。
背景:
我们得到了一个图片,然后把它显示在picture control中。添加一个toolbar,通过toolbar中的按钮控制图片的大小以及显示方式。通过使用context menu,可以实现一些控制,比如删除图片,打开新图片等等。
8.4.1 定义数据
*Class pre-define which used to reference
class lcl_pic_container definition deferred.
class event_receiver definition deferred.
class lcl_toolbar definition deferred.
class cl_gui_cfw definition load.
*Screen field for program
data: ok_code like sy-ucomm.
data: s_object_id type string,
s_object_value type string.
data: ename(50) type c.
*Picture container and custom container
data: container_pic type ref to cl_gui_custom_container,
container_h type ref to cl_gui_custom_container,
container_v type ref to cl_gui_custom_container.
data: ref_pic type ref to lcl_pic_container,
ref_tool type ref to lcl_toolbar.
*If the control created indicator
data: init type c.
*Data For Load Pre-Defined context menu*******
data: prog type sy-repid.
8.4.2 创建屏幕
定义context menu form
定义context menu
处理pre-defined context
*Using the form routine which defined in you dynpro
form on_ctmenu_input using l_menu type ref to cl_ctmenu.
prog = sy-repid.
call method:l_menu->load_gui_status
exporting program = prog
status = 'PF1' "Your context menu name
menu = l_menu.
call method l_menu->add_function
exporting fcode = 'GETN'
text = 'Select new picture'.
endform.
定义pic类
class lcl_pic_container definition.
public section.
data: object_id type string,
object_value type string.
constants:
id_bmap(5) value 'BMAP',
value_enjoy(20) value 'ENJOY',
value_sapsmartforms(20) value 'SAPSMARTFORMS',
value_mysapcom(20) value 'MYSAPCOM'.
methods:
* Initialize screen
init_screen,
* Add picture to picture container
add_pic,
* Free objects
free,
* Setter, set picture id and type
set_object
importing i_object_id type string
i_object_value type string,
get_object
exporting value(e_id) type string
value(e_name) type string.
private section.
data:
pic type ref to cl_gui_picture,
url(255).
* For picture control events
data:
event type cntl_simple_event,
events type cntl_simple_events.
data:
event_ref type ref to event_receiver.
methods:
* Get picture url
get_pic_url
importing i_object_id type string
i_object_value type string
returning value(s_url) type string,
* Set pf-status
set_pf_status,
* Set handler for picture control
set_handler.
endclass.
8.4.4 定义toolbar类
class lcl_toolbar definition.
public section.
methods:
* Create class
constructor
importing pic_ref type ref to cl_gui_picture,
free.
private section.
* Define two toolbar
data:
h_ref type ref to cl_gui_toolbar,
v_ref type ref to cl_gui_toolbar.
* Toolbar events
data:
event type cntl_simple_event,
events type cntl_simple_events.
data:
event_ref type ref to event_receiver.
* Define button group table
data:
bgroup type ttb_button.
data: pict type ref to cl_gui_picture.
* Private method for add toolbar button group
methods:
add_button_group importing
fcode type ui_func
icon type iconname
type type tb_btype
text type text40
tip type iconquick.
endclass.
8.4.5 定义event处理类
*For toolbar and picture control and context menu event handle
class event_receiver definition.
public section.
methods:
* For create class, get the object which created it
constructor
importing pic_ref type ref to object,
* Events for picture control ****************
event_dblclick for event picture_dblclick
of cl_gui_picture
importing sender,
event_click for event picture_click
of cl_gui_picture
importing sender,
event_con_menu for event context_menu
of cl_gui_picture
importing sender,
event_con_menu_sel for event context_menu_selected
of cl_gui_picture
importing fcode sender,
* Events for picture control ****************
* Events for toolbar control ****************
event_t_f_sel for event function_selected
of cl_gui_toolbar
importing fcode sender,
event_t_d_clk for event dropdown_clicked
of cl_gui_toolbar
importing fcode posx posy sender.
* Events for toolbar control ****************
private section.
* Indicator for object swich
data: flag type c.
data: flag_vh type c.
* Catch cast exception
data: cast type c.
data: pict type ref to cl_gui_picture.
endclass.
8.4.6 Implementation 各个类
对于pic类,方法init_screen初始化屏幕
method init_screen.
call method set_pf_status.
* Create custom container for picture container
create object container_pic
exporting
container_name = 'CONTAINER_PIC' .
* Create picture control
create object pic
exporting
parent = container_pic .
* Create toolbar in screen
create object ref_tool
exporting pic_ref = pic.
* Add events for picture control
event-eventid = cl_gui_picture=>eventid_picture_click.
event-appl_event = 'X'.
append event to events.
event-eventid = cl_gui_picture=>eventid_picture_dblclick.
event-appl_event = 'X'.
append event to events.
event-eventid = cl_gui_picture=>eventid_context_menu.
event-appl_event = 'X'.
append event to events.
event-eventid = cl_gui_picture=>eventid_context_menu_selected.
event-appl_event = 'X'.
append event to events.
* Register events for picture control
call method pic->set_registered_events
exporting events = events.
* Set handler for picture control
call method set_handler.
* Set picture control border type
call method pic->set_3d_border
exporting border = 1.
* Set picture id and name
call method ref_pic->set_object
exporting i_object_id = s_object_id
i_object_value = s_object_value.
* Add picture to picture control
call method add_pic.
* Set object created indicator
init = 'X'.
endmethod.
PBO 处理
module status_0100 output.
if init is initial.
create object ref_pic.
s_object_id = lcl_pic_container=>id_bmap.
s_object_value = lcl_pic_container=>value_sapsmartforms.
call method ref_pic->init_screen.
endif.
endmodule. " STATUS_0100 OUTPUT
1.2 测试结果