프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / SmartUpdate / IconHelper.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (9.13 KB)

1 c4a4d59c ljiyeon
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Data;
6
using System.Runtime.InteropServices;
7
8
namespace Etier.IconHelper
9
{
10
    /// <summary>
11
    /// Provides static methods to read system icons for both folders and files.
12
    /// </summary>
13
    /// <example>
14
    /// <code>IconReader.GetFileIcon("c:\\general.xls");</code>
15
    /// </example>
16
    public class IconReader
17
    {
18
        /// <summary>
19
        /// Options to specify the size of icons to return.
20
        /// </summary>
21
        public enum IconSize
22
        {
23
            /// <summary>
24
            /// Specify large icon - 32 pixels by 32 pixels.
25
            /// </summary>
26
            Large = 0,
27
            /// <summary>
28
            /// Specify small icon - 16 pixels by 16 pixels.
29
            /// </summary>
30
            Small = 1
31
        }
32
33
        /// <summary>
34
        /// Options to specify whether folders should be in the open or closed state.
35
        /// </summary>
36
        public enum FolderType
37
        {
38
            /// <summary>
39
            /// Specify open folder.
40
            /// </summary>
41
            Open = 0,
42
            /// <summary>
43
            /// Specify closed folder.
44
            /// </summary>
45
            Closed = 1
46
        }
47
48
        /// <summary>
49
        /// Returns an icon for a given file - indicated by the name parameter.
50
        /// </summary>
51
        /// <param name="name">Pathname for file.</param>
52
        /// <param name="size">Large or small</param>
53
        /// <param name="linkOverlay">Whether to include the link icon</param>
54
        /// <returns>System.Drawing.Icon</returns>
55
        public static System.Drawing.Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
56
        {
57
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
58
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
59
60
            if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;
61
62
            /* Check the size specified for return. */
63
            if (IconSize.Small == size)
64
            {
65
                flags += Shell32.SHGFI_SMALLICON;
66
            }
67
            else
68
            {
69
                flags += Shell32.SHGFI_LARGEICON;
70
            }
71
72
            Shell32.SHGetFileInfo(name,
73
                Shell32.FILE_ATTRIBUTE_NORMAL,
74
                ref shfi,
75
                (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
76
                flags);
77
78
            // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
79
            System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
80
            User32.DestroyIcon(shfi.hIcon);     // Cleanup
81
            return icon;
82
        }
83
84
        /// <summary>
85
        /// Used to access system folder icons.
86
        /// </summary>
87
        /// <param name="size">Specify large or small icons.</param>
88
        /// <param name="folderType">Specify open or closed FolderType.</param>
89
        /// <returns>System.Drawing.Icon</returns>
90
        public static System.Drawing.Icon GetFolderIcon(IconSize size, FolderType folderType)
91
        {
92
            // Need to add size check, although errors generated at present!
93
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
94
95
            if (FolderType.Open == folderType)
96
            {
97
                flags += Shell32.SHGFI_OPENICON;
98
            }
99
100
            if (IconSize.Small == size)
101
            {
102
                flags += Shell32.SHGFI_SMALLICON;
103
            }
104
            else
105
            {
106
                flags += Shell32.SHGFI_LARGEICON;
107
            }
108
109
            // Get the folder icon
110
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
111
            Shell32.SHGetFileInfo(null,
112
                Shell32.FILE_ATTRIBUTE_DIRECTORY,
113
                ref shfi,
114
                (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
115
                flags);
116
117
            System.Drawing.Icon.FromHandle(shfi.hIcon); // Load the icon from an HICON handle
118
119
            // Now clone the icon, so that it can be successfully stored in an ImageList
120
            System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
121
122
            User32.DestroyIcon(shfi.hIcon);     // Cleanup
123
            return icon;
124
        }
125
    }
126
127
    /// <summary>
128
    /// Wraps necessary Shell32.dll structures and functions required to retrieve Icon Handles using SHGetFileInfo. Code
129
    /// courtesy of MSDN Cold Rooster Consulting case study.
130
    /// </summary>
131
    /// 
132
133
    // This code has been left largely untouched from that in the CRC example. The main changes have been moving
134
    // the icon reading code over to the IconReader type.
135
    public class Shell32
136
    {
137
138
        public const int MAX_PATH = 256;
139
        [StructLayout(LayoutKind.Sequential)]
140
        public struct SHITEMID
141
        {
142
            public ushort cb;
143
            [MarshalAs(UnmanagedType.LPArray)]
144
            public byte[] abID;
145
        }
146
147
        [StructLayout(LayoutKind.Sequential)]
148
        public struct ITEMIDLIST
149
        {
150
            public SHITEMID mkid;
151
        }
152
153
        [StructLayout(LayoutKind.Sequential)]
154
        public struct BROWSEINFO
155
        {
156
            public IntPtr hwndOwner;
157
            public IntPtr pidlRoot;
158
            public IntPtr pszDisplayName;
159
            [MarshalAs(UnmanagedType.LPTStr)]
160
            public string lpszTitle;
161
            public uint ulFlags;
162
            public IntPtr lpfn;
163
            public int lParam;
164
            public IntPtr iImage;
165
        }
166
167
        // Browsing for directory.
168
        public const uint BIF_RETURNONLYFSDIRS = 0x0001;
169
        public const uint BIF_DONTGOBELOWDOMAIN = 0x0002;
170
        public const uint BIF_STATUSTEXT = 0x0004;
171
        public const uint BIF_RETURNFSANCESTORS = 0x0008;
172
        public const uint BIF_EDITBOX = 0x0010;
173
        public const uint BIF_VALIDATE = 0x0020;
174
        public const uint BIF_NEWDIALOGSTYLE = 0x0040;
175
        public const uint BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX);
176
        public const uint BIF_BROWSEINCLUDEURLS = 0x0080;
177
        public const uint BIF_BROWSEFORCOMPUTER = 0x1000;
178
        public const uint BIF_BROWSEFORPRINTER = 0x2000;
179
        public const uint BIF_BROWSEINCLUDEFILES = 0x4000;
180
        public const uint BIF_SHAREABLE = 0x8000;
181
182
        [StructLayout(LayoutKind.Sequential)]
183
        public struct SHFILEINFO
184
        {
185
            public const int NAMESIZE = 80;
186
            public IntPtr hIcon;
187
            public int iIcon;
188
            public uint dwAttributes;
189
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
190
            public string szDisplayName;
191
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
192
            public string szTypeName;
193
        };
194
195
        public const uint SHGFI_ICON = 0x000000100;     // get icon
196
        public const uint SHGFI_DISPLAYNAME = 0x000000200;     // get display name
197
        public const uint SHGFI_TYPENAME = 0x000000400;     // get type name
198
        public const uint SHGFI_ATTRIBUTES = 0x000000800;     // get attributes
199
        public const uint SHGFI_ICONLOCATION = 0x000001000;     // get icon location
200
        public const uint SHGFI_EXETYPE = 0x000002000;     // return exe type
201
        public const uint SHGFI_SYSICONINDEX = 0x000004000;     // get system icon index
202
        public const uint SHGFI_LINKOVERLAY = 0x000008000;     // put a link overlay on icon
203
        public const uint SHGFI_SELECTED = 0x000010000;     // show icon in selected state
204
        public const uint SHGFI_ATTR_SPECIFIED = 0x000020000;     // get only specified attributes
205
        public const uint SHGFI_LARGEICON = 0x000000000;     // get large icon
206
        public const uint SHGFI_SMALLICON = 0x000000001;     // get small icon
207
        public const uint SHGFI_OPENICON = 0x000000002;     // get open icon
208
        public const uint SHGFI_SHELLICONSIZE = 0x000000004;     // get shell size icon
209
        public const uint SHGFI_PIDL = 0x000000008;     // pszPath is a pidl
210
        public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;     // use passed dwFileAttribute
211
        public const uint SHGFI_ADDOVERLAYS = 0x000000020;     // apply the appropriate overlays
212
        public const uint SHGFI_OVERLAYINDEX = 0x000000040;     // Get the index of the overlay
213
214
        public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
215
        public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
216
217
        [DllImport("Shell32.dll")]
218
        public static extern IntPtr SHGetFileInfo(
219
            string pszPath,
220
            uint dwFileAttributes,
221
            ref SHFILEINFO psfi,
222
            uint cbFileInfo,
223
            uint uFlags
224
            );
225
    }
226
227
    /// <summary>
228
    /// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example.
229
    /// </summary>
230
    public class User32
231
    {
232
        /// <summary>
233
        /// Provides access to function required to delete handle. This method is used internally
234
        /// and is not required to be called separately.
235
        /// </summary>
236
        /// <param name="hIcon">Pointer to icon handle.</param>
237
        /// <returns>N/A</returns>
238
        [DllImport("User32.dll")]
239
        public static extern int DestroyIcon(IntPtr hIcon);
240
    }
241
}
클립보드 이미지 추가 (최대 크기: 500 MB)