C# 如何防止图像掉到窗体之外?
我正在制作一个棋盘,我不想让用户在窗体外拖放一个棋子(图像) 当前代码:C# 如何防止图像掉到窗体之外?,c#,drag-and-drop,windows-forms-designer,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(o
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
}
或者有没有一种方法不允许将图像放到表单之外?
我不知道表单之外是否有可以触发的事件。请在此处输入您的darg drop代码。这不是真正的问题,您永远不会得到drop事件。鼠标光标告诉用户,用户知道这一点。唯一的问题是他可能会拖到另一个接受图像的窗口。这当然并不罕见。您可以通过在自己的类中包装图像并拖动该类的对象来修复此问题。只有您的代码才能识别它。