AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > PowerBuilder

树形目录在PB中的实现与应用

51自学网 http://www.51zixue.net

  程序具体实现分成以下几个步骤:

  第一步:在tv_dir的constructor事件中生成所有驱动器信息,代码如下:

//显示所有驱动器,并加入目录树中
treeviewitem ltvi_new
int li_count, i
String ls_DriverType
lb_dir.DirList("",16384)
li_count = lb_dir.totalitems()
IF li_count > 0 then
for i = 1 to li_count
ltvi_New.Label = lb_dir.Text(i)
ltvi_New.Data = i -1
ltvi_New.Children = TRUE
//根据驱动器类型设置图标
ls_DriverType=uf_drivetype(lb_dir.Text(i))
//函数uf_drivetype用来获取驱动器类型,根据类型设置图标
Choose case ls_DriverType
case "REMOVABLE"
ltvi_New.PictureIndex = 5
ltvi_New.SelectedPictureIndex =5
CASE "FIXED"
ltvi_New.PictureIndex = 1
ltvi_New.SelectedPictureIndex =2
CASE "CDROM"
ltvi_New.PictureIndex = 6
ltvi_New.SelectedPictureIndex =6
END CHOOSE
This.InsertItemlast(0, ltvi_New)
next
This.selectitem(1)
End if

  第二步,在tv_dir的itempopulate事件中生成相应目录的下级目录信息:

String ls_Driver,ls_Data
Int li_level,li_num,li_i,li_new_level
TreeViewItem ltvi_choose
treeviewitem ltvi_new,ltvi_parent
long ll_handle
//获取当前所选目录
tv_dir.GetItem(handle,ltvi_choose)
li_Level = ltvi_Choose.Level
lb_dir.dirlist("",0)
//
IF li_level=1 Then //选择目录树中的驱动器
ls_Driver=Mid(ltvi_choose.label,3,1)
lb_dir.DirList(ls_Driver+":/",16+32768) //显示当前目录子目录信息
li_num=lb_Dir.totalitems()
For li_i=1 to li_num
ls_Data=lb_dir.Text(li_i)
If Mid(ls_data,1,1)="[" Then
ltvi_New.Label =mid(ls_data,2,len(ls_data) -2 )
ltvi_New.Data = 1
LB_next.dirlist(ls_driver+":/"+ltvi_New.Label,16+32768)
if lb_next.totalitems()>1 Then
ltvi_New.Children = TRUE
else
ltvi_New.Children = False
End if
ltvi_New.PictureIndex = 3
ltvi_New.SelectedPictureIndex =4
tv_dir.InsertItemlast(handle, ltvi_New)
End if
Next
End If

IF li_level>=2 Then //选择目录树中的子目录
ls_driver=uf_get_dir(li_level,tv_dir,handle,ltvi_choose.label)
lb_dir.DirList(ls_Driver,16+32768) //显示当前目录子目录信息
li_num=lb_Dir.totalitems()
For li_i=1 to li_num
ls_Data=lb_dir.Text(li_i)
If Mid(ls_data,1,1)="[" and mid(ls_data,2,2)<>".." Then
ltvi_New.Label =mid(ls_data,2,len(ls_data) -2 )
ltvi_New.Data = 1
LB_next.dirlist(ls_driver+"/"+ltvi_New.Label,16+32768)
if lb_next.totalitems()>1 Then
ltvi_New.Children = TRUE
else
ltvi_New.Children = False
End if
ltvi_New.PictureIndex = 3
ltvi_New.SelectedPictureIndex =4
tv_dir.InsertItemlast(handle, ltvi_New)
End if
Next
End If

  第三步:当用户单击某一具体目录时,显示该目录中满足条件的文件名,代码如下:

//tv_dir中Click事件代码
Int li_num,li_i,li_long,i
TreeViewItem ltvi_cur
String ls_dir,ls_data
String Filetype[8]
//设定需要显示的文件类型
filetype[1]=".htm"
filetype[2]=".html"
filetype[4]=".swf"
filetype[5]=".gif"
filetype[6]=".jpg"
filetype[7]=".shtml"
filetype[8]=".txt"
li_long=8
this.GetItem(handle,ltvi_cur)
If ltvi_cur.level=1 then
 ls_dir=mid(ltvi_cur.Label,3,1)+":/"
else
 ls_dir=uf_get_dir(ltvi_cur.level,this,handle,ltvi_cur.Label)
end if
is_dir=ls_dir
Lb_next.DirList(ls_dir+"/*.*",0+1+2+4+32) //显示所有文件
li_num=lb_Dir.totalitems()
lb_file.Reset()
//将指定文件显示在列表框中
For li_i=1 to li_num
 ls_Data=lb_next.Text(li_i)
 For i=1 to li_long
  if match(lower(Right(ls_data,len(FileType[i]))),Filetype[i]) then lb_file.addItem(ls_data)
Next
Next

  第四步:在OLE控件中显示所选文件的内容,主要代码如下:

//显示文件内容
String ls_filename
If len(is_dir)=3 Then
 ls_filename=is_dir+this.text(index)
Else
 ls_filename=is_dir+"/"+this.text(index)
End if
ole_disp.object.navigate(ls_filename)

  其中程序中使用的uf_get_dir为用户自定义函数,该函数用来获取所选定目录的完整路径。

//用户自定义函数
function string uf_get_dir (integer pi_level, treeview tv_dir, long handle, string ps_str);
 TreeViewItem ltvi_parent
 Int li_new_level
 Long ll_handle
 String ls_driver
 //找到当前级别的上一级的信息
 ls_driver=ps_str
 li_new_level=pi_level
 ll_handle=handle
 do while li_new_level>1
  ll_handle=tv_dir.FindItem(ParentTreeItem!,ll_handle)
  tv_dir.GetItem(ll_handle,ltvi_parent)
  li_new_level=ltvi_parent.level
  if li_new_level=1 then
   ls_driver=Mid(ltvi_parent.label,3,1)+":/"+ls_driver
  else
   ls_driver=ltvi_parent.label+"/"+ls_driver
  end if
 loop
 Return ls_driver

  3.结束语

  采用上文介绍的方法可实现了树形目录结构,如信息系统的数据备份中的目录选择、照片浏览选择等,取得了较好的应用效果。本文提到的方法在实际运用可进一步完善,如可将树形目录制作成PB中的用户自定义对象,可方便地在各种地方使用。

 
 

上一篇:用PowerBuilder实现多层C/S系统  下一篇:PB中用Windows&nbsp;API制作位图菜单