C#拖动和拖放:显示拖动项目的同时拖动 [英] C# Drag-and-Drop: Show the dragged item while dragging

查看:229
本文介绍了C#拖动和拖放:显示拖动项目的同时拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在C#桌面应用程序与Windows窗体。我有一个自定义的控制,我希望能够拖动和我的应用程序中拖放(而不是外)。现在,我实现了与通常的DoDragDrop / OnDragOver / OnDragDrop方法。有没有什么办法,因为它得到周围拖连续绘制控制 - 那种你使用jQuery的拖放和拖放看到了什么?我想实际控制留在原地,但我想用户拖动它来画它的外观的副本。理想情况下,复制甚至会是半透明的,但是这更像是一个不错的。

I'm building a desktop app in C# with Windows Forms. I have a custom Control, and I'd like to be able to drag and drop it within my application (not outside). Right now I'm implementing that with the usual DoDragDrop/OnDragOver/OnDragDrop methods. Is there any way to continuously paint the control as it gets dragged around--sort of what you see with JQuery's drag-and-drop? I want the actual control to stay in place, but I want to paint a copy of its appearance as the user drags it. Ideally the copy would even be semi-transparent, but that's more a "nice to have."

我能想到做到这一点的唯一方法是把在主窗体的OnPaint方法油漆code,但是这似乎是一个不雅的解决方案。任何其他的想法?事情变得更容易,如果控制涂料本身只是一个位图?

The only way I can think to do this is to put the paint code in the main form's OnPaint method, but that seems like an inelegant solution. Any other ideas? Are things any easier if the Control paints itself as just a Bitmap?

推荐答案

我认为我应该回来,并回答这个问题我自己,因为我没有得到它最终的工作。

I thought I should come back and answer this myself, since I did get it working eventually.

我创建了这些功能的CursorUtil类:

I created a CursorUtil class with these functions:

public struct IconInfo {
    public bool fIcon;
    public int xHotspot;
    public int yHotspot;
    public IntPtr hbmMask;
    public IntPtr hbmColor;
}

public class CursorUtil {
    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    [DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr handle);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    extern static bool DestroyIcon(IntPtr handle);

    // Based on the article and comments here:
    // http://www.switchonthecode.com/tutorials/csharp-tutorial-how-to-use-custom-cursors
    // Note that the returned Cursor must be disposed of after use, or you'll leak memory!
    public static Cursor CreateCursor(Bitmap bm, int xHotspot, int yHotspot) {
        IntPtr cursorPtr;
        IntPtr ptr = bm.GetHicon();
        IconInfo tmp = new IconInfo();
        GetIconInfo(ptr, ref tmp);
        tmp.xHotspot = xHotspot;
        tmp.yHotspot = yHotspot;
        tmp.fIcon = false;
        cursorPtr = CreateIconIndirect(ref tmp);

        if (tmp.hbmColor != IntPtr.Zero) DeleteObject(tmp.hbmColor);
        if (tmp.hbmMask != IntPtr.Zero) DeleteObject(tmp.hbmMask);
        if (ptr != IntPtr.Zero) DestroyIcon(ptr);

        return new Cursor(cursorPtr);
    }

    public static Bitmap AsBitmap(Control c) {
        Bitmap bm = new Bitmap(c.Width, c.Height);
        c.DrawToBitmap(bm, new Rectangle(0, 0, c.Width, c.Height));
        return bm;
    }

然后我写了一个拖动类(也不是面向对象的,唉,但我想你只能在一个桌面应用程序拖到一件事的时间)。这是一个有点那个code的:

Then I wrote a Drag class (also not object-oriented, alas, but I figured you can only drag one thing at a time in a desktop app). Here is a bit of that code:

    public static void StartDragging(Control c) {
        Dragged = c;
        DisposeOldCursors();
        Bitmap bm = CursorUtil.AsBitmap(c);
        DragCursorMove = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y);      
        DragCursorLink = CursorUtil.CreateCursor((Bitmap)bm.Clone(), DragStart.X, DragStart.Y);      
        DragCursorCopy = CursorUtil.CreateCursor(CursorUtil.AddCopySymbol(bm), DragStart.X, DragStart.Y);
        DragCursorNo = CursorUtil.CreateCursor(CursorUtil.AddNoSymbol(bm), DragStart.X, DragStart.Y);
        //Debug.WriteLine("Starting drag");
    }   

    // This gets called once when we move over a new control,
    // or continuously if that control supports dropping.
    public static void UpdateCursor(object sender, GiveFeedbackEventArgs fea) {
        //Debug.WriteLine(MainForm.MousePosition);
        fea.UseDefaultCursors = false;
        //Debug.WriteLine("effect = " + fea.Effect);
        if (fea.Effect == DragDropEffects.Move) {
            Cursor.Current = DragCursorMove;

        } else if (fea.Effect == DragDropEffects.Copy) {
            Cursor.Current = DragCursorCopy;

        } else if (fea.Effect == DragDropEffects.None) {
            Cursor.Current = DragCursorNo;

        } else if (fea.Effect == DragDropEffects.Link) {
            Cursor.Current = DragCursorLink;

        } else {
            Cursor.Current = DragCursorMove;
        }
    }

当您设置控件,您可以使用这些方法,例如在构造函数中:

You can use these methods when you set up your controls, for example in the constructor:

GiveFeedback += new GiveFeedbackEventHandler(Drag.UpdateCursor);

和在该方法中:

    protected override void OnMouseMove(MouseEventArgs mea) {
        if (Drag.IsDragging(mea)) {
            Drag.StartDragging(this);
            DragDropEffects dde = DoDragDrop(Plan, DragDropEffects.Move | DragDropEffects.Copy);
            Drag.StopDragging();
        }
    }

这篇关于C#拖动和拖放:显示拖动项目的同时拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆