fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10.  
  11. namespace IconFreeArrangementPanel
  12. {
  13. public partial class FreeListView : ListView
  14. {
  15. [DllImport("comctl32.dll")]
  16. private static extern bool ImageList_BeginDrag(IntPtr imageList,
  17. int index, int x, int y);
  18. [DllImport("comctl32.dll")]
  19. private static extern void ImageList_EndDrag();
  20. [DllImport("comctl32.dll")]
  21. private static extern bool ImageList_DragEnter(IntPtr window, int x, int y);
  22. [DllImport("comctl32.dll")]
  23. private static extern bool ImageList_DragLeave(IntPtr window);
  24. [DllImport("comctl32.dll")]
  25. private static extern bool ImageList_DragMove(int x, int y);
  26. [DllImport("comctl32.dll")]
  27. private static extern bool ImageList_Destroy(IntPtr imgaeList);
  28. [DllImport("comctl32.dll")]
  29. private static extern IntPtr ImageList_Merge(IntPtr imgaeList1, int index1,
  30. IntPtr imageList2, int index2,
  31. int x, int y);
  32. private IntPtr dragImage;
  33. /// <summary> ドラッグする前のクリック位置(X座標) </summary>
  34. private int pointX;
  35. /// <summary> ドラッグする前のクリック位置(Y座標) </summary>
  36. private int pointY;
  37.  
  38. /// <summary>
  39. /// コンストラクタ
  40. /// </summary>
  41. public FreeListView()
  42. {
  43. InitializeComponent();
  44. }
  45.  
  46. public FreeListView(string imageDir)
  47. : this()
  48. {
  49. string[] jpgFiles =
  50. System.IO.Directory.GetFiles(imageDir, "*.jpg");
  51.  
  52. int width = 100;
  53. int height = 80;
  54.  
  55. imageList1.ImageSize = new Size(width, height);
  56. this.LargeImageList = imageList1;
  57.  
  58. Image original = null;
  59. Image thumbnail = null;
  60.  
  61. for (int i = 0; i < jpgFiles.Length; i++)
  62. {
  63. original = Bitmap.FromFile(jpgFiles[i]);
  64. thumbnail = this.createThumbnail(original, width, height);
  65.  
  66. // ImageListのImage設定
  67. imageList1.Images.Add(thumbnail);
  68. // ListViewのラベル設定
  69. this.Items.Add("Item" + i, i);
  70.  
  71. original.Dispose();
  72. //thumbnail.Dispose();
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// 幅w、高さhのImageオブジェクトを作成
  78. /// </summary>
  79. /// <param name="image"></param>
  80. /// <param name="w"></param>
  81. /// <param name="h"></param>
  82. /// <returns></returns>
  83. public Image createThumbnail(Image image, int w, int h)
  84. {
  85. Bitmap canvas = new Bitmap(w, h);
  86.  
  87. Graphics g = Graphics.FromImage(canvas);
  88. g.FillRectangle(new SolidBrush(Color.White), 0, 0, w, h);
  89.  
  90. float fw = (float)w / (float)image.Width;
  91. float fh = (float)h / (float)image.Height;
  92.  
  93. float scale = Math.Min(fw, fh);
  94. fw = image.Width * scale;
  95. fh = image.Height * scale;
  96.  
  97. g.DrawImage(image, (w - fw) / 2, (h - fh) / 2, fw, fh);
  98. g.Dispose();
  99.  
  100. return canvas;
  101. }
  102.  
  103. /// <summary>
  104. /// ドラッグ開始
  105. /// </summary>
  106. /// <param name="e"></param>
  107. protected override void OnItemDrag(ItemDragEventArgs e)
  108. {
  109. base.OnItemDrag(e);
  110. const int LvmCreateDragImage = 0x1000 + 33;
  111. Point pos = Point.Empty;
  112. foreach (ListViewItem item in this.SelectedItems)
  113. {
  114. IntPtr pp = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Point)));
  115. Message m = Message.Create(this.Handle, LvmCreateDragImage,
  116. new IntPtr(item.Index), pp);
  117. this.WndProc(ref m);
  118. Point p = (Point)Marshal.PtrToStructure(pp, typeof(Point));
  119. Marshal.FreeCoTaskMem(pp);
  120. if (this.dragImage.Equals(IntPtr.Zero))
  121. {
  122. this.dragImage = m.Result;
  123. pos = p;
  124. }
  125. else
  126. {
  127. IntPtr old = this.dragImage;
  128. this.dragImage = ImageList_Merge(old, 0, m.Result, 0,
  129. p.X - pos.X, p.Y - pos.Y);
  130. ImageList_Destroy(old);
  131. ImageList_Destroy(m.Result);
  132. }
  133. }
  134. Point view = this.PointToClient(Control.MousePosition);
  135. ImageList_BeginDrag(dragImage, 0, view.X - pos.X, view.Y - pos.Y);
  136. ImageList_DragEnter(this.Handle, view.X, view.Y);
  137. }
  138.  
  139. /// <summary>
  140. /// コントロール上を移動
  141. /// </summary>
  142. /// <param name="e"></param>
  143. protected override void OnMouseMove(MouseEventArgs e)
  144. {
  145. base.OnMouseMove(e);
  146. if (dragImage != IntPtr.Zero)
  147. {
  148. ImageList_DragMove(e.X, e.Y);
  149. }
  150. }
  151.  
  152. /// <summary>
  153. /// マウスボタンが離された(ドラッグ終了)
  154. /// </summary>
  155. /// <param name="e"></param>
  156. protected override void OnMouseUp(MouseEventArgs e)
  157. {
  158. base.OnMouseUp(e);
  159. if (dragImage != IntPtr.Zero)
  160. {
  161. ImageList_DragLeave(this.Handle);
  162. ImageList_EndDrag();
  163. ImageList_Destroy(dragImage);
  164. dragImage = IntPtr.Zero;
  165.  
  166. foreach (ListViewItem srcItem in this.SelectedItems)
  167. {
  168. // ドロップ位置から挿入位置となるインデックス値を決定する
  169. ListViewItem item = this.GetItemAt(e.X, e.Y);
  170. int destIndex = this.Items.IndexOf(item);
  171.  
  172. // 項目の範囲外でドロップしたときは、最後尾に追加する様にインデックス値を設定
  173. if (destIndex < 0)
  174. {
  175. destIndex = this.Items.Count;
  176. }
  177. else if (destIndex > srcItem.Index)
  178. {
  179. destIndex++;
  180. }
  181.  
  182. ListViewItem newItem = this.Items.Insert(destIndex, (ListViewItem)srcItem.Clone());
  183. newItem.Selected = true;
  184. this.Items.Remove(srcItem);
  185. }
  186. }
  187. }
  188. }
  189. }
  190.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(4,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(5,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(8,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 3 error(s), 0 warnings
stdout
Standard output is empty