특정 컨트롤에서 Drag&Drop 으로 파일을 받으려면 우선 해당 컨트롤의 AllowDrop 옵션을 True로 설정.

이후 컨트롤의 DragOver 와 DragDrop 이벤트 핸들러를 작성.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Drag & Drop
private void OnFileDragDrop(object sender, DragEventArgs drgevent)
{
Console.WriteLine("Error in AppendData procedure.");
//textFilePath.Text = "Drag Drop";
//base.OnDragDrop(drgevent);
try
{
if (drgevent.Data.GetDataPresent(DataFormats.FileDrop, false))
{
string[] fileNames = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
//foreach (string fileName in fileNames)
{
//파일 처리
pLogSession.sFilename = fileNames[0];
}
}
}
catch (System.Exception ex)
{
//예외처리
Console.WriteLine(ex.Message);
}
}
private void OnFileDragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Target Form Drag Enter");
e.Data.GetDataPresent(typeof(System.String));
//e.Effect = DragDropEffects.All;
e.Effect = DragDropEffects.Copy;
}

 

 

 

폼에서 탐색기로 파일을 끌고가고 싶을 때는,

1
2
3
4
5
6
7
8
9
10
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
string[] files = new string[] { @"c:\temp\test.txt" };
this.DoDragDrop(new DataObject(DataFormats.FileDrop, files), DragDropEffects.Copy);
}
}

'Tip' 카테고리의 다른 글

SmtpClient로 메일 보내기 - gmail  (0) 2013.04.11
C# 자주 쓰는 코드  (0) 2013.04.11
에디트 박스 제일 밑으로 스크롤 하기  (0) 2013.04.11
람다식  (0) 2013.02.19
64비트 서버에 32비트 프레임웍 설치  (0) 2012.04.26
Posted by 퓨전마법사
,