OSDN Git Service

AppliStation-GUI,ショートカットファイルが不正(リンク先解決不能状態:持ち運びなどで発生)の際にアンインストールダイアログが開けなくなるバグを修正
[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                                                 try {\r
37                                                         using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) {\r
38                                                                 if (File.Exists(link.GetPath(0))) {\r
39                                                                         return Icon.ExtractAssociatedIcon(link.GetPath(0));\r
40                                                                 }\r
41                                                         }\r
42                                                 } catch (System.Runtime.InteropServices.COMException) {\r
43                                                         // ShellLinkのオープンあるいは、リンク先解決に失敗した場合\r
44                                                 }\r
45                                         }\r
46                                 }\r
47                         }\r
48                         return null;\r
49                 }\r
50                 \r
51                 /// <summary>\r
52                 /// グレーアウトアイコンを作るための ImageAttributes を作って返す。\r
53                 /// </summary>\r
54                 /// <param name="alpha">透明度。(1.0が不透明、0.0が完全透明)</param>\r
55                 /// <returns>生成されたImageAttributes</returns>\r
56                 public static ImageAttributes GetImageAttributeToGrayOut(float alpha)\r
57                 {\r
58                         // RGB比率として、YIQカラーモデルの値を採用する\r
59                         const float r = 0.298912f;\r
60                         const float g = 0.586611f;\r
61                         const float b = 0.114478f;\r
62                         \r
63                         ColorMatrix cm = new ColorMatrix(new float[][]{\r
64                                 new float[]{r, r, r,     0, 0},\r
65                                 new float[]{g, g, g,     0, 0},\r
66                                 new float[]{b, b, b,     0, 0},\r
67                                 new float[]{0, 0, 0, alpha, 0},\r
68                                 new float[]{0, 0, 0,     0, 1},\r
69                          });\r
70                         ImageAttributes ia = new ImageAttributes();\r
71                         ia.SetColorMatrix(cm);\r
72                         \r
73                         return ia;\r
74                 }\r
75                 \r
76                 /// <summary>\r
77                 /// 画像を指定領域の真中に描く\r
78                 /// </summary>\r
79                 /// <param name="g">描画対象のグラフィックス</param>\r
80                 /// <param name="img">画像</param>\r
81                 /// <param name="b">指定領域</param>\r
82                 /// <param name="ia">ImageAttributes。nullでもかまわない</param>\r
83                 public static void Graphics_DrawCenterImage(Graphics g, Image img, Rectangle b, ImageAttributes ia)\r
84                 {\r
85                         int x = b.Left + (b.Width  - img.Width ) / 2;\r
86                         int y = b.Top  + (b.Height - img.Height) / 2;\r
87                         \r
88                         g.DrawImage(img,\r
89                                     new Rectangle(x, y, img.Width, img.Height),\r
90                                     0, 0, img.Width, img.Height,\r
91                                     GraphicsUnit.Pixel, ia);\r
92                 }\r
93         }\r
94 }\r