第三十一讲 深入窗体 PowerBuild中拖放技术的应用(二) 打印本页  
 
  我们现在继续来介绍在PowerBuild中拖放技术的应用。
  
  现在在TreeView控件中编写核心程序。这些程序是实现拖放技术的关键。
  选择tv_1控件的itempopulate事件,编程
  Integer li_Rows, li_Cnt
  TreeViewItem ltvi_Dept, ltvi_Emp
  If GetItem(handle, ltvi_Dept) = -1 Then Return
  SetPointer(HourGlass!)
  
  // Populate the department with its employees
  ids_Source.Reset()
  li_Rows = ids_Source.Retrieve(Integer(ltvi_Dept.data))
  
  ltvi_Emp.pictureindex = 6
  ltvi_Emp.selectedpictureindex = 6
  ltvi_Emp.children = false
  
  For li_Cnt = 1 To li_Rows
  ltvi_Emp.label = ids_Source.Object.emp_name[li_Cnt]
  ltvi_Emp.data = ids_Source.Object.emp_id[li_Cnt]
  InsertItemSort(handle, ltvi_Emp)
  Next
  
  现在开始编写拖放技术的程序了。
  tv_1控件的begnidrag事件,编程
  TreeViewItem ltvi_Source
  GetItem(handle, ltvi_Source)
  If ltvi_Source.Level <> 2 Then
  This.Drag(Cancel!)
  Else
  il_DragSource = handle
  il_DragParent = FindItem(ParentTreeItem!, handle)
  End If
  选择tv_1控件的dragwithin事件,编程:
  TreeViewItem ltvi_Over
  If GetItem(handle, ltvi_Over) = -1 Then
  SetDropHighlight(0)
  il_DropTarget = 0
  Return
  End If
  If ltvi_Over.level = 1 Then
  If handle <> il_DragParent Then
  SetDropHighlight(handle)
  il_DropTarget = handle
  Else
  SetDropHighlight(0)
  il_DropTarget = 0
  End If
  Else
  il_DropTarget = FindItem(ParentTreeItem!, handle)
  If il_DropTarget <> il_DragParent Then
  SetDropHighlight(il_DropTarget)
  Else
  SetDropHighlight(0)
  il_DropTarget = 0
  End If
  End If
  选择tv_1控件的dragdrop事件,编程:
  Integer li_Pending
  Long ll_NewItem
  TreeViewItem ltvi_Target, ltvi_Source, ltvi_Parent, ltvi_New
  If GetItem(il_DropTarget, ltvi_Target) = -1 Then Return
  If GetItem(il_DragSource, ltvi_Source) = -1 Then Return
  GetItem(il_DragParent, ltvi_Parent)
  If MessageBox("Transfer Employee", "Are you sure you want to transfer " + &
  ltvi_Source.Label + " from " + ltvi_Parent.label + " to " + ltvi_Target.label + &
  "?", Question!, YesNo!) = 2 Then Return
  li_Pending = UpperBound(istr_PendingXfers) + 1
  istr_PendingXfers[li_Pending].i_EmpID = ltvi_Source.Data
  istr_PendingXfers[li_Pending].i_NewDept = ltvi_Target.Data
  DeleteItem(il_DragSource)
  SetNull(ltvi_Source.ItemHandle)
  ll_NewItem = InsertItemSort(il_DropTarget, ltvi_Source)
  SelectItem(ll_NewItem)
  
  选择“update”按钮的clicked事件
  Integer li_Cnt, li_Xfers
  li_Xfers = UpperBound(istr_PendingXfers)
  For li_Cnt = 1 To li_Xfers
  UPDATE "employee"
  SET "dept_id" = :istr_PendingXfers[li_Cnt].i_NewDept
  WHERE "employee"."emp_id" = :istr_PendingXfers[li_Cnt].i_EmpID ;
  If sqlca.sqlcode <> 0 Then
  MessageBox("Update Error", "Error updating employees: " +& String(sqlca.sqlcode) +" - " + sqlca.sqlerrtext, Exclamation!)
  Rollback;
  End If
  Next
  Commit;
  Close(Parent)
  小结:拖放是一种执行应用程序中的任务的显著方法。遵循的原则是源对象和目标对象完成的操作之间关系,是直观的和有意义的。另外,拖放不应成为完成某项任务的唯一方法,还应提供键盘或菜单选项完成同样功能,这样能为用户提供更多的灵活性。