用Visual Basic编程建立自己的回收站[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 shawls 的 blog

 

用visual basic编程建立自己的回收站

在windows 95中删除一个文件或文件夹,只需用鼠标拖动文件(夹)到回收站,然后松开鼠标按钮即可,这种可视化的操作给用户带来了很大方便。如果程序开发人员在自己的程序中能做到这样,在自己的程序中也增加一个和windows中风格相同的回收站,那么开发出的程序一定会受到用户的欢迎。怎么才能完成这种操作呢?本文介绍一种使用visual basic编程来完成这个动作的方法。

  在microsoft windows资料中将按下鼠标按钮移动对象称为拖,将鼠标按钮的松开称为放。在visual basic中,如果项目中使用多个窗体,可以把一个图标从一个窗体拖放到另一个窗体。控件允许两种拖动,这取决于dragmode特性的两个不同的值。缺省情况是dragmode特性的值为0,除特定情况外不允许拖动控件;将这个特性的值改成1(automatic)意味着用户可以拖动控件。

  下面介绍拖动鼠标删除文件方法的实现过程。在visual basic的项目中允许用户拖动代表文件的图标至处理单元,此单元相当于windows 95中的回收站,在给出警告之后把文件从磁盘中删除。

  第一步,首先启动一个有两个窗体的项目,在启动窗体的下角加一个图片框(或图像控件),将这个控件的picture特性设置成trash03.ico文件中的图标,该文件位于icon库的computer子目录中。如果使用图片框,将autosize特性设置成true(如果选择一个图像控件,将stretch特性设置成false)。

  第二步,建立两个控件数组,一个是控件名为filename的标签数组,另一个名字为files的图像控件数组。将图像控件的dragmode特性设置成1(automatic);将visble特性设置成false,使标签和图像控件在运行时都不可见。通常,这些控件数组中的所有后续元素都继承这些特性。

  这个项目中的大多数工作都体现在下面的dragdrop事件过程中:

  private sub garbage_dragdrop(source as control,x as single,y as single)
  ‘local variables
  dim msg as string
  dim controlindex as integer,yesno as integer
  controlindex=source.index
  forml.filename(controlindex).visble=false
  msg= "do you really want to delete"+
  forml.filename(controlindex).caption
  yesno=msgbox(msg,vbyesno, "confirmation box")
  if yesno=vbyes then
  kill(forml.filename(controlindex).caption)
  unload files(controlindex)
  unload forml.filename(controlindex)
  else
  forml.filename(controlindex).visible=true
  forml.file(controlindex).visible=true
  end if
  end sub

  该事件过程中的controlindex这一行代码找出被拖动控件的索引;接下来的两行代码使图片框和标签在放置操作后暂时不可见。controlindex变量让visual basic从标签数组中提取标题(也就是文件名)。消息框的类型为=vbyesno,因此是个yes/no消息框,标题是confirmation box。kill命令从磁盘中删除文件。一旦程序删除了文件,就从控件数组中卸除标签和图片框。如果用户操作有误,确认的消息框返回no,则程序代码使得原来的图片框和标签再次显现。

  下一步,建立一个称为directons的通用过程,在一个窗体上显示出给用户提供的这个应用程序所需的信息,该窗体用作一个定制对话框。

  sub directons ()
  forml.hide
  form2.show
  form2.cls
  form1.print "this program illustrates dragging and dropping mouse operations."
  form2.print "the user gives a file spec inside the message box and a form"
  form2.print "appears with icons labelled by all the files with that file specifcation."
  form2.print "the user can drag the icon to be 'flushed' away i. e. deleted."
  end sub

  对话框上ok按钮的click过程是:

  private sub btnok-click ()
  me.hide
  form1.show
  end sub

  启动窗体的form-load 过程需要装入directons窗体并调用显示信息的通用过程。它可以这样的:

  private sub form-load ()
  load form2
  directons
  end sub

  启动窗体上的file 菜单有以下两项:

  new file spec

  exit

  单击new file spec菜单项让用户查看新的目录或驱动器。这个菜单项会调用一个通用过程getfile,从磁盘读入文件名。

  最后,是如何处理新的文件说明。用户第一次使用这个应用程序时,visual basic装入两个控件数组中一定数目的元素。程序需要改变标题中所含的信息,这些标题含有要删除的文件的名字。这两个控件数组只装入一次,然后使用dir $命令建立一个字符串,从磁盘读入住处后将这个数组中的值赋给标签的caption特性。该程序代码要读磁盘信息两次来恰当地重新确定数组尺寸。至于菜单项,因为“n”加了下划线,用户可以按n作为激活这一项的简捷键。菜单打开后这个菜单的控件名设置成mnunewfilespec。这个控件的click事件过程是:

  private sub mnunewfilespec click
  unload form1
  load form1
  getfile
  end sub
  unload form1这个代码行清除控件数组的旧元素。getfile 处理过程是这样的:
  public sub getfile ( )
  'local variables
  dim filespec as string
  form2. hide
  filespesc=inputbox$ ("file specification?")
  if filespesc= "" then
  directons
  else
  form1.show
  displayfiles (filespec)
  end if
  end sub

  在启动窗体上显示文件的详细住处包含在一个通用过程displayfiles中,该过程只有一个参数,该过程如下:

  public sub displayfiles (filespec as string)
  ‘local variables
  dim nameoffile as string
  dim controlindex as integer
  form1 .show
  form1.width=8000
  form1.height=5500
  form1.garbage.move 7000,4200 'move the garbage can icon
  nameoffile=dir $(filespec)
  if nameoffile= " " then
  msgbox "no files found with that file specification
  end if
  controlindex= controlindex +1 'start with index=1
  do while nameogfile<> ""
  load form1.files(controlindex)
  load form1.filename(contro;index)
  column=(controlindex mod 6)
  row=(controlindex-1)\6
  form1.files(controlindex).move(1300*column)+275, 800*row+2 'to allow for menu
  form1.filename(controlindex).move 1300*column, 800*(row+1)
  form1.files(controlindex).visible=true
  form1.filename(controlindex).visible=true
  form1.filename(controlindex).caption=nameoffile
  controlindex= controlindex+1
  if controlindex >30 then
  msgbox("too many files!")
  exit do
  end if
  nameoffile=dir$
  loop
  end sub

  do循环实现将图片框及标签分隔开,分隔的间距通过用不同的值试验确定。就像控件数组中的所有新元素一样,在将visible特性设置成true之前,它们都是不可见的。

  为了结束程序,只需将事件过程与窗体上的quit菜单项关联,并增加一个查询unload事件来结束程序。

  sub mnuquit_click()
  unload form2
  unload me
  end sub
  sub form_unload(cancel as integer,unload mode as integer)
  end

本文关键:Visual Basic,回收站
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top