[2768] | 1 | using System;
|
---|
[4068] | 2 | using System.Diagnostics;
|
---|
| 3 | using System.Drawing;
|
---|
[2768] | 4 | using System.IO;
|
---|
| 5 | using System.Runtime.InteropServices;
|
---|
| 6 | using System.Windows.Forms;
|
---|
| 7 |
|
---|
[4068] | 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 | // ------------------------------------------------------------------
|
---|
[2768] | 17 | /// <summary>
|
---|
[4068] | 18 | /// Implementation of IVersion - the FileShape's current version.
|
---|
[2768] | 19 | /// </summary>
|
---|
[4068] | 20 | // ------------------------------------------------------------------
|
---|
| 21 | protected double fileShapeVersion = 1.0;
|
---|
[2768] | 22 |
|
---|
[4068] | 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;
|
---|
[2768] | 30 |
|
---|
[4068] | 31 | // ------------------------------------------------------------------
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// The IconLabelMaterial used to show the file's icon.
|
---|
| 34 | /// </summary>
|
---|
| 35 | // ------------------------------------------------------------------
|
---|
| 36 | IconLabelMaterial myIcon = null;
|
---|
[2768] | 37 |
|
---|
[4068] | 38 | // ------------------------------------------------------------------
|
---|
| 39 | /// <summary>
|
---|
| 40 | /// The LabelMaterial used to show the file's name.
|
---|
| 41 | /// </summary>
|
---|
| 42 | // ------------------------------------------------------------------
|
---|
| 43 | LabelMaterial myLabel = null;
|
---|
[2768] | 44 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 55 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 66 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 85 | }
|
---|
[4068] | 86 | catch {
|
---|
[2768] | 87 | }
|
---|
[4068] | 88 | }
|
---|
| 89 | }
|
---|
[2768] | 90 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 105 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 118 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 130 |
|
---|
[4068] | 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;
|
---|
[2768] | 147 | }
|
---|
[4068] | 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 | }
|
---|
[2768] | 155 |
|
---|
[4068] | 156 | #endregion
|
---|
[2768] | 157 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 171 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 181 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 194 |
|
---|
[4068] | 195 | this.myLabel.Text = filename;
|
---|
| 196 | }
|
---|
[2768] | 197 |
|
---|
[4068] | 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);
|
---|
[2768] | 206 |
|
---|
[4068] | 207 | int iconWidth = myIcon.Icon.Width;
|
---|
| 208 | int iconHeight = myIcon.Icon.Height;
|
---|
[2768] | 209 |
|
---|
[4068] | 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);
|
---|
[2768] | 215 |
|
---|
[4068] | 216 | // Position the label just under the icon and center it.
|
---|
| 217 | int xOffset = ((int)labelSize.Width - iconWidth) / 2;
|
---|
[2768] | 218 |
|
---|
[4068] | 219 | Point labelLocation = new Point(
|
---|
| 220 | myIcon.Rectangle.Location.X - xOffset,
|
---|
| 221 | myIcon.Rectangle.Location.Y + iconHeight + 5);
|
---|
[2768] | 222 |
|
---|
[4068] | 223 | this.myLabel.Transform(new Rectangle(
|
---|
| 224 | labelLocation,
|
---|
| 225 | Size.Round(labelSize)));
|
---|
[2768] | 226 |
|
---|
[4068] | 227 | this.myLabel.Paint(g);
|
---|
[2768] | 228 |
|
---|
[4068] | 229 | mRectangle.Width = myIcon.Icon.Width + 20;
|
---|
| 230 | mRectangle.Location = new Point(
|
---|
| 231 | myIcon.Rectangle.X - 10,
|
---|
| 232 | mRectangle.Y);
|
---|
[2768] | 233 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 240 |
|
---|
[4068] | 241 | mRectangle.Height =
|
---|
| 242 | myIcon.Icon.Height +
|
---|
| 243 | myLabel.Rectangle.Height + 20;
|
---|
| 244 | }
|
---|
[2768] | 245 |
|
---|
[4068] | 246 | #region Get Icon Associated With A File
|
---|
[2768] | 247 |
|
---|
[4068] | 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 | };
|
---|
[2768] | 258 |
|
---|
[4068] | 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
|
---|
[2768] | 263 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 271 |
|
---|
[4068] | 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();
|
---|
[2768] | 285 |
|
---|
[4068] | 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 | }
|
---|
[2768] | 303 |
|
---|
[4068] | 304 | // Return the image.
|
---|
| 305 | return System.Drawing.Icon.FromHandle(shinfo.hIcon);
|
---|
[2768] | 306 | }
|
---|
[4068] | 307 |
|
---|
| 308 | #endregion
|
---|
| 309 | }
|
---|
[2768] | 310 | }
|
---|