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