如何使用图像列表在自己的窗口之外绘制C#拖动 [英] How can I use an Image List to draw C# dragging outside of my own windows

查看:95
本文介绍了如何使用图像列表在自己的窗口之外绘制C#拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Control.DoDragDrop()使用C#的内置拖放功能.我使用图像列表,ImageList_DragMove和朋友来移动半透明图像,并使用鼠标进行跟踪. (请参见此线程获取更多信息).在窗口外面时,如何使ImageList跟踪鼠标?我仅在OnDragOver()中收到鼠标位置消息,并且仅当鼠标悬停在我的Windows之一上时才收到.拖到我的应用程序的另一个实例,我希望ImageList贯穿整个过程,包括在桌面上.我想基本的问题是DoDragDrop运行自己的小消息循环.

I am using C#'s inbuilt drag and drop via Control.DoDragDrop(). I use an Image List and ImageList_DragMove and friends to move a semi-transparent image around, tracking with the mouse. (See my reply in this thread for more information). How can I make the ImageList track the mouse when it is outside my windows? I only receive mouse position messages in OnDragOver(), and only when the mouse is over one of my Windows. The drag is going to another instance of my application, and I would like the ImageList to go the whole way, including over the desktop. I guess the basic problem is that DoDragDrop runs its own little message loop.

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

Windows Explorer gets this done so I know it is possible. I suppose I could start a thread to keep track of the mouse or write my own drag/drop message loop, but I am hoping for an easier way.

推荐答案

您不能在自己的窗户外面画画.完成此操作的方法是更改​​鼠标光标.这就是GiveFeedback事件可用的功能,将e.UseDefaultCursors设置为false并设置Cursor.Current.

You cannot draw outside your own windows. The way to do this is by changing the mouse cursor. That's what the GiveFeedback event is available, set e.UseDefaultCursors to false and set the Cursor.Current.

只是为了让您了解一下它的外观,这是一个示例表格,可拖动可见的文本.对其进行更改以按照所需方式绘制位图,例如从ImageList中绘制位图.请注意,Bitmap.GetHicon()不会创建漂亮的图标,颜色映射很差.

Just to give you an idea what this looks like, here's a sample form that drags visible text. Alter it to draw the bitmap the way you want it, from your ImageList for example. Beware that Bitmap.GetHicon() does not create great icons, the color mapping is poor.

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

}

这篇关于如何使用图像列表在自己的窗口之外绘制C#拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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