拖放将图片框拖放到表单上

发布于 2024-12-06 05:51:00 字数 289 浏览 0 评论 0原文

我正在写一个游戏。玩家可以选择物品(例如武器)并将其拖到表单中。项目位于侧面的 PictureBox 控件中。我已将 Form.AllowDrop 设置为 True。当我拖动其中一项 pictureBox 时,pictureBox 不会掉落,甚至不会拖动。

我想在表单上拖动一个图片框,或者至少知道玩家想要将其拖入表单中的位置。

编辑:看看上面的徽标。当您单击它并拖动(不释放)时,它会拖动。

I am writing a game. Player can choose items (such as weapon) and drag them to the form. Items are on the side, in PictureBox controls. I have set Form.AllowDrop to True. When I drag one of the items pictureBoxes, the pictureBox doesn't drop, neither even drag.

I want to drag a pictureBox on the form, or at least know the position in the form which the player want to drag it in.

EDIT: look at the logo above. When you click it and drag (without release) it drags.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-12-13 05:51:00

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

In Winforms you need to change the cursor. Here's a complete example, start a new forms project and drop a picturebox on the form. Set its Image property to a small bitmap. Click and drag to drop copies of the image on the form.

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);
}
一曲爱恨情仇 2024-12-13 05:51:00

对于可拖动的项目,您需要调用 DoDragDrop MouseDown 事件中的 方法。确保您的表单(或目标)的 AllowDrop 属性设置为 true。

对于您的目标,您需要连接拖动事件:

private void Form1_DragOver(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
  // Examine e.Data.GetData stuff
}

For the draggable items, you need to call the DoDragDrop method in the MouseDown event. Make sure your form (or target) has the AllowDrop property set to true.

For your target, you need to wire the dragging events:

private void Form1_DragOver(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
  // Examine e.Data.GetData stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文