我在Visual Studio 2012中用C#工作,我需要將一個圖片框拖到另一個圖片框中,基本上用拖動圖片框圖像替換目標圖片框圖像。C#從一個圖片框拖放到另一個圖片框
我該怎麼做?
請具體說明並嘗試儘可能簡單和儘可能地解釋。 我對編程非常陌生,有點絕望,請耐心等待。
我在Visual Studio 2012中用C#工作,我需要將一個圖片框拖到另一個圖片框中,基本上用拖動圖片框圖像替換目標圖片框圖像。C#從一個圖片框拖放到另一個圖片框
我該怎麼做?
請具體說明並嘗試儘可能簡單和儘可能地解釋。 我對編程非常陌生,有點絕望,請耐心等待。
你可以使用鼠標進入和離開事件來做到這一點很容易..例如,你有兩個圖片框pictureBox1和pictureBox2 ...而你想從圖片box1拖動圖像並將其放到圖片盒2上做一些事情這...
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (a == 1)
{
pictureBox1.Image = pictureBox2.Image;
a = 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
a = 1;
}
其中「a」只是一個鎖或鑰匙,檢查鼠標是否已進入上,我們希望在放棄這一形象...希望它helped..worked我控制
Drag + Drop被隱藏在PictureBox控件上。不知道爲什麼,它工作得很好。這裏可能的指導是,用戶不會明白你可以在控件上放置圖像。你必須做些什麼,至少將BackColor屬性設置爲非默認值,以便用戶可以看到它。
安美居,你需要實現第一個PictureBox的MouseDown事件,所以你可以點擊它,並開始拖動:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
var img = pictureBox1.Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) {
pictureBox1.Image = null;
}
}
我假定你想移動的圖像,如果必要的,如果複製是調整意。然後你必須在第二個picturebox上實現DragEnter和DragDrop事件。由於屬性是隱藏的,你應該在窗體的構造函數中設置它們。像這樣:
public Form1() {
InitializeComponent();
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox2.AllowDrop = true;
pictureBox2.DragEnter += pictureBox2_DragEnter;
pictureBox2.DragDrop += pictureBox2_DragDrop;
}
void pictureBox2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
void pictureBox2_DragDrop(object sender, DragEventArgs e) {
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
這確實允許您將圖像從另一個應用程序拖入框中。我們稱之爲功能。如果你想禁止這個,請使用布爾標誌。
if(img == null)return; 這條線的回報是什麼? 對不起,這只是幾乎我第一次編程 – user2250165
當用戶點擊一個沒有圖像的圖片框時,它停止代碼崩潰。 –
好的,好的,非常感謝你,我還有幾個問題。的InitializeComponent(); - 這是做什麼 以及爲什麼你使用+ =符號而不是=符號? – user2250165
可能的重複:http://stackoverflow.com/questions/1935925/drag-drop-of-a-dynamically-created-shortcut?rq=1 –