GUI_PICTURE以及context_menu學習筆記

TolyHuang發表於2009-04-24

[@more@]在這一篇中將學習一下 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了!

原文:http://www.cnblogs.com/flysky927/archive/2009/01/11/1373792.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9437124/viewspace-1021179/,如需轉載,請註明出處,否則將追究法律責任。

相關文章