《HitTest 方法(ListView 和 TreeView 控件)示例讲解》由会员分享,可在线阅读,更多相关《HitTest 方法(ListView 和 TreeView 控件)示例讲解(2页珍藏版)》请在金锄头文库上搜索。
1、HitTest 方法ListView 和 TreeView控件例如讲解这个例子在 TreeView 控件中添加几个 Node 对象。在选择了一个 Node 对象之后,可以将它拖动到任何其它 Node。为试用此例,要在窗体上安放 TreeView 和 ImageList 控件, 并在窗体的声明局部粘贴该代码。运行此例,并且在四周拖动一些 Node 对象来观看结果。”声明全局变量。Dim indrag As Boolean ”指示拖放操作的标志。Dim nodX As Object ”要拖动的项。Private Sub Form_Load()”在 Imagelist 控件中加载一个位图。Dim i
2、mgX As ListImage Dim BitmapPath As StringBitmapPath = “iconsmailmail01a.ico“Set imgX = imagelist1.ListImages.Add(, , LoadPicture(BitmapPath)”初始化 TreeView 控件,并创立假设干节点。TreeView1.ImageList = imagelist1 Dim nodX As Node”创立树。Set nodX = TreeView1.Nodes.Add(, , , “Parent1“, 1) Set nodX = TreeView1.Nodes.Ad
3、d(, , , “Parent2“, 1)SetnodX=TreeView1.Nodes.Add(1,tvwChild,“Child1“,1)SetnodX=TreeView1.Nodes.Add(1,tvwChild,“Child2“,1)SetnodX=TreeView1.Nodes.Add(2,tvwChild,“Child3“,1)SetnodX=TreeView1.Nodes.Add(2,tvwChild,“Child4“,1)SetnodX=TreeView1.Nodes.Add(3,tvwChild,“Child5“,1)nodX.EnsureVisible ”开放树以显示全部节
4、点。End SubPrivate Sub TreeView1_MouseDown_(Button As Integer, Shift As Integer, x As Single, y As Single) Set nodX = TreeView1.SelectedItem ”设置要拖动的项。End SubPrivate Sub TreeView1_MouseMove _(Button As Integer, Shift As Integer, x As Single, y As Single)If Button = vbLeftButton Then ”指示一个拖动操作。indrag =
5、True ”设置 flag 为 true。”使用CreateDragImage 方法设置拖动图标。TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage TreeView1.Drag vbBeginDrag ”拖动操作。End If End SubPrivate Sub TreeView1_DragDrop_(Source As Control, x As Single, y As Single) If TreeView1.DropHighlight Is Nothing ThenSet TreeView1.DropHighlig
6、ht = Nothing indrag = FalseExit Sub ElseIf nodX = TreeView1.DropHighlight Then Exit Sub ClsPrint nodX.Text & “ dropped on “ & TreeView1.DropHighlight.Text Set TreeView1.DropHighlight = Nothingindrag = False End IfEnd SubPrivate Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, Sta te As Integer)If indrag = True Then”设置 DropHighlight 为鼠标坐标。Set TreeView1.DropHighlight = TreeView1.HitTest(x, y) End IfEnd Sub