在执行一些数据量大的程序时,通常在程序下方的状态栏中加一个秒表用来显示进度,供用户了解程序运行到什么程度。实现方法有如下两种:
1, SAPGUI_PROGRESS_INDICATOR
常用而古老的方法,调用SAPGUI_PROGRESS_INDICATOR函数,指定函数的两个参数
- percentage :百分比的数值,用来控制秒表的指针,0到100之间的数字。
- text:状态栏显示的文本
例子代码:
选择画面输入循环次数,每次循环等待1秒,然后更新进步条的秒表指针和文本。
REPORT ztest_progress. DATA: g_perc TYPE p. DATA: g_stxt TYPE string. DATA: g_sperc(3) TYPE c. PARAMETERS: p_times TYPE i DEFAULT 5. g_perc = 0. DO p_times TIMES. WAIT UP TO 1 SECONDS. CLEAR:g_stxt. g_perc = sy-index / p_times * 100. g_sperc = g_perc. CONCATENATE '已经完成了' g_sperc '%' INTO g_stxt. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' EXPORTING percentage = g_perc "百分比的数值,用来控制秒表的指针。 text = g_stxt. "状态栏显示的文本。 ENDDO.
运行效果:
2, CL_PROGRESS_INDICATOR
SAP还提供了更方便快捷的类CL_PROGRESS_INDICATOR中的方法PROGRESS_INDICATE来实现这个进度条。
这个类方法其实也是调用了SAPGUI_PROGRESS_INDICATOR函数,只不过是更友好的封装了一下。
CL_PROGRESS_INDICATOR=>PROGRESS_INDICATE有两种显示模式:
- 立即显示
- 每隔10秒更新一下进度条
通过参数i_output_immediately控制('X' = Display Progress Immediately ).
立即显示的例子:
PARAMETERS: p_times1 TYPE i DEFAULT 5. "Immediately DO p_times1 TIMES. cl_progress_indicator=>progress_indicate( EXPORTING i_text = | Output immediately { sy-index } / { p_times1 } | * i_msgid = i_msgid " Message Class (If I_TEXT is not transferred) * i_msgno = i_msgno " Message Number (If I_TEXT is not transferred) * i_msgv1 = i_msgv1 " Message Variable (Maximum of 50 Characters) * i_msgv2 = i_msgv2 " Message Variable (Maximum of 50 Characters) * i_msgv3 = i_msgv3 " Message Variable (Maximum of 50 Characters) * i_msgv4 = i_msgv4 " Message Variable (Maximum of 50 Characters) i_processed = sy-index " Number of Objects Already Processed i_total = p_times1 " Total Number of Objects to Be Processed i_output_immediately = abap_true " X = Display Progress Immediately * importing * e_progress_sent = e_progress_sent " X = Progress Information Was Displayed ). WAIT UP TO 1 SECONDS. ENDDO.
运行效果:
每隔10秒显示的例子:
PARAMETERS: p_times1 TYPE i DEFAULT 20. "Once per 10 seconds. DO p_times1 TIMES. cl_progress_indicator=>progress_indicate( EXPORTING i_text = | Output once per 10 seconds { sy-index } / { p_times1 } | * i_msgid = i_msgid " Message Class (If I_TEXT is not transferred) * i_msgno = i_msgno " Message Number (If I_TEXT is not transferred) * i_msgv1 = i_msgv1 " Message Variable (Maximum of 50 Characters) * i_msgv2 = i_msgv2 " Message Variable (Maximum of 50 Characters) * i_msgv3 = i_msgv3 " Message Variable (Maximum of 50 Characters) * i_msgv4 = i_msgv4 " Message Variable (Maximum of 50 Characters) i_processed = sy-index " Number of Objects Already Processed i_total = p_times1 " Total Number of Objects to Be Processed i_output_immediately = abap_false " X = Display Progress Immediately * importing * e_progress_sent = e_progress_sent " X = Progress Information Was Displayed ). WAIT UP TO 1 SECONDS. ENDDO.
运行效果:
以上。