-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUIUtil.cs
More file actions
222 lines (191 loc) · 7.19 KB
/
Copy pathUIUtil.cs
File metadata and controls
222 lines (191 loc) · 7.19 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using PaintDotNet;
using PaintDotNet.AppModel;
using PaintDotNet.Drawing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Imaging.Effects;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace PdnCodeLab
{
internal static class UIUtil
{
private static readonly bool hiDpi = UIScaleFactor.Current.Scale > 1;
private static readonly Assembly assembly = Assembly.GetExecutingAssembly();
internal static readonly Image EmptyImage = new Bitmap(16, 16);
private static IShellService iShellService;
internal static void SetIShellService(IShellService shellService)
{
iShellService = shellService;
}
internal static Image GetImage(string resName, string directory = "Icons")
{
string resource = hiDpi ?
$"PdnCodeLab.{directory}.{resName}.32.png" :
$"PdnCodeLab.{directory}.{resName}.png";
using (Stream imageStream = assembly.GetManifestResourceStream(resource))
{
if (imageStream != null)
{
return Image.FromStream(imageStream);
}
}
if (hiDpi)
{
resource = $"PdnCodeLab.{directory}.{resName}.png";
using (Stream imageStream = assembly.GetManifestResourceStream(resource))
{
if (imageStream != null)
{
return Image.FromStream(imageStream);
}
}
}
return EmptyImage;
}
internal static Bitmap GetBitmapFromFile(string filePath)
{
try
{
// This is the BKM for loading bitmaps from files without locking the file.
// This pattern should always be used when loading a bitmap from a file.
using (Bitmap bmpTemp = new Bitmap(filePath))
{
return new Bitmap(bmpTemp);
}
}
catch
{
return null;
}
}
internal static Icon CreateIcon(string resName)
{
using (Image resImage = GetImage(resName))
using (Surface surface = Surface.CopyFromGdipImage(resImage, false))
using (Bitmap resBmp = surface.CreateAliasedGdipBitmap(BitmapAlphaMode.Premultiplied))
{
IntPtr hIcon = resBmp.GetHicon();
return Icon.FromHandle(hIcon);
}
}
internal static int Scale(int value)
{
return (int)double.Round(value * UIScaleFactor.Current.Scale);
}
internal static Size ScaleSize(int width, int height)
{
return new Size(Scale(width), Scale(height));
}
internal static string[] GetColorNames(bool includeTransparent)
{
List<string> names = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(prop => prop.PropertyType == typeof(Color))
.Select(prop => prop.Name).ToList();
if (!includeTransparent)
{
names.Remove(nameof(Color.Transparent));
}
names.Sort();
return names.ToArray();
}
internal static void LaunchUrl(IWin32Window owner, string url)
{
if (iShellService != null)
{
iShellService.LaunchUrl(owner, url);
}
else
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start(startInfo);
}
}
internal static bool LaunchFolderAndSelectFile(IWin32Window owner, string filePath)
{
if (iShellService != null)
{
return iShellService.LaunchFolderAndSelectFile(owner, filePath);
}
else
{
return ProcessUtil.TryExec("explorer.exe", new[] { "/select," + filePath }) != 0;
}
}
internal static Image CreateDisabledImage(this Image normalImage)
{
Bitmap disabledBitmap = new Bitmap(normalImage);
disabledBitmap.ApplyEffect(DisabledImageColorMatrix);
return disabledBitmap;
}
#region Matrix code copied from internal WinForms Code
private static ColorMatrixEffect s_disabledImageColorMatrix;
private static ColorMatrixEffect DisabledImageColorMatrix
{
get
{
if (s_disabledImageColorMatrix is null)
{
// this is the result of a GreyScale matrix multiplied by a transparency matrix of .5
float[][] greyscale = new float[5][];
greyscale[0] = new float[5] { 0.2125f, 0.2125f, 0.2125f, 0, 0 };
greyscale[1] = new float[5] { 0.2577f, 0.2577f, 0.2577f, 0, 0 };
greyscale[2] = new float[5] { 0.0361f, 0.0361f, 0.0361f, 0, 0 };
greyscale[3] = new float[5] { 0, 0, 0, 1, 0 };
greyscale[4] = new float[5] { 0.38f, 0.38f, 0.38f, 0, 1 };
float[][] transparency = new float[5][];
transparency[0] = new float[5] { 1, 0, 0, 0, 0 };
transparency[1] = new float[5] { 0, 1, 0, 0, 0 };
transparency[2] = new float[5] { 0, 0, 1, 0, 0 };
transparency[3] = new float[5] { 0, 0, 0, .7F, 0 };
transparency[4] = new float[5] { 0, 0, 0, 0, 0 };
ColorMatrix colorMatrix = MultiplyColorMatrix(transparency, greyscale);
s_disabledImageColorMatrix = new ColorMatrixEffect(colorMatrix);
}
return s_disabledImageColorMatrix;
}
}
/// <summary>
/// Multiply two 5x5 color matrices.
/// </summary>
private static ColorMatrix MultiplyColorMatrix(float[][] matrix1, float[][] matrix2)
{
const int Size = 5;
// Build up an empty 5x5 array for results.
float[][] result = new float[Size][];
for (int row = 0; row < Size; row++)
{
result[row] = new float[Size];
}
float[] column = new float[Size];
for (int j = 0; j < Size; j++)
{
for (int k = 0; k < Size; k++)
{
column[k] = matrix1[k][j];
}
for (int i = 0; i < Size; i++)
{
float[] row = matrix2[i];
float s = 0;
for (int k = 0; k < Size; k++)
{
s += row[k] * column[k];
}
result[i][j] = s;
}
}
return new ColorMatrix(result);
}
#endregion
}
}