你不能在你自己的窗户外面画画。方法是改变鼠标光标。这就是 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);
}