Repository navigation
Expand file tree
/
Copy pathIconReference.cs
More file actions
168 lines (145 loc) · 5.32 KB
/
Copy pathIconReference.cs
File metadata and controls
168 lines (145 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Copyright (c) Microsoft Corporation. All rights reserved.
// ReSharper disable AssignNullToNotNullAttribute
// ReSharper disable ConditionIsAlwaysTrueOrFalse
namespace Microsoft.WindowsAPICodePack.Shell;
/// <summary>
/// A refence to an icon resource
/// </summary>
public struct IconReference
{
#region Private members
private string? _moduleName;
private string _referencePath;
private static readonly char[] CommaSeparator = new char[] { ',' };
#endregion
/// <summary>
/// Overloaded constructor takes in the module name and resource id for the icon reference.
/// </summary>
/// <param name="moduleName">String specifying the name of an executable file, DLL, or icon file</param>
/// <param name="resourceId">Zero-based index of the icon</param>
public IconReference(string? moduleName, int resourceId)
: this()
{
if (string.IsNullOrEmpty(moduleName))
{
throw new ArgumentNullException(nameof(moduleName));
}
_moduleName = moduleName;
ResourceId = resourceId;
_referencePath = string.Format(CultureInfo.InvariantCulture,
"{0},{1}", moduleName, resourceId);
}
/// <summary>
/// Overloaded constructor takes in the module name and resource id separated by a comma.
/// </summary>
/// <param name="refPath">Reference path for the icon consiting of the module name and resource id.</param>
public IconReference(string refPath)
: this()
{
if (string.IsNullOrEmpty(refPath))
{
throw new ArgumentNullException(nameof(refPath));
}
string?[] refParams = refPath.Split(CommaSeparator);
if (refParams.Length != 2 || string.IsNullOrEmpty(refParams[0]) || string.IsNullOrEmpty(refParams[1]))
{
throw new ArgumentException(LocalizedMessages.InvalidReferencePath, nameof(refPath));
}
_moduleName = refParams[0];
// refParams[1] is guaranteed to be non-null and non-empty after the check above
ResourceId = int.Parse(refParams[1]!, CultureInfo.InvariantCulture);
_referencePath = refPath;
}
/// <summary>
/// String specifying the name of an executable file, DLL, or icon file
/// </summary>
public string? ModuleName
{
get => _moduleName;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(value));
}
_moduleName = value;
}
}
/// <summary>
/// Zero-based index of the icon
/// </summary>
public int ResourceId { get; set; }
/// <summary>
/// Reference to a specific icon within a EXE, DLL or icon file.
/// </summary>
public string ReferencePath
{
get => _referencePath;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(value));
}
string?[] refParams = value.Split(CommaSeparator);
if (refParams.Length != 2 || string.IsNullOrEmpty(refParams[0]) || string.IsNullOrEmpty(refParams[1]))
{
throw new ArgumentException(LocalizedMessages.InvalidReferencePath, nameof(value));
}
ModuleName = refParams[0];
// refParams[1] is guaranteed to be non-null and non-empty after the check above
ResourceId = int.Parse(refParams[1]!, CultureInfo.InvariantCulture);
_referencePath = value;
}
}
/// <summary>
/// Implements the == (equality) operator.
/// </summary>
/// <param name="icon1">First object to compare.</param>
/// <param name="icon2">Second object to compare.</param>
/// <returns>True if icon1 equals icon1; false otherwise.</returns>
public static bool operator ==(IconReference icon1, IconReference icon2)
{
return (icon1._moduleName == icon2._moduleName) &&
(icon1._referencePath == icon2._referencePath) &&
(icon1.ResourceId == icon2.ResourceId);
}
/// <summary>
/// Implements the != (unequality) operator.
/// </summary>
/// <param name="icon1">First object to compare.</param>
/// <param name="icon2">Second object to compare.</param>
/// <returns>True if icon1 does not equals icon1; false otherwise.</returns>
public static bool operator !=(IconReference icon1, IconReference icon2)
{
return !(icon1 == icon2);
}
/// <summary>
/// Determines if this object is equal to another.
/// </summary>
/// <param name="obj">The object to compare</param>
/// <returns>Returns true if the objects are equal; false otherwise.</returns>
public override bool Equals(object? obj)
{
if (obj == null || !(obj is IconReference)) { return false; }
return (this == (IconReference)obj);
}
/// <summary>
/// Generates a nearly unique hashcode for this structure.
/// </summary>
/// <returns>A hash code.</returns>
public override int GetHashCode()
{
if (_moduleName != null)
{
int hash = _moduleName.GetHashCode();
hash = hash * 31 + _referencePath.GetHashCode();
hash = hash * 31 + ResourceId.GetHashCode();
return hash;
}
else
{
return 0;
}
}
}