我创建了一个小型Windows窗体测试应用程序来尝试一些拖放代码.该表单由三个PictureBoxes组成.我的目的是从一个PictureBox中抓取一张图片,在拖动操作期间将其显示为自定义光标,然后将其放在另一个PictureBox目标上.
只要它们在同一个表单上,这从一个PictureBox到另一个PictureBox就可以正常工作.
如果我打开同一个应用程序的两个实例并尝试在它们之间拖放,我会收到以下神秘错误:
此远程处理代理没有通道接收器,这意味着服务器没有正在侦听的已注册服务器通道,或者此应用程序没有合适的客户端通道与服务器通信.
但是,出于某种原因,它可以拖放到Wordpad(但不是MS Word或画笔).
三个PictureBoxes将它们的事件连接起来如下:
foreach (Control pbx in this.Controls) {
if (pbx is PictureBox) {
pbx.AllowDrop = true;
pbx.MouseDown += new MouseEventHandler(pictureBox_MouseDown);
pbx.GiveFeedback += new GiveFeedbackEventHandler(pictureBox_GiveFeedback);
pbx.DragEnter += new DragEventHandler(pictureBox_DragEnter);
pbx.DragDrop += new DragEventHandler(pictureBox_DragDrop);
}
}
Run Code Online (Sandbox Code Playgroud)
然后是这样的四个事件:
void pictureBox_MouseDown(object sender, MouseEventArgs e) {
int width = (sender as PictureBox).Image.Width;
int height = (sender as PictureBox).Image.Height;
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage((sender as PictureBox).Image, 0, 0, width, height);
g.Dispose();
cursorCreatedFromControlBitmap = CustomCursors.CreateFormCursor(bmp, …Run Code Online (Sandbox Code Playgroud)