Anyone can comment, so leave your views

Tuesday, November 25, 2008

Select Files & Folders: C# dotnet project excerpts

I recently had to do a MS Visual Studio DotNet C# project to mimic the back up tab of the Backup utility (attached image)
 
Two sites that helped : http://www.tangiblesoftwaresolutions.com/ and http://www.infragistics.com/ (tree view, over riding how a control is to be drawn, native support for 3 states of check boxes)
 
 
Interesting part is how it has to select parent nodes and gray them when you select a node whose parent/ grand parents are not selected.
 
Here is my altervative to using a image list and instead caching a few images
 
using System;
using System.Threading;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
 
using System.Data;
 
using System.Text;
using Infragistics.Win;
 
//using Infragistics.Win.UltraWinTree;
using Icons2;
 
using Infragistics.Win.UltraWinListView;
using Infragistics.Win.UltraWinTree;
using Infragistics.Shared.Serialization;
using FldrSel.draw;
using System.Runtime.InteropServices;
 
namespace Icons2
{
    /**
    * a holder for an icon and its ID
    * not required in all cases
    */
    public class IconInf
    {
        public Icon theIcon;
        public int id;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };
 
    class Win32
    {
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
 
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    }
   
    public class Othr
    {
        private struct SHFILEINFO
        {
            public IntPtr hIcon; // : icon
            public int iIcon; // : icondex
            public int dwAttributes; // : SFGAO_ flags
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }
 
        [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint = "SHGetFileInfo", ExactSpelling = false, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);
 
        private const int SHGFI_ICON = 0X100;
        private const int SHGFI_SMALLICON = 0X1;
        private const int SHGFI_LARGEICON = 0X0; // Large icon
  
        public const string FOLDER_STR = "[folder]";
        public const string DRIVE_STR = "[folder]";
        Dictionary<string, IconInf> icons = new Dictionary<string, IconInf>();
 
        public Othr()
        {
        }
        public void init(){
            string sExtn = "txt";
            if (!icons.ContainsKey(sExtn))
            {
 
                DefaultIcon di = new DefaultIcon();
                IconInf oIconInf = new IconInf();
                oIconInf.theIcon = di.GetIcon(sExtn);
                oIconInf.id = icons.Count;
                icons.Add(sExtn, oIconInf);
            }
        }
 

        public IconInf getIcon(string sFileName)
        {
            return getIcon2(sFileName);
        }
        private IconInf getIcon2(string FileName)
        {
            lock (icons)
            {
                if (icons.Count > 1100)
                {
                    System.Collections.ArrayList keys = new System.Collections.ArrayList(icons.Keys);
                    for (int i = 700; i < 800; i++)//remove from the middle, hopefully they are less used
                    {                      
                        icons.Remove(keys[i].ToString());
                       
                    }
                    init();//reinit
 
                }
            }
           
            System.Drawing.Icon myIcon = null;
 

            IntPtr hImgSmall = default(IntPtr); //The handle to the system image list.
            IntPtr hImgLarge = default(IntPtr);
            string sExtn = null;
            sExtn = "txt";
 
 
 
            try
            {
               
                //FileName = "d:\Dev"
                if (!System.IO.Directory.Exists(FileName))
                {
                    sExtn = FileName.Substring((FileName.LastIndexOf(".") + 1));
                }
                else
                {
                    sExtn = FileName;
                }
                if (icons.ContainsKey(FileName))
                {
                    sExtn = FileName;
                }
                else if (icons.ContainsKey(sExtn))
                {
                    //do nothing
                }
                else
                {
                    DefaultIcon di = new DefaultIcon();
                    IconInf oIconInf =  new IconInf();
                    oIconInf.theIcon = di.GetIcon(sExtn);
                    oIconInf.id = icons.Count;
                    icons.Add(sExtn, oIconInf);
 
                }
 
            }
            catch (Exception ex)
            {
 
                try
                {
                    SHFILEINFO shinfo = new SHFILEINFO();
                    shinfo = new SHFILEINFO();
                    hImgSmall = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
                    myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
                    //icons[sExtn] = myIcon;
                   
                    IconInf oIconInf = new IconInf();
                    oIconInf.theIcon = myIcon;
                    oIconInf.id = icons.Count;
                    icons.Add(sExtn, oIconInf);
                }
                catch (Exception ex2)
                {
                    //default
                    sExtn = "txt";
                }
            }
 
            IconInf oIconInf2 = icons[sExtn];           
            return oIconInf2;
 
        }
       
    }
}

G
 
Forum
Ads by google:











Source code, testing article - one stop shop Software applications written in Java for cell phones, PHP, Java Dot net, PHP & Perl for the web and Vb6, Java, VB6, Perl and Excel VBA for the desktop .

- Tushar G Kapila
Bangalore India 2007 © Remaining page can be removed if you save on your pc or server.

Contributors