1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.IO;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Runtime.InteropServices;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using System.Diagnostics;
|
---|
9 |
|
---|
10 | namespace Netron.Diagramming.Core
|
---|
11 | {
|
---|
12 | // ----------------------------------------------------------------------
|
---|
13 | /// <summary>
|
---|
14 | /// A shape used for a shortcut to a file.
|
---|
15 | /// </summary>
|
---|
16 | // ----------------------------------------------------------------------
|
---|
17 | [Serializable()]
|
---|
18 | public class FileShape : ComplexShapeBase
|
---|
19 | {
|
---|
20 | // ------------------------------------------------------------------
|
---|
21 | /// <summary>
|
---|
22 | /// Implementation of IVersion - the FileShape's current version.
|
---|
23 | /// </summary>
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | protected double fileShapeVersion = 1.0;
|
---|
26 |
|
---|
27 | // ------------------------------------------------------------------
|
---|
28 | /// <summary>
|
---|
29 | /// The complete path to the file we're to open when this shape is
|
---|
30 | /// double-clicked.
|
---|
31 | /// </summary>
|
---|
32 | // ------------------------------------------------------------------
|
---|
33 | protected string myFileName = String.Empty;
|
---|
34 |
|
---|
35 | // ------------------------------------------------------------------
|
---|
36 | /// <summary>
|
---|
37 | /// The IconLabelMaterial used to show the file's icon.
|
---|
38 | /// </summary>
|
---|
39 | // ------------------------------------------------------------------
|
---|
40 | IconLabelMaterial myIcon = null;
|
---|
41 |
|
---|
42 | // ------------------------------------------------------------------
|
---|
43 | /// <summary>
|
---|
44 | /// The LabelMaterial used to show the file's name.
|
---|
45 | /// </summary>
|
---|
46 | // ------------------------------------------------------------------
|
---|
47 | LabelMaterial myLabel = null;
|
---|
48 |
|
---|
49 | // ------------------------------------------------------------------
|
---|
50 | /// <summary>
|
---|
51 | /// Gets the 'friendly' name of this shape.
|
---|
52 | /// </summary>
|
---|
53 | // ------------------------------------------------------------------
|
---|
54 | public override string EntityName
|
---|
55 | {
|
---|
56 | get
|
---|
57 | {
|
---|
58 | return "File";
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | // ------------------------------------------------------------------
|
---|
63 | /// <summary>
|
---|
64 | /// Gets the version of this file shape.
|
---|
65 | /// </summary>
|
---|
66 | // ------------------------------------------------------------------
|
---|
67 | public override double Version
|
---|
68 | {
|
---|
69 | get
|
---|
70 | {
|
---|
71 | return fileShapeVersion;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | // ------------------------------------------------------------------
|
---|
76 | /// <summary>
|
---|
77 | /// Gets or sets the complete path to the file we're to open when this
|
---|
78 | /// shape is double-clicked.
|
---|
79 | /// </summary>
|
---|
80 | // ------------------------------------------------------------------
|
---|
81 | public string FileName
|
---|
82 | {
|
---|
83 | get
|
---|
84 | {
|
---|
85 | return myFileName;
|
---|
86 | }
|
---|
87 | set
|
---|
88 | {
|
---|
89 | try
|
---|
90 | {
|
---|
91 | FileInfo info = new FileInfo(value);
|
---|
92 | if (info.Exists)
|
---|
93 | {
|
---|
94 | myFileName = value;
|
---|
95 | CreateIcon(myFileName);
|
---|
96 | CreateLabel(info.Name);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | catch
|
---|
100 | {
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | // ------------------------------------------------------------------
|
---|
106 | /// <summary>
|
---|
107 | /// Gets or sets if this shape is resizable - the FileShape is not
|
---|
108 | /// resizable.
|
---|
109 | /// </summary>
|
---|
110 | // ------------------------------------------------------------------
|
---|
111 | public override bool Resizable
|
---|
112 | {
|
---|
113 | get
|
---|
114 | {
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | set
|
---|
118 | {
|
---|
119 | base.Resizable = value;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | // ------------------------------------------------------------------
|
---|
124 | /// <summary>
|
---|
125 | /// Constructor.
|
---|
126 | /// </summary>
|
---|
127 | // ------------------------------------------------------------------
|
---|
128 | public FileShape()
|
---|
129 | : base()
|
---|
130 | {
|
---|
131 | myIcon = new IconLabelMaterial();
|
---|
132 | myIcon.Icon = (Bitmap) ImagePalette.Shortcut;
|
---|
133 | myIcon.Shape = this;
|
---|
134 | Children.Add(myIcon);
|
---|
135 | }
|
---|
136 |
|
---|
137 | // ------------------------------------------------------------------
|
---|
138 | /// <summary>
|
---|
139 | /// Constructor that receives the path to the file.
|
---|
140 | /// </summary>
|
---|
141 | /// <param name="filename">string: The complete path to the file we're
|
---|
142 | /// to open when this shape is double-clicked.</param>
|
---|
143 | // ------------------------------------------------------------------
|
---|
144 | public FileShape(string filename) : base()
|
---|
145 | {
|
---|
146 | FileName = filename;
|
---|
147 | }
|
---|
148 |
|
---|
149 | #region Overrides
|
---|
150 |
|
---|
151 | // ------------------------------------------------------------------
|
---|
152 | /// <summary>
|
---|
153 | /// Opens the file when this shape is double-clicked.
|
---|
154 | /// </summary>
|
---|
155 | /// <param name="e">MouseEventArgs</param>
|
---|
156 | /// <returns>bool: True if handled.</returns>
|
---|
157 | // ------------------------------------------------------------------
|
---|
158 | public override bool MouseDown(MouseEventArgs e)
|
---|
159 | {
|
---|
160 | if ((e.Button == MouseButtons.Left) &&
|
---|
161 | (e.Clicks == 2) &&
|
---|
162 | (this.myFileName != String.Empty) )
|
---|
163 | {
|
---|
164 | try
|
---|
165 | {
|
---|
166 | Process.Start(this.myFileName);
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 | catch (Exception ex)
|
---|
170 | {
|
---|
171 | MessageBox.Show("Unable to open the file.\n" + ex.Message);
|
---|
172 | return false;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return base.MouseDown(e);
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endregion
|
---|
179 |
|
---|
180 | // ------------------------------------------------------------------
|
---|
181 | /// <summary>
|
---|
182 | /// Loads the icon from Windows for the filename specified and
|
---|
183 | /// creates our 'icon'.
|
---|
184 | /// </summary>
|
---|
185 | /// <param name="filename">string</param>
|
---|
186 | // ------------------------------------------------------------------
|
---|
187 | void CreateIcon(string filename)
|
---|
188 | {
|
---|
189 | if (this.myIcon == null)
|
---|
190 | {
|
---|
191 | this.myIcon = new IconLabelMaterial();
|
---|
192 | Children.Add(myIcon);
|
---|
193 | this.myIcon.Shape = this;
|
---|
194 | }
|
---|
195 |
|
---|
196 | try
|
---|
197 | {
|
---|
198 | this.myIcon.Icon = GetIconForFile(filename, false).ToBitmap();
|
---|
199 | }
|
---|
200 | catch
|
---|
201 | {
|
---|
202 | this.myIcon.Icon = (Bitmap) ImagePalette.Shortcut;
|
---|
203 | }
|
---|
204 | this.myIcon.Transform(new Rectangle(Location, new Size(1, 1)));
|
---|
205 | Transform(myIcon.Rectangle);
|
---|
206 | }
|
---|
207 |
|
---|
208 | // ------------------------------------------------------------------
|
---|
209 | /// <summary>
|
---|
210 | /// Creates the label that shows the file's name.
|
---|
211 | /// </summary>
|
---|
212 | /// <param name="filename">string: The name to show in the
|
---|
213 | /// label.</param>
|
---|
214 | // ------------------------------------------------------------------
|
---|
215 | void CreateLabel(string filename)
|
---|
216 | {
|
---|
217 | if (this.myLabel == null)
|
---|
218 | {
|
---|
219 | this.myLabel = new LabelMaterial();
|
---|
220 | Children.Add(this.myLabel);
|
---|
221 | }
|
---|
222 |
|
---|
223 | this.myLabel.Text = filename;
|
---|
224 | }
|
---|
225 |
|
---|
226 | // ------------------------------------------------------------------
|
---|
227 | /// <summary>
|
---|
228 | /// Draws this shape to the GDI+ graphics surface specified.
|
---|
229 | /// </summary>
|
---|
230 | /// <param name="g">Graphics</param>
|
---|
231 | // ------------------------------------------------------------------
|
---|
232 | public override void Paint(Graphics g)
|
---|
233 | {
|
---|
234 | this.myIcon.Paint(g);
|
---|
235 |
|
---|
236 | int iconWidth = myIcon.Icon.Width;
|
---|
237 | int iconHeight = myIcon.Icon.Height;
|
---|
238 |
|
---|
239 | // Make the max available label width a little bigger than the
|
---|
240 | // icon.
|
---|
241 | SizeF labelSize = g.MeasureString(myLabel.Text,
|
---|
242 | myLabel.Font,
|
---|
243 | myIcon.Icon.Width + 100);
|
---|
244 |
|
---|
245 | // Position the label just under the icon and center it.
|
---|
246 | int xOffset = ((int)labelSize.Width - iconWidth) / 2;
|
---|
247 |
|
---|
248 | Point labelLocation = new Point(
|
---|
249 | myIcon.Rectangle.Location.X - xOffset,
|
---|
250 | myIcon.Rectangle.Location.Y + iconHeight + 5);
|
---|
251 |
|
---|
252 | this.myLabel.Transform(new Rectangle(
|
---|
253 | labelLocation,
|
---|
254 | Size.Round(labelSize)));
|
---|
255 |
|
---|
256 | this.myLabel.Paint(g);
|
---|
257 |
|
---|
258 | mRectangle.Width = myIcon.Icon.Width + 20;
|
---|
259 | mRectangle.Location = new Point(
|
---|
260 | myIcon.Rectangle.X - 10,
|
---|
261 | mRectangle.Y);
|
---|
262 |
|
---|
263 | if (labelSize.Width > iconWidth)
|
---|
264 | {
|
---|
265 | mRectangle.Width = (int)labelSize.Width + 20;
|
---|
266 | mRectangle.Location = new Point(
|
---|
267 | myLabel.Rectangle.X - 10,
|
---|
268 | mRectangle.Y);
|
---|
269 | }
|
---|
270 |
|
---|
271 | mRectangle.Height =
|
---|
272 | myIcon.Icon.Height +
|
---|
273 | myLabel.Rectangle.Height + 20;
|
---|
274 | }
|
---|
275 |
|
---|
276 | #region Get Icon Associated With A File
|
---|
277 |
|
---|
278 | [StructLayout(LayoutKind.Sequential)]
|
---|
279 | public struct SHFILEINFO
|
---|
280 | {
|
---|
281 | public IntPtr hIcon;
|
---|
282 | public IntPtr iIcon;
|
---|
283 | public int dwAttributes;
|
---|
284 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
---|
285 | public string szDisplayName;
|
---|
286 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
|
---|
287 | public string szTypeName;
|
---|
288 | };
|
---|
289 |
|
---|
290 | internal class Win32
|
---|
291 | {
|
---|
292 | public const uint SHGFI_ICON = 0x100;
|
---|
293 | public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
|
---|
294 | public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
|
---|
295 |
|
---|
296 | [DllImport("shell32.dll")]
|
---|
297 | public static extern IntPtr SHGetFileInfo(string pszPath,
|
---|
298 | uint dwFileAttributes,
|
---|
299 | ref SHFILEINFO psfi,
|
---|
300 | uint cbSizeFileInfo,
|
---|
301 | uint uFlags);
|
---|
302 | }
|
---|
303 |
|
---|
304 | // ------------------------------------------------------------------
|
---|
305 | /// <summary>
|
---|
306 | /// Returns the Icon associated with the file specified.
|
---|
307 | /// </summary>
|
---|
308 | /// <param name="fileName"></param>
|
---|
309 | /// <param name="smallImage">bool: Get the small image or the
|
---|
310 | /// big one?</param>
|
---|
311 | /// <returns></returns>
|
---|
312 | // ------------------------------------------------------------------
|
---|
313 | public static Icon GetIconForFile(string fileName, bool smallImage)
|
---|
314 | {
|
---|
315 | IntPtr hImgSmall; //the handle to the system small image list
|
---|
316 | IntPtr hImgLarge; //the handle to the system large image list
|
---|
317 | SHFILEINFO shinfo = new SHFILEINFO();
|
---|
318 |
|
---|
319 | // Get the small image if we're supposed to.
|
---|
320 | if (smallImage)
|
---|
321 | {
|
---|
322 | hImgSmall = Win32.SHGetFileInfo(
|
---|
323 | fileName,
|
---|
324 | 0,
|
---|
325 | ref shinfo,
|
---|
326 | (uint)Marshal.SizeOf(shinfo),
|
---|
327 | Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | // Otherwise get the large image.
|
---|
332 | hImgLarge = Win32.SHGetFileInfo(
|
---|
333 | fileName,
|
---|
334 | 0,
|
---|
335 | ref shinfo,
|
---|
336 | (uint)Marshal.SizeOf(shinfo),
|
---|
337 | Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
|
---|
338 | }
|
---|
339 |
|
---|
340 | // Return the image.
|
---|
341 | return System.Drawing.Icon.FromHandle(shinfo.hIcon);
|
---|
342 | }
|
---|
343 |
|
---|
344 | #endregion
|
---|
345 | }
|
---|
346 | }
|
---|