0

我通过 Control.DoDragDrop() 使用 C# 的内置拖放功能。我使用 Image List 和 ImageList_DragMove 和朋友来移动一个半透明的图像,用鼠标跟踪。(有关更多信息,请参阅我在此线程中的回复)。如何使 ImageList在我的窗口外时跟踪鼠标?我只在 OnDragOver() 中接收鼠标位置消息,并且只有当鼠标位于我的一个 Windows 上时。拖动将转到我的应用程序的另一个实例,我希望 ImageList 可以一直进行,包括在桌面上。我猜基本问题是 DoDragDrop 运行它自己的小消息循环。

Windows 资源管理器完成了这项工作,所以我知道这是可能的。我想我可以启动一个线程来跟踪鼠标或编写我自己的拖放消息循环,但我希望有一种更简单的方法。

4

2 回答 2

2

你不能在你自己的窗户外面画画。方法是改变鼠标光标。这就是 GiveFeedback 事件可用的原因,将 e.UseDefaultCursors 设置为 false 并设置 Cursor.Current。

只是为了让您了解它的外观,这是一个拖动可见文本的示例表单。更改它以按照您想要的方式绘制位图,例如从您的 ImageList 中。请注意 Bitmap.GetHicon() 不会创建出色的图标,颜色映射很差。

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.GiveFeedback += Form1_GiveFeedback;
    }

    void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e) {
        string txt = "Dragging text";
        SizeF sz;
        using (var gr = this.CreateGraphics()) {
            sz = gr.MeasureString(txt, this.Font);
        }
        using (var bmp = new Bitmap((int)sz.Width, (int)sz.Height)) {
            using (var gr = Graphics.FromImage(bmp)) {
                gr.Clear(Color.White);
                gr.DrawString(txt, this.Font, Brushes.Black, 0, 0);
            }
            bmp.MakeTransparent(Color.White);
            e.UseDefaultCursors = false;
            IntPtr hIcon = bmp.GetHicon();
            Cursor.Current = new Cursor(hIcon);
            DestroyIcon(hIcon);
        }
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        this.DoDragDrop("example", DragDropEffects.Copy);
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    extern static bool DestroyIcon(IntPtr handle);

}
于 2010-09-08T11:30:24.533 回答
0

我建议你创建一个特殊的表单并拖动它而不是使用 ImageList。我们已经在我们的产品(例如 XtraGrid)中做到了这一点,以允许最终用户排列列。

于 2010-09-08T07:07:45.993 回答