'drag'에 해당되는 글 2건

  1. 2022.03.31 C# Drag & Drop - 드래그 & 드롭 2
  2. 2022.03.31 C# Drag & Drop - 드래그 & 드롭 1
반응형

이미지 파일을 Drag & Drop으로 열어보자.

 

WinForm에 PictureBox를 적당히 배치한다.

 

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.AllowDrop = true;
            // 인텔리센스에는 pictureBox1.AllowDrop가 표시되지 않는다.
            // 하지만 사용은 가능하며 인텔리센스를 사용하고 싶다면 아래처럼
            // Control로 형변환해서 해도 된다.
            // ((Control)pictureBox1).AllowDrop = true;
        }
 
        private void pictureBox1_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }
 
        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                // 드롭된 파일들의 전체 경로명을 반환.
                foreach (string file in files)
                {
                    if (file.EndsWith("jpg"|| file.EndsWith("JPG"))
                    {
                        // JPG 파일만 디스플레이
                        pictureBox1.Image = System.Drawing.Image.FromFile(file);
                    }
                }
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

프로그램을 실행하고 PictureBox 위로 JPG파일을 드래그 & 드롭한다.

 

이미지가 표시된다.

 

반응형
Posted by J-sean
:
반응형

간단한 Drag & Drop을 구현해 보자.

 

WinForm에 ListBox 두 개를 적당히 배치한다.

 

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            listBox2.AllowDrop = true;
 
            listBox1.Items.Add("모니터");
            listBox1.Items.Add("키보드");
            listBox1.Items.Add("스피커");
            listBox1.Items.Add("마우스");
            listBox1.Items.Add("카메라");
        }
 
        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            DragDropEffects effect;
 
            int index = listBox1.IndexFromPoint(e.X, e.Y);
            // listBox1.SelectedIndex을 사용하지 않으므로
            // 오른쪽 마우스 버튼의 드래그&드롭도 처리 가능
 
            if (index != ListBox.NoMatches)
            {
                string item = (string)listBox1.Items[index];
                effect = DoDragDrop(item, DragDropEffects.Copy | DragDropEffects.Move);
                if (effect == DragDropEffects.Move)
                {
                    listBox1.Items.RemoveAt(index);
                }
            }
        }
 
        private void listBox1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            if (e.EscapePressed)
            {
                e.Action = DragAction.Cancel;
                // 드래그&드롭 중 ESC키가 눌리면 취소된다.
            }
        }
 
        private void listBox2_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                // StringFormat 형식을 허용하기 때문에 다른 프로그램(노트패드 등)
                // 의 문자열을 선택하고 드래그&드롭도 가능하다.
 
                if ((e.KeyState & 8!= 0)
                {
                    e.Effect = DragDropEffects.Copy;
                    // 1 (bit 0)   The left mouse button.
                    // 2 (bit 1)   The right mouse button.
                    // 4 (bit 2)   The SHIFT key.
                    // 8 (bit 3)   The CTRL key.
                    // 16 (bit 4)  The middle mouse button.
                    // 32 (bit 5)  The ALT key.
                }
                else
                {
                    e.Effect = DragDropEffects.Move;
                }
            }
        }
 
        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                listBox2.Items.Add(e.Data.GetData(DataFormats.StringFormat));
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

프로그램을 실행하면 위와 같이 표시된다.

 

드래그 & 드롭으로 복사 및 이동이 가능하다.

 

 

다른 프로그램의 문자열도 복사 및 이동이 가능하다.

 

반응형
Posted by J-sean
: