Changeset 230


Ignore:
Timestamp:
3/11/2008 10:15:07 AM (7 years ago)
Author:
lowjoel
Message:

Use the PathCompactPathEx? function to code a GetCompactPath? function for use with the progress dialog.

Location:
branches/eraser6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Eraser/ProgressForm.Designer.cs

    r220 r230  
    108108            // item 
    109109            //  
    110             this.item.AutoSize = true; 
    111110            this.item.Location = new System.Drawing.Point(215, 61); 
    112111            this.item.Name = "item"; 
    113             this.item.Size = new System.Drawing.Size(71, 15); 
     112            this.item.Size = new System.Drawing.Size(220, 15); 
    114113            this.item.TabIndex = 7; 
    115114            this.item.Text = "C:\\...\\File.txt"; 
  • branches/eraser6/Eraser/ProgressForm.cs

    r220 r230  
    88 
    99using Eraser.Manager; 
     10using Eraser.Util; 
    1011 
    1112namespace Eraser 
     
    4243            } 
    4344 
    44             item.Text = e.CurrentItemName; 
     45            item.Text = File.GetCompactPath(e.CurrentItemName, item.Width, item.Font); 
    4546            pass.Text = string.Format("{0} out of {1}", e.CurrentPass, e.TotalPasses); 
    4647            timeLeft.Text = string.Format("{0} left", new TimeSpan(0, 0, e.TimeLeft).ToString()); 
     
    8182                    break; 
    8283            } 
     84 
     85            //Change the Stop button to be a Close button. 
     86            stop.Text = "Close"; 
    8387        } 
    8488 
  • branches/eraser6/Util/File.cs

    r135 r230  
    44 
    55using System.Runtime.InteropServices; 
     6using System.Windows.Forms; 
    67using System.Drawing; 
    78 
     
    4243            return Icon.FromHandle(shfi.hIcon); 
    4344        } 
     45 
     46        public static string GetCompactPath(string longPath, int newWidth, Font drawFont) 
     47        { 
     48            using (Control ctrl = new Control()) 
     49            { 
     50                //First check if the source string is too long. 
     51                Graphics g = ctrl.CreateGraphics(); 
     52                int width = g.MeasureString(longPath, drawFont).ToSize().Width; 
     53                if (width <= newWidth) 
     54                    return longPath; 
     55 
     56                //It is, shorten it. 
     57                int aveCharWidth = width / longPath.Length; 
     58                int charCount = newWidth / aveCharWidth; 
     59                StringBuilder builder = new StringBuilder(); 
     60                builder.Append(longPath); 
     61                builder.EnsureCapacity(charCount); 
     62 
     63                while (g.MeasureString(builder.ToString(), drawFont).Width > newWidth) 
     64                { 
     65                    if (PathCompactPathEx(builder, longPath, (UIntPtr)(--charCount), 
     66                        (UIntPtr)0) == UIntPtr.Zero) 
     67                    { 
     68                        return string.Empty; 
     69                    } 
     70                } 
     71 
     72                return builder.ToString(); 
     73            } 
     74        } 
     75 
     76        /// <summary> 
     77        /// Truncates a path to fit within a certain number of characters by 
     78        /// replacing path components with ellipses. 
     79        /// </summary> 
     80        /// <param name="pszOut">[out] The address of the string that has been altered.</param> 
     81        /// <param name="pszSrc">[in] A pointer to a null-terminated string of maximum 
     82        /// length MAX_PATH that contains the path to be altered.</param> 
     83        /// <param name="cchMax">[in] The maximum number of characters to be 
     84        /// contained in the new string, including the terminating NULL character. 
     85        /// For example, if cchMax = 8, the resulting string can contain a maximum 
     86        /// of 7 characters plus the terminating NULL character.</param> 
     87        /// <param name="dwFlags">Reserved.</param> 
     88        /// <returns>Returns TRUE if successful, or FALSE otherwise.</returns> 
     89        [DllImport("Shlwapi.dll")] 
     90        private static extern UIntPtr PathCompactPathEx( 
     91            StringBuilder pszOut, string pszSrc, UIntPtr cchMax, UIntPtr dwFlags); 
    4492 
    4593        /// <summary> 
  • branches/eraser6/Util/Util.csproj

    r167 r230  
    3232    <Reference Include="System.Data" /> 
    3333    <Reference Include="System.Drawing" /> 
     34    <Reference Include="System.Windows.Forms" /> 
    3435    <Reference Include="System.Xml" /> 
    3536  </ItemGroup> 
Note: See TracChangeset for help on using the changeset viewer.