OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Collections.Generic; | |
5 using System.Drawing; | |
6 using System.IO; | |
7 using System.Windows.Forms; | |
8 using Microsoft.VisualStudio; | |
9 | |
10 namespace Microsoft.VisualStudio.Project | |
11 { | |
12 public class ImageHandler : IDisposable | |
13 { | |
14 private ImageList imageList; | |
15 private List<IntPtr> iconHandles; | |
16 private static volatile object Mutex; | |
17 private bool isDisposed; | |
18 | |
19 /// <summary> | |
20 /// Initializes the <see cref="RDTListener"/> class. | |
21 /// </summary> | |
22 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Perf
ormance", "CA1810:InitializeReferenceTypeStaticFieldsInline")] | |
23 static ImageHandler() | |
24 { | |
25 Mutex = new object(); | |
26 } | |
27 | |
28 /// <summary> | |
29 /// Builds an empty ImageHandler object. | |
30 /// </summary> | |
31 public ImageHandler() | |
32 { | |
33 } | |
34 | |
35 /// <summary> | |
36 /// Builds an ImageHandler object from a Stream providing the bi
tmap that | |
37 /// stores the images for the image list. | |
38 /// </summary> | |
39 public ImageHandler(Stream resourceStream) | |
40 { | |
41 if(null == resourceStream) | |
42 { | |
43 throw new ArgumentNullException("resourceStream"
); | |
44 } | |
45 imageList = Utilities.GetImageList(resourceStream); | |
46 } | |
47 | |
48 /// <summary> | |
49 /// Builds an ImageHandler object from an ImageList object. | |
50 /// </summary> | |
51 public ImageHandler(ImageList list) | |
52 { | |
53 if(null == list) | |
54 { | |
55 throw new ArgumentNullException("list"); | |
56 } | |
57 imageList = list; | |
58 } | |
59 | |
60 public ImageHandler(Bitmap bmp) { | |
61 if (null == bmp) { | |
62 throw new ArgumentNullException("bmp"); | |
63 } | |
64 imageList = new ImageList(); | |
65 ImageList.Images.AddStrip(bmp); | |
66 ImageList.TransparentColor = bmp.GetPixel(0, 0); | |
67 } | |
68 | |
69 /// <summary> | |
70 /// Closes the ImageHandler object freeing its resources. | |
71 /// </summary> | |
72 public void Close() | |
73 { | |
74 if(null != iconHandles) | |
75 { | |
76 foreach(IntPtr hnd in iconHandles) | |
77 { | |
78 if(hnd != IntPtr.Zero) | |
79 { | |
80 NativeMethods.DestroyIcon(hnd); | |
81 } | |
82 } | |
83 iconHandles = null; | |
84 } | |
85 | |
86 if(null != imageList) | |
87 { | |
88 imageList.Dispose(); | |
89 imageList = null; | |
90 } | |
91 } | |
92 | |
93 /// <summary> | |
94 /// Add an image to the ImageHandler. | |
95 /// </summary> | |
96 /// <param name="image">the image object to be added.</param> | |
97 public void AddImage(Image image) | |
98 { | |
99 if(null == image) | |
100 { | |
101 throw new ArgumentNullException("image"); | |
102 } | |
103 if(null == imageList) | |
104 { | |
105 imageList = new ImageList(); | |
106 } | |
107 imageList.Images.Add(image); | |
108 if(null != iconHandles) | |
109 { | |
110 iconHandles.Add(IntPtr.Zero); | |
111 } | |
112 } | |
113 | |
114 /// <summary> | |
115 /// Get or set the ImageList object for this ImageHandler. | |
116 /// </summary> | |
117 public ImageList ImageList | |
118 { | |
119 get { return imageList; } | |
120 set | |
121 { | |
122 Close(); | |
123 imageList = value; | |
124 } | |
125 } | |
126 | |
127 /// <summary> | |
128 /// Returns the handle to an icon build from the image of index | |
129 /// iconIndex in the image list. | |
130 /// </summary> | |
131 public IntPtr GetIconHandle(int iconIndex) | |
132 { | |
133 // Verify that the object is in a consistent state. | |
134 if((null == imageList)) | |
135 { | |
136 throw new InvalidOperationException(); | |
137 } | |
138 // Make sure that the list of handles is initialized. | |
139 if(null == iconHandles) | |
140 { | |
141 InitHandlesList(); | |
142 } | |
143 | |
144 // Verify that the index is inside the expected range. | |
145 if((iconIndex < 0) || (iconIndex >= iconHandles.Count)) | |
146 { | |
147 throw new ArgumentOutOfRangeException("iconIndex
"); | |
148 } | |
149 | |
150 // Check if the icon is in the cache. | |
151 if(IntPtr.Zero == iconHandles[iconIndex]) | |
152 { | |
153 Bitmap bitmap = imageList.Images[iconIndex] as B
itmap; | |
154 // If the image is not a bitmap, then we can not
build the icon, | |
155 // so we have to return a null handle. | |
156 if(null == bitmap) | |
157 { | |
158 return IntPtr.Zero; | |
159 } | |
160 | |
161 iconHandles[iconIndex] = bitmap.GetHicon(); | |
162 } | |
163 | |
164 return iconHandles[iconIndex]; | |
165 } | |
166 | |
167 private void InitHandlesList() | |
168 { | |
169 iconHandles = new List<IntPtr>(imageList.Images.Count); | |
170 for(int i = 0; i < imageList.Images.Count; ++i) | |
171 { | |
172 iconHandles.Add(IntPtr.Zero); | |
173 } | |
174 } | |
175 | |
176 #region IDisposable Members | |
177 | |
178 /// <summary> | |
179 /// Performs application-defined tasks associated with freeing,
releasing, or resetting unmanaged resources. | |
180 /// </summary> | |
181 public void Dispose() | |
182 { | |
183 this.Dispose(true); | |
184 GC.SuppressFinalize(this); | |
185 } | |
186 #endregion | |
187 | |
188 private void Dispose(bool disposing) | |
189 { | |
190 if(!this.isDisposed) | |
191 { | |
192 lock(Mutex) | |
193 { | |
194 if(disposing) | |
195 { | |
196 this.imageList.Dispose(); | |
197 } | |
198 | |
199 this.isDisposed = true; | |
200 } | |
201 } | |
202 } | |
203 } | |
204 } | |
OLD | NEW |