首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C#拖放:在拖拽的同时显示拖拽的项目

C#拖放:在拖拽的同时显示拖拽的项目
EN

Stack Overflow用户
提问于 2010-07-14 03:16:19
回答 4查看 33.6K关注 0票数 21

我正在使用Windows Forms在C#中构建一个桌面应用程序。我有一个自定义的控件,我希望能够将它拖放到我的应用程序中(而不是外部)。现在,我正在用通常的DoDragDrop/OnDragOver/OnDragDrop方法实现它。有没有办法在控件被拖来拖去时持续绘制它--就像您在JQuery的拖放中看到的那样?我希望实际的控件留在原地,但我希望在用户拖动它时绘制其外观的副本。理想情况下,副本甚至应该是半透明的,但这更像是一种“很好的拥有”。

我能想到的唯一方法就是将画图代码放在主窗体的OnPaint方法中,但这似乎是一个不太优雅的解决方案。还有其他想法吗?如果控件只将自己绘制为一个位图,事情会变得更简单吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-29 21:24:43

我想我应该回来自己回答这个问题,因为我最终确实让它工作了。

我创建了一个具有以下函数的CursorUtil类:

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;
    }

然后我写了一个Drag类(也不是面向对象的,但我认为在桌面应用程序中一次只能拖动一件事)。下面是其中一些代码:

    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;
        }
    }

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

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();
        }
    }
票数 15
EN

Stack Overflow用户

发布于 2014-07-09 07:04:31

这可能是一种选择:

private void btntarget_MouseDown(object sender, MouseEventArgs e)
    {                                

        Bitmap bmp = new Bitmap(btntarget.Width, btntarget.Height);
        btntarget.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
        //optionally define a transparent color
        bmp.MakeTransparent(Color.White);

        Cursor cur = new Cursor(bmp.GetHicon());                                
        Cursor.Current = cur;            

    }

光标的热点将在图像的中间创建

票数 7
EN

Stack Overflow用户

发布于 2010-07-14 03:58:55

这通常由GiveFeedback事件处理。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3240603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档