-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragDropProvider.cs
147 lines (131 loc) · 4.47 KB
/
DragDropProvider.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using DevExpress.XtraEditors;
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DXSample
{
public class DragDropProvider
{
Image draggedImage;
PictureEdit edit;
public DragDropProvider(PictureEdit edit)
{
this.edit = edit;
edit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;
}
[DllImport("GdiPlus.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int GdipCreateBitmapFromGdiDib(IntPtr pBIH, IntPtr pPix, out IntPtr pBitmap);
private IntPtr GetPixelInfo(IntPtr bmpPtr)
{
BITMAPINFOHEADER bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(bmpPtr, typeof(BITMAPINFOHEADER));
if(bmi.biSizeImage == 0)
bmi.biSizeImage = (uint)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
int p = (int)bmi.biClrUsed;
if((p == 0) && (bmi.biBitCount <= 8))
p = 1 << bmi.biBitCount; p = (p * 4) + (int)bmi.biSize + (int)bmpPtr;
return (IntPtr)p;
}
private void OnDragDrop(object sender, DragEventArgs e)
{
if((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
edit.Image = draggedImage;
draggedImage = null;
}
}
private void OnDragEnter(object sender, DragEventArgs e)
{
SetDragDropEffects(e);
}
private void SetDragDropEffects(DragEventArgs e)
{
e.Effect = DragDropEffects.None;
MemoryStream str = e.Data.GetData(DataFormats.Dib) as MemoryStream;
if(str != null)
{
try
{
GCHandle handle = GCHandle.Alloc(str.ToArray(), GCHandleType.Pinned);
draggedImage = BitmapFromDIB(handle.AddrOfPinnedObject());
if(draggedImage != null)
e.Effect = DragDropEffects.Copy;
}
catch
{
draggedImage = null;
}
}
else
{
string[] fileName = e.Data.GetData("FileName") as string[];
if(fileName != null)
{
try
{
draggedImage = Image.FromFile(fileName[0]);
e.Effect = DragDropEffects.Copy;
}
catch
{
draggedImage = null;
}
}
else
{
object bmp = e.Data.GetData("Bitmap");
if(bmp != null)
{
draggedImage = (Image)bmp;
e.Effect = DragDropEffects.Copy;
}
}
}
}
public Bitmap BitmapFromDIB(IntPtr pDIB)
{
IntPtr pPix = GetPixelInfo(pDIB);
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus",
BindingFlags.Static | BindingFlags.NonPublic);
if(mi == null) return null;
IntPtr pBmp = IntPtr.Zero;
int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);
if((status == 0) && (pBmp != IntPtr.Zero))
return (Bitmap)mi.Invoke(null, new object[] { pBmp });
else
return null;
}
public void DisableDragDrop()
{
edit.DragEnter -= OnDragEnter;
edit.DragDrop -= OnDragDrop;
}
public void EnableDragDrop()
{
edit.AllowDrop = true;
edit.DragEnter += OnDragEnter;
edit.DragDrop += OnDragDrop;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
public void Init()
{
biSize = (uint)Marshal.SizeOf(this);
}
}
}