问题描述
靠近左边正常靠近中间,拖动图像逐渐偏离靠右边看不见拖动图像了这个估计是panel1的坐标跟主窗口坐标冲突吧?下面是代码:看看有什么问题privatevoidtreeView_ItemDrag(objectsender,System.Windows.Forms.ItemDragEventArgse){//Getdragnodeandselectitthis.dragNode=(TreeNode)e.Item;this.treeView1.SelectedNode=this.dragNode;//Resetimagelistusedfordragimagethis.imageListDrag.Images.Clear();this.imageListDrag.ImageSize=newSize(this.dragNode.Bounds.Size.Width+this.treeView1.Indent,this.dragNode.Bounds.Height);//Createnewbitmap//ThisbitmapwillcontainthetreenodeimagetobedraggedBitmapbmp=newBitmap(this.dragNode.Bounds.Width+this.treeView1.Indent,this.dragNode.Bounds.Height);//GetgraphicsfrombitmapGraphicsgfx=Graphics.FromImage(bmp);//Drawnodeiconintothebitmapgfx.DrawImage(this.imageListTreeView.Images[0],0,0);//Drawnodelabelintobitmapgfx.DrawString(this.dragNode.Text,this.treeView1.Font,newSolidBrush(this.treeView1.ForeColor),(float)this.treeView1.Indent,1.0f);//Addbitmaptoimagelistthis.imageListDrag.Images.Add(bmp);//GetmousepositioninclientcoordinatesPointp=this.treeView1.PointToClient(Control.MousePosition);//Computedeltabetweenmousepositionandnodeboundsintdx=p.X+this.treeView1.Indent-this.dragNode.Bounds.Left;intdy=p.Y-this.dragNode.Bounds.Top;//Begindraggingimageif(DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle,0,dx,dy)){//Begindraggingthis.treeView1.DoDragDrop(bmp,DragDropEffects.Move);//EnddraggingimageDragHelper.ImageList_EndDrag();}}privatevoidtreeView1_DragDrop(objectsender,System.Windows.Forms.DragEventArgse){//UnlockupdatesDragHelper.ImageList_DragLeave(this.treeView1.Handle);//GetdropnodeTreeNodedropNode=this.treeView1.GetNodeAt(this.treeView1.PointToClient(newPoint(e.X,e.Y)));//Ifdropnodeisn'tequaltodragnode,adddragnodeaschildofdropnodeif(this.dragNode!=dropNode){//Removedragnodefromparentif(this.dragNode.Parent==null){this.treeView1.Nodes.Remove(this.dragNode);}else{this.dragNode.Parent.Nodes.Remove(this.dragNode);}//AdddragnodetodropnodedropNode.Nodes.Add(this.dragNode);dropNode.ExpandAll();//Setdragnodetonullthis.dragNode=null;//Disablescrolltimerthis.timer.Enabled=false;}}privatevoidtreeView1_DragEnter(objectsender,System.Windows.Forms.DragEventArgse){DragHelper.ImageList_DragEnter(this.treeView1.Handle,e.X-this.treeView1.Left,e.Y-this.treeView1.Top);//Enabletimerforscrollingdraggeditemthis.timer.Enabled=true;}privatevoidtreeView1_DragLeave(objectsender,System.EventArgse){DragHelper.ImageList_DragLeave(this.treeView1.Handle);//Disabletimerforscrollingdraggeditemthis.timer.Enabled=false;}privatevoidtreeView1_DragOver(objectsender,System.Windows.Forms.DragEventArgse){//ComputedragpositionandmoveimagePointformP=this.PointToClient(newPoint(e.X,e.Y));DragHelper.ImageList_DragMove(formP.X-this.treeView1.Left,formP.Y-this.treeView1.Top);//GetactualdropnodeTreeNodedropNode=this.treeView1.GetNodeAt(this.treeView1.PointToClient(newPoint(e.X,e.Y)));if(dropNode==null){e.Effect=DragDropEffects.None;return;}e.Effect=DragDropEffects.Move;//ifmouseisonanewnodeselectitif(this.tempDropNode!=dropNode){DragHelper.ImageList_DragShowNolock(false);this.treeView1.SelectedNode=dropNode;DragHelper.ImageList_DragShowNolock(true);tempDropNode=dropNode;}//AvoidthatdropnodeischildofdragnodeTreeNodetmpNode=dropNode;while(tmpNode.Parent!=null){if(tmpNode.Parent==this.dragNode)e.Effect=DragDropEffects.None;tmpNode=tmpNode.Parent;}}privatevoidtreeView1_GiveFeedback(objectsender,System.Windows.Forms.GiveFeedbackEventArgse){if(e.Effect==DragDropEffects.Move){//Showpointercursorwhiledragginge.UseDefaultCursors=false;this.treeView1.Cursor=Cursors.Default;}elsee.UseDefaultCursors=true;}publicclassDragHelper{[DllImport("comctl32.dll")]publicstaticexternboolInitCommonControls();[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternboolImageList_BeginDrag(IntPtrhimlTrack,intiTrack,intdxHotspot,intdyHotspot);[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternboolImageList_DragMove(intx,inty);[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternvoidImageList_EndDrag();[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternboolImageList_DragEnter(IntPtrhwndLock,intx,inty);[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternboolImageList_DragLeave(IntPtrhwndLock);[DllImport("comctl32.dll",CharSet=CharSet.Auto)]publicstaticexternboolImageList_DragShowNolock(boolfShow);staticDragHelper(){InitCommonControls();}}
解决方案
本帖最后由 u014325761 于 2014-07-17 02:33:40 编辑
解决方案二:
// Add bitmap to imagelist this.imageListDrag.Images.Add(bmp); // Get mouse position in client coordinates Point p = this.treeView1.PointToClient(Control.MousePosition); // Compute delta between mouse position and node bounds int dx = p.X + this.treeView1.Indent - this.dragNode.Bounds.Left; int dy = p.Y - this.dragNode.Bounds.Top;应该是list控件大小的问题你再放大点看看,坐标的位置X取得小一些
解决方案三:
不行啊,treeView1只要不放在panel1,任意大小,任意位置都有拖动效果
解决方案四:
我想问题出在这里DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle,0,dx,dy)BOOLWINAPIImageList_BeginDrag(HIMAGELISThimlTrack,intiTrack,intdxHotspot,intdyHotspot);
dxHotspotanddyHotspot[in]Specifiesthelocationofthedragpositionrelativetotheupper-leftcorneroftheimage.//dx,dy是相对于父窗口的坐标,放在容器里他就把容器当作父做口了,容器位置改变是,图像就发生偏移了.但是这个坐标这么转换呢?