>************************************************************** >*myForm.Init() >*this method code comes from Doug Hennig's article in June's 97 FoxTalk > >* Calculate the conversion factor between VFP window units (in pixels) and >* TreeView window units (in twips). > >LOCAL liHWnd, ; > liHDC, ; > liPixelsPerInchX, ; > liPixelsPerInchY > >* Define some constants. > >#DEFINE cnLOG_PIXELS_X 88 >* From WINGDI.H >#DEFINE cnLOG_PIXELS_Y 90 >* From WINGDI.H >#DEFINE cnTWIPS_PER_INCH 1440 >* 1440 twips per inch > >* Declare some Windows API functions. > >DECLARE integer GetActiveWindow in WIN32API >DECLARE integer GetDC in WIN32API ; > integer iHDC >DECLARE integer GetDeviceCaps in WIN32API ; > integer iHDC, integer iIndex > >* Get a device context for VFP. > >liHWnd = GetActiveWindow() >liHDC = GetDC(liHWnd) > >* Get the pixels per inch. > >liPixelsPerInchX = GetDeviceCaps(liHDC, cnLOG_PIXELS_X) >liPixelsPerInchY = GetDeviceCaps(liHDC, cnLOG_PIXELS_Y) > >* Get the twips per pixel. > >WITH This > .nTreeFactorX = cnTWIPS_PER_INCH/liPixelsPerInchX > .nTreeFactorY = cnTWIPS_PER_INCH/liPixelsPerInchY >ENDWITH >DoDefault() >*************************** > >What about images in the ListView - by default TreeView references them by numbers, but just give the names for images in ImageList in the Key property and you will be able to reference them by that name or numeric index. > >I took some sample code from my form which represents user groups and group members in the TreeView and supports DragDrop from listbox lstUser and within the treeview. > >I have to say that in the latest MS TreeView versions HitTest doesn't work properly, returning always empty string instead of .NULL. or node reference. I didn't figure out yet what is wrong. >Version of the MS TreeView shipped with VFP 5.0 works properly. > > >**********************************************************// >Sample of TreeView.MouseDown : > >*** OLE Control Event *** >LPARAMETERS button, shift, x, y >local loNode >if Button = 1 > loNode = This.HitTest(X * Thisform.nTreeFactorX, Y * Thisform.nTreeFactorX) > if not isnull(loNode) && and loNode.Key <> This.SelectedItem.Key > loNode.Selected = .T. > thisform.cDraggedNodeKey = loNode.Key > thisform.cDraggedNodeText = loNode.Text > else > thisform.cDraggedNodeKey = "" > thisform.cDraggedNodeText = "" > endif not isnull(loNode) > >* The user pressed the mouse button, so record the current mouse position so >* we can later determine if the mouse moved enough to start a drag operation. > >endif Button = 1 >Thisform.nMouseX = X >Thisform.nMouseY = Y > > >********************************************************************** >Sample of TreeView.MouseMove: > >******************************************* >*** OLE Control Event *** >LPARAMETERS button, shift, x, y > >* If the left mouse button is pressed and the user has moved the mouse (since >* the button was first pressed) by more than our defined threshold, start the >* drag operation. > >with Thisform > if Button = 1 and (abs(X - .nMouseX) > .nDragThreshold or ; > abs(Y - .nMouseY) > .nDragThreshold) > * we need to trap the selected node, because > * MouseDown and MouseMove happen when we click on + sign to open node > IF !EMPTY(thisform.cDraggedNodeKey) > This.Drag() > ENDIF > endif Button = 1 ... >endwith > >*************************************************** > >Sample of TreeView.DragDrop: > >*************************** > >LPARAMETERS oSource, nXCoord, nYCoord >SELECT rv_us_groups >LOCAL loNode, oNewNode >oNewNode=.NULL. >IF oSource.Name == lstUser' > loNode = This.HitTest(nXCoord * .nTreeFactorX, nYCoord * .nTreeFactorX) > IF not isnull(loNode) > loNode.Selected = .T. > * > *your code here > * > ENDIF >ENDIF oSource.Name == 'lstUser' > > >IF oSource.Name == 'TreeView' > loNode = This.HitTest(nXCoord * .nTreeFactorX, nYCoord * .nTreeFactorX) > IF not isnull(loNode) > loNode.Selected = .T. > > ************************** > IF not isnull(loNode) and loNode.Key <> thisform.cDraggedNodeKey > *with this part you can move nodes within the treeview > loNode.Selected = .T. > IF isnull(loNode.parent) AND loNode.Image="group" && add moved *node only to first node level and only if it is a group > this.Nodes.Remove(thisform.cDraggedNodeKey) > oNewNode=this.Nodes.add(loNode,4,thisform.cDraggedNodeKey, thisform.cDraggedNodeText,"user") > > oNewNode.EnsureVisible() > oNewNode.Selected = .t. > ENDIF > > ENDIF > ENDIF >ENDIF oSource.Name == 'TreeView' >thisform.cDraggedNodeKey = "" >thisform.cDraggedNodeText = "" > >********************************************************************** > >Sample of TreeView.DragOver: > >****************************** >LPARAMETERS oSource, nXCoord, nYCoord, nState >LOCAL loNode >IF oSource.Name == "lstUser" > WITH Thisform > loNode = This.HitTest(nXCoord * .nTreeFactorX, nYCoord * .nTreeFactorX) > IF !isnull(loNode) > loNode.Selected = .T. > * NoDrop cursor when mouse is over group member > oSource.DragIcon = IIF(loNode.Image = "user","no_l.cur","dragmove.cur") > ELSE > oSource.DragIcon = "dragmove.cur" && if cursor is outside nodes > ENDIF !isnull(loNode) ... > ENDWITH >ENDIF > >IF oSource.Name == "TreeView" > WITH Thisform > loNode = This.HitTest(nXCoord * .nTreeFactorX, nYCoord * .nTreeFactorX) > loNode.Selected = .T. > this.DragIcon = IIF(loNode.Image == "group" ,"dragmove.cur","no_l.cur") > ELSE > this.DragIcon = "dragmove.cur" > ENDIF !isnull(loNode) ... > ENDWITH >ENDIF >************************************************************************************ >>HTH,