Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

DragDrop Sample (C#)

This sample illustrates drag & drop.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\DragDrop\Form1.cs or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\DragDrop\Form1.cs (on 32-bit Windows versions).


        private void Command1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {
            DropTargetPic.AllowDrop = true;
            int i;
            axSftTree1.Items.Add("Item 0");
            i = axSftTree1.Items.Add("Item 1");
            axSftTree1.get_Item(i).Level = 1;
            i = axSftTree1.Items.Add("Item 2");
            axSftTree1.get_Item(i).Level = 2;
            i = axSftTree1.Items.Add("Item 3");
            axSftTree1.get_Item(i).Level = 1;
        }

        private void axSftTree1_DragStarting(object sender, AxSftTreeLib75._DSftTreeEvents_DragStartingEvent e) {

            axSftTree1.CancelMode();    // .NET controls drag&drop, so tree control needs to cancel

            // This section demonstrates drag (&drop) using the .NET DoDragDrop method
            // THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 1 - Manual ) or SftTreeDragMethodConstants.dragSftTreeManual

            int curr = axSftTree1.Items.Current;
            // cell text
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Text, axSftTree1.get_Cell(curr, 0).Text);

            // item picture
            SftPictureObject img = null;
            if (axSftTree1.get_Item(curr).Image.Type == SftPictureTypeConstants.sftTypeIDispatch) {
                img = axSftTree1.get_Item(curr).Image;
            } else {
                if (axSftTree1.get_Item(curr).Expanded)
                    img = axSftTree1.Items.ItemImageExpanded;
                else if (axSftTree1.get_Item(curr).DependentAllCount > 0)
                    img = axSftTree1.Items.ItemImageExpandable;
                else
                    img = axSftTree1.Items.ItemImageLeaf;
            }
            Image sendingImage = OLECvt.ToImage(img.Picture);
            data.SetData(DataFormats.Bitmap, true, sendingImage);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void axSftTree1_OLEStartDrag(object sender, AxSftTreeLib75._DSftTreeEvents_OLEStartDragEvent e) {

            // This section demonstrates drag&drop) using OLE mechanisms built into this control
            // THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 3 - OLEDrag ) or SftTreeDragMethodConstants.dragSftTreeOLE

            int curr = axSftTree1.Items.Current;
            // cell text
            e.data.SetData(axSftTree1.get_Cell(curr, 0).Text, SftOLEClipboardConstants.sftCFText);
            // item picture
            SftPictureObject img = null;
            if (axSftTree1.get_Item(curr).Image.Type == SftPictureTypeConstants.sftTypeIDispatch) {
                img = axSftTree1.get_Item(curr).Image;
            } else {
                if (axSftTree1.get_Item(curr).Expanded)
                    img = axSftTree1.Items.ItemImageExpanded;
                else if (axSftTree1.get_Item(curr).DependentAllCount > 0)
                    img = axSftTree1.Items.ItemImageExpandable;
                else
                    img = axSftTree1.Items.ItemImageLeaf;
            }
            e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFDIB);
            e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFBitmap);
            e.allowedEffects = 3;
        }

        private void DropTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.Text, true) || e.Data.GetDataPresent(DataFormats.Bitmap, true))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void DropTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
            String[] arrayOfFormats = e.Data.GetFormats(true); // informational
            if (e.Data.GetDataPresent(DataFormats.Text, true))
                DropTarget.Text = (string) e.Data.GetData(DataFormats.Text, true);
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true))
                DropTargetPic.Image = (Image) e.Data.GetData(DataFormats.Bitmap, true);
        }

        private void DropTargetPic_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void DropTargetPic_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
            String[] arrayOfFormats = e.Data.GetFormats(true); // informational
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true)) {
                object o = e.Data.GetData(DataFormats.Bitmap, true);
                DropTargetPic.Image = (Image) o;
            }
        }

        private void axSftTree1_OLEDragDrop(object sender, AxSftTreeLib75._DSftTreeEvents_OLEDragDropEvent e) {

            // get horizontal extent and width of column 0
            int horzExtent = axSftTree1.Items.HorizontalExtentPix;
            int firstColumnWidth = axSftTree1.get_Column(0).WidthPix;

            int insertAt = axSftTree1.Items.DropHighlight;
            if (insertAt < 0) return;
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFText)) {
                short lvl = axSftTree1.get_Item(insertAt).Level;
                string str = (string) e.data.GetData(SftOLEClipboardConstants.sftCFText);
                int newItem = axSftTree1.Items.Insert(insertAt + 1, str);
                axSftTree1.get_Item(newItem).Level = (short) (lvl + 1);
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFDIB)) {
                axSftTree1.get_Cell(insertAt, 0).Image.Picture = (stdole.IPictureDisp)e.data.GetData((short) SftOLEClipboardConstants.sftCFDIB);
                axSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFBitmap)) {
                axSftTree1.get_Cell(insertAt, 0).Image.Picture = (stdole.IPictureDisp)e.data.GetData((short) SftOLEClipboardConstants.sftCFBitmap);
                axSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFFiles)) {
                axSftTree1.BulkUpdate = true;
                short lvl = axSftTree1.get_Item(insertAt).Level;
                int newItem = insertAt + 1;
                for (int i = e.data.Files.Count ; i >= 1 ; --i) {
                    newItem = axSftTree1.Items.Insert(newItem, e.data.Files[i]);
                    axSftTree1.get_Item(newItem).Level = (short) (lvl + 1);
                }
                axSftTree1.BulkUpdate = false;
            }

            // make horizontal extent and width of column 0 wider than previous
            // setting, but never smaller
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();

            if (horzExtent > axSftTree1.Items.HorizontalExtent)
                axSftTree1.Items.HorizontalExtent = horzExtent;
            if (firstColumnWidth > axSftTree1.get_Column(0).WidthPix)
                axSftTree1.get_Column(0).WidthPix = firstColumnWidth;
        }

        private void Picture1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Bitmap, true, Picture1.Image);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void Picture2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Dib, true, Picture2.Image);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void Text1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Text, Text1.Text);
            DoDragDrop(data, DragDropEffects.Copy);
        }

    }
}

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.