我正在写一个游戏.玩家可以选择物品(如武器)并将其拖动到表格中.物品在侧面,在PictureBox
控制中.我已经开始Form.AllowDrop
了True
.当我拖动其中一个图片盒子时,pictureBox
它不会掉落,甚至都不会拖动.
我想在窗体上拖动一个pictureBox,或者至少知道播放器想要拖动它的形式中的位置.
编辑:看看上面的标志.当您单击它并拖动(没有释放)时它会拖动.
在Winforms中,您需要更改光标.这是一个完整的示例,启动一个新的表单项目并在表单上放置一个图片框.将其Image属性设置为一个小位图.单击并拖动以删除表单上的图像副本.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.AllowDrop = true;
this.pictureBox1.MouseDown += pictureBox1_MouseDown;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
var dragImage = (Bitmap)pictureBox1.Image;
IntPtr icon = dragImage.GetHicon();
Cursor.Current = new Cursor(icon);
DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
DestroyIcon(icon);
}
}
protected override void OnGiveFeedback(GiveFeedbackEventArgs e) {
e.UseDefaultCursors = false;
}
protected override void OnDragEnter(DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(Bitmap))) e.Effect = DragDropEffects.Copy;
}
protected override void OnDragDrop(DragEventArgs e) {
var bmp = (Bitmap)e.Data.GetData(typeof(Bitmap));
var pb = new PictureBox();
pb.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
pb.Size = pb.Image.Size;
pb.Location = this.PointToClient(new Point(e.X - pb.Width/2, e.Y - pb.Height/2));
this.Controls.Add(pb);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
}
Run Code Online (Sandbox Code Playgroud)