OSDN Git Service

ce699659dbb086c3af6b24ad7a89a5aa92bcdbbc
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / GUIUtils.cs
1 using System;\r
2 using System.IO;\r
3 using System.Drawing;\r
4 using System.Drawing.Imaging;\r
5 using NaGet.Packages;\r
6 using NaGet.Packages.Install;\r
7 \r
8 namespace AppliStation.Util\r
9 {\r
10         /// <summary>\r
11         /// GUIに関する雑多な関数\r
12         /// </summary>\r
13         public sealed class GUIUtils\r
14         {\r
15                 /// <summary>\r
16                 /// パッケージに対応するアイコンを返す\r
17                 /// </summary>\r
18                 /// <param name="pkg">パッケージ</param>\r
19                 /// <returns>対応するアイコン。検出できなかった場合はnull。</returns>\r
20                 public static Icon GetIconForPackage(InstalledPackage pkg)\r
21                 {\r
22                         string iconPath = pkg.UninstallInfo.IconPath;\r
23                         if (! string.IsNullOrEmpty(iconPath)) {\r
24                                 if (iconPath.EndsWith(",0") || iconPath.EndsWith(",-0")) {\r
25                                         iconPath = iconPath.Substring(0, iconPath.LastIndexOf(','));\r
26                                 }\r
27                                 if (File.Exists(iconPath)) {\r
28                                         return Icon.ExtractAssociatedIcon(iconPath);\r
29                                 }\r
30                         } else if (pkg.Type == InstallerType.ARCHIVE) {\r
31                                 string progGrp = Path.Combine(NaGet.Env.ArchiveProgramGroup, pkg.Name);\r
32                                 if (Directory.Exists(progGrp)) {\r
33                                         string[] lnkFiles = Directory.GetFiles(progGrp, "*.lnk");\r
34                                         \r
35                                         if (lnkFiles.Length >= 1) {\r
36                                                 using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) {\r
37                                                         if (File.Exists(link.GetPath(0))) {\r
38                                                                 return Icon.ExtractAssociatedIcon(link.GetPath(0));\r
39                                                         }\r
40                                                 }\r
41                                         }\r
42                                 }\r
43                         }\r
44                         return null;\r
45                 }\r
46                 \r
47                 /// <summary>\r
48                 /// グレーアウトアイコンを作るための ImageAttributes を作って返す。\r
49                 /// </summary>\r
50                 /// <param name="alpha">透明度。(1.0が不透明、0.0が完全透明)</param>\r
51                 /// <returns>生成されたImageAttributes</returns>\r
52                 public static ImageAttributes GetImageAttributeToGrayOut(float alpha)\r
53                 {\r
54                         // RGB比率として、YIQカラーモデルの値を採用する\r
55                         const float r = 0.298912f;\r
56                         const float g = 0.586611f;\r
57                         const float b = 0.114478f;\r
58                         \r
59                         ColorMatrix cm = new ColorMatrix(new float[][]{\r
60                                 new float[]{r, r, r,     0, 0},\r
61                                 new float[]{g, g, g,     0, 0},\r
62                                 new float[]{b, b, b,     0, 0},\r
63                                 new float[]{0, 0, 0, alpha, 0},\r
64                                 new float[]{0, 0, 0,     0, 1},\r
65                          });\r
66                         ImageAttributes ia = new ImageAttributes();\r
67                         ia.SetColorMatrix(cm);\r
68                         \r
69                         return ia;\r
70                 }\r
71                 \r
72                 /// <summary>\r
73                 /// 画像を指定領域の真中に描く\r
74                 /// </summary>\r
75                 /// <param name="g">描画対象のグラフィックス</param>\r
76                 /// <param name="img">画像</param>\r
77                 /// <param name="b">指定領域</param>\r
78                 /// <param name="ia">ImageAttributes。nullでもかまわない</param>\r
79                 public static void Graphics_DrawCenterImage(Graphics g, Image img, Rectangle b, ImageAttributes ia)\r
80                 {\r
81                         int x = b.Left + (b.Width  - img.Width ) / 2;\r
82                         int y = b.Top  + (b.Height - img.Height) / 2;\r
83                         \r
84                         g.DrawImage(img,\r
85                                     new Rectangle(x, y, img.Width, img.Height),\r
86                                     0, 0, img.Width, img.Height,\r
87                                     GraphicsUnit.Pixel, ia);\r
88                 }\r
89         }\r
90 }\r