我創建了一個小型Windows窗體測試應用程序來嘗試一些拖放代碼。該表單由三個PictureBox組成。我的意圖是從一個PictureBox中抓取圖片,在拖動操作中將其顯示爲自定義光標,然後將其放到另一個PictureBox目標上。在同一Windows窗體應用程序的實例之間拖放
從一個PictureBox到另一個的工作正常,只要它們的形式相同。
如果我打開同一個應用程序的兩個實例,並試圖拖動它們之間/降,我得到以下神祕的錯誤:
This remoting proxy has no channel sink which means either the server has no registered server channels that are listening, or this application has no suitable client channel to talk to the server.
出於某種原因,但是,它的工作拖動/降寫字板(但不是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);
}
}
然後有四個這樣的活動:
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, transparencyType);
bmp.Dispose();
Cursor.Current = this.cursorCreatedFromControlBitmap;
(sender as PictureBox).DoDragDrop((sender as PictureBox).Image, DragDropEffects.All);
}
void pictureBox_GiveFeedback(object sender, GiveFeedbackEventArgs gfea) {
gfea.UseDefaultCursors = false;
}
void pictureBox_DragEnter(object sender, DragEventArgs dea) {
if ((dea.KeyState & 32) == 32) { // ALT is pressed
dea.Effect = DragDropEffects.Link;
}
else if ((dea.KeyState & 8) == 8) { // CTRL is pressed
dea.Effect = DragDropEffects.Copy;
}
else if ((dea.KeyState & 4) == 4) { // SHIFT is pressed
dea.Effect = DragDropEffects.None;
}
else {
dea.Effect = DragDropEffects.Move;
}
}
void pictureBox_DragDrop(object sender, DragEventArgs dea) {
if (((IDataObject)dea.Data).GetDataPresent(DataFormats.Bitmap))
(sender as PictureBox).Image = (Image)((IDataObject)dea.Data).GetData(DataFormats.Bitmap);
}
任何幫助將不勝感激!
很棒的回答。很酷。 +1 – 2009-07-30 16:31:29
嗨邁克爾! 我喜歡你的方法。感謝你的回答!這已經困擾了我很長一段時間,你的解決方案是一個很好的解決方案,以解決一個反覆出現的問題。但是,我發現了另一種解決方案,在傳輸常用剪貼板格式的情況下可能會更好(至少更短)。該解決方案如下所述。無論如何,我想給你「接受的答案」的信譽,因爲你的解決方案可能更適用於一般情況。請查看下面的解決方案,以解決此問題的另一種方法。 - Peder - – Pedery 2009-07-31 16:30:32
您的發現非常有趣,可能是您的案例的最佳解決方案。我完全打算深入研究這篇文章並嘗試這種技術。我們兩方都需要這樣的努力來解決這個問題,這有點令人傷心。任何時候我都需要與.NET的shell進行交互,因爲婚姻通常是非常棘手的。感謝您的信任,甚至更多關於此主題的更多信息。 – 2009-07-31 17:33:15