PowerBuilder编程小技巧三则


-- PowerBuilder是当前得到广泛应用的C/S数据库前端开发工具, 在使用PB的过程中,我整理和收集了一些使用PB编程的小技巧, 提供给大家参考.

---- 1. 在Grid或tabular数据窗口中轻松实现按列排序.

---- (1)首先制作datawindow时,使标题的text对象tags=列名.

---- (2)在窗口中定义实例变量boolean ib_accend=false

---- (3).在datawindow中的clicked事件中加入下面的代码:

 string ls_column_name,ls_sort
 if dwo.type="text" then
  ib_accend= not ib_accend
   ls_column_name=dwo.tag   
//得到列名,注意text的tag值应该等于列名
   if ib_accend then
    ls_sort=ls_column_name+" A"
  else
    ls_sort=ls_column_name+" D"
  end if
  this.setsort(ls_sort)
  this.sort()
 else
  do other job
 end if

---- 2.PB程序最小化为systemtray中的图标

       建立主窗口“w_main”: 
     窗口定义如下“Local External Fuctions”: 

     function long LoadLibrary
( ref string string ) LIBRARY
 "KERNEL32" Alias For LoadLibraryA 
     function long FreeLibrary
( long long ) LIBRARY "KERNEL32"  
     function boolean Shell_NotifyIcon
( ulong long, ref s_str str ) LIBRARY 
"SHELL32" Alias FOR "Shell_NotifyIconA"  
     function long LoadIcon( long long, 
long long ) LIBRARY "USER32" Alias For LoadIconA 

---- 定义结构型变量:

       s_str: 

     size  Unsignedlong 
     Hwnd  Long 
     Id    Unsignedlong 
     Flags Unsignedlong 
     Callbackmessage  Unsignedlong 
     Icon  Long 
     tips[64] Character 

     定义实例变量: 

     private: s_str istr_icon 

     窗口最小化按钮Click事件写入如下Script: 

     string ls_resource 
     Long ll_handle 
     // 下三句为装入图标资源,notepad.exe
即为写字板,exe执行时显示的图标内定为1 
     ls_resource = "notepad.exe" 
     ll_handle = loadlibrary ( ls_resource ) 
     istr_icon.icon = loadicon ( ll_handle, 1 ) 

     // 窗口回调事件号,pbm_custom01
即为1024,02为1025,依此类推 

     istr_icon.callbackmessage = 1024 
     istr_icon.tips = "应用程序" 
     istr_icon.hwnd = handle ( parent ) 
     istr_icon.size = 88 
     istr_icon.id = 1 

     // 标识值,即为显示tips 4,
 显示icon 2, 激活窗口对应事件号 

     istr_icon.flags = 7 

     // 显示icon关键函数, 
0为显示,1为修改,2为删除 

     shell_notifyicon ( 0, istr_icon ) 

     parent.hide ( ) 

     // 释放资源 

     freelibrary ( ll_handle ) 

     为窗口创建用户定义事件ue_event,
event id为pbm_custom01,为其写script: 

     m_popup lm_popup  
     Integer li_X, li_Y 
     choose case lparam  
     Case 513 // LButtonUp 

     // 删除图标 

      Shell_NotifyIcon( 2, istr_icon ) 
     // 显示窗口 
     this.show ( ) 
     CASE 517 // RButtonUp 
     li_X = This.X 
     li_Y = This.Y 

     // 移动到屏幕外以免show 时看到,
你可关闭此句看什么效果 
     This.Move ( - This.Width - 10, 
- This.Height - 10 ) 

     // 加这句才能看到菜单条
(菜单条属于此windows) 

     This.Show ( ) 

     lm_popup = CREATE m_popup 

     lm_popup.m_item.PopMenu ( PointerX 
( ), PointerY ( ) ) 

     // 恢复设置 

     This.Hide ( ) 

     This.Move ( li_X, li_Y ) 

     DESTROY lm_popup

---- 3. 使Datawindow每页打印固定行

---- 第一步:增加一个计算列,此计算列必须放在Detail段,Expression中输入: ceiling(getrow()/20) <--这里20还可以用全局函数取代,这样可以允许用户任意设置每页打印多少行。

---- 第二步:定义分组,选择菜单Rows->Create Group...

---- 按计算列字段分组,并一定将check box-->New Page On Group Break选中。

---- 第三步:将此计算列设为不可视。

---- 另外,如果需要最后一页不足补空行。也很简单,如下:

 long  ll_pagerow = 6 //每页打印行数 
long  ll_count, ll_row 
ll_count = dw_report.retrieve(...)
  //取得现有报表的总行数 
ll_count = ll_pagerow - mod(ll_count, ll_pagerow) 
If ll_count <  ll_pagerow Then 
    for ll_row = 1 to ll_count  
       dw_print.insertrow(0)  //补足空行 
    next 
end If