如何防止将图像拖放到表单之外?

时间:2019-11-09 10:44:42

标签: c# drag-and-drop windows-forms-designer

我正在制作国际象棋棋盘,但我不想允许用户将国际象棋棋子(图像)拖放到表单之外。

当前代码:

private void PictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        pb.DoDragDrop(pb.Image, DragDropEffects.Copy);
    }
private void PictureBox_DragEnter(object sender, DragEventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        pb.Image = DragDropPreview(e);
    }
private void PictureBox_DragDrop(object sender, DragEventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        pb.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
    }
private void PictureBox_DragLeave(object sender, EventArgs e)
    {
        PictureBox pb = (PictureBox)sender;
        pb.Image = null;
    }

是否可以防止用户将图像拖出表格?

 private void frmChessBoard_DragLeave(object sender, EventArgs e)
    {
        // stop the Drag & Drop action
    }

还是有一种不允许将图像拖放到表格之外的方法? 我不知道是否可以触发表单之外的事件。

0 个答案:

没有答案