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.Runtime.InteropServices;\r
4 using System.Drawing;\r
5 using System.Drawing.Imaging;\r
6 using NaGet.Packages;\r
7 using NaGet.Packages.Install;\r
8 \r
9 namespace AppliStation.Util\r
10 {\r
11         /// <summary>\r
12         /// GUIに関する雑多な関数\r
13         /// </summary>\r
14         public sealed class GUIUtils\r
15         {\r
16                 #region ExtraIcon関連\r
17                 \r
18                 [DllImport("shell32.dll")]\r
19                 private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);\r
20                 \r
21                 [DllImport("user32.dll", SetLastError=true)]\r
22                 [return: MarshalAs(UnmanagedType.Bool)]\r
23                 private static extern bool DestroyIcon(IntPtr hIcon);\r
24                 \r
25                 /// <summary>\r
26                 /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る\r
27                 /// </summary>\r
28                 /// <remarks>内部でコピーされるのでWin32APIのDestroyIconを使わないでいいが、やや遅い動作</remarks>\r
29                 /// <param name="form">ハンドラ</param>\r
30                 /// <param name="lpszExeFileName">対象ファイル</param>\r
31                 /// <param name="nIconIndex">アイコンインデックス</param>\r
32                 /// <returns>生成されたアイコン</returns>\r
33                 public static Icon ExtractIcon(System.Windows.Forms.Form form, string lpszExeFileName, uint nIconIndex)\r
34                 {\r
35                         Icon ico = null;\r
36                         \r
37                         if (! File.Exists(lpszExeFileName)) {\r
38                                 ico = Icon.ExtractAssociatedIcon(lpszExeFileName); // ExtractAssociatedIconに例外を吐いてもらう\r
39                         } else {\r
40                                 IntPtr hInst = (form != null)? form.Handle : IntPtr.Zero;\r
41                                 IntPtr hIcon = IntPtr.Zero;\r
42                                 \r
43                                 try {\r
44                                         hIcon = ExtractIcon(hInst, lpszExeFileName, (int) nIconIndex);\r
45                                         if ((hIcon != IntPtr.Zero) && (hIcon.ToInt32() != 2)) {\r
46                                                 ico = (Icon) Icon.FromHandle(hIcon).Clone();\r
47                                                 DestroyIcon(hIcon);\r
48                                         }\r
49                                 } catch (System.Runtime.InteropServices.COMException) {\r
50                                         // ExtraIconのP/Invoke失敗時用\r
51                                 }\r
52                         }\r
53                         return ico;\r
54                 }\r
55                 \r
56                 /// <summary>\r
57                 /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る\r
58                 /// </summary>\r
59                 /// <param name="form">ハンドラ</param>\r
60                 /// <param name="lpszExeFileNameAndIndex">対象ファイルとアイコンインデックスの文字列表現</param>\r
61                 /// <returns>生成されたアイコン。</returns>\r
62                 public static Icon ExtractIcon(System.Windows.Forms.Form form, string lpszExeFileNameAndIndex)\r
63                 {\r
64                         int index = lpszExeFileNameAndIndex.LastIndexOf(',');\r
65                         if (index >= 0) {\r
66                                 uint nIconIndex = uint.Parse(lpszExeFileNameAndIndex.Substring(index+1));\r
67                                 return ExtractIcon(form, lpszExeFileNameAndIndex.Substring(0, index), nIconIndex);\r
68                         } else {\r
69                                 return Icon.ExtractAssociatedIcon(lpszExeFileNameAndIndex);\r
70                         }\r
71                 }\r
72                 \r
73                 /// <summary>\r
74                 /// シェルからフォルダアイコンを生成して返す\r
75                 /// </summary>\r
76                 /// <returns>フォルダアイコン</returns>\r
77                 public static Icon GetShellIconForFolder()\r
78                 {\r
79                         // Vista以降ならば、SHGetStockIconInfo(SIID_FOLDER, SHGSI_ICON, &sInfo); をP/Invoke呼び出しするのが王道かと\r
80                         string windir = Environment.GetEnvironmentVariable("windir");\r
81                         return ExtractIcon(null, Path.Combine(windir, @"system32\shell32.dll"), 3);\r
82                 }\r
83                 \r
84                 #endregion\r
85                 \r
86                 /// <summary>\r
87                 /// パッケージに対応するアイコンを返す\r
88                 /// </summary>\r
89                 /// <param name="pkg">パッケージ</param>\r
90                 /// <returns>対応するアイコン。検出できなかった場合はnull。</returns>\r
91                 public static Icon GetIconForPackage(InstalledPackage pkg)\r
92                 {\r
93                         Icon ico = null;\r
94                         string iconPath = pkg.UninstallInfo.IconPath;\r
95                         if (! string.IsNullOrEmpty(iconPath)) {\r
96                                 ico = ExtractIcon(null, iconPath);\r
97                         } else if (pkg.Type == InstallerType.ARCHIVE) {\r
98                                 string progGrp = Path.Combine(NaGet.Env.ArchiveProgramGroup, pkg.Name);\r
99                                 if (Directory.Exists(progGrp)) {\r
100                                         string[] lnkFiles = Directory.GetFiles(progGrp, "*.lnk");\r
101                                         \r
102                                         if (lnkFiles.Length >= 1) {\r
103                                                 try {\r
104                                                         using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) {\r
105                                                                 if (File.Exists(link.GetPath(0))) {\r
106                                                                         ico = Icon.ExtractAssociatedIcon(link.GetPath(0));\r
107                                                                 }\r
108                                                         }\r
109                                                 } catch (System.Runtime.InteropServices.COMException) {\r
110                                                         // ShellLinkのオープンあるいは、リンク先解決に失敗した場合\r
111                                                 }\r
112                                         }\r
113                                 }\r
114                         }\r
115                         return ico;\r
116                 }\r
117                 \r
118                 /// <summary>\r
119                 /// グレーアウトアイコンを作るための ImageAttributes を作って返す。\r
120                 /// </summary>\r
121                 /// <param name="alpha">透明度。(1.0が不透明、0.0が完全透明)</param>\r
122                 /// <returns>生成されたImageAttributes</returns>\r
123                 public static ImageAttributes GetImageAttributeToGrayOut(float alpha)\r
124                 {\r
125                         // RGB比率として、YIQカラーモデルの値を採用する\r
126                         const float r = 0.298912f;\r
127                         const float g = 0.586611f;\r
128                         const float b = 0.114478f;\r
129                         \r
130                         ColorMatrix cm = new ColorMatrix(new float[][]{\r
131                                 new float[]{r, r, r,     0, 0},\r
132                                 new float[]{g, g, g,     0, 0},\r
133                                 new float[]{b, b, b,     0, 0},\r
134                                 new float[]{0, 0, 0, alpha, 0},\r
135                                 new float[]{0, 0, 0,     0, 1},\r
136                          });\r
137                         ImageAttributes ia = new ImageAttributes();\r
138                         ia.SetColorMatrix(cm);\r
139                         \r
140                         return ia;\r
141                 }\r
142                 \r
143                 /// <summary>\r
144                 /// 画像を指定領域の真中に描く\r
145                 /// </summary>\r
146                 /// <param name="g">描画対象のグラフィックス</param>\r
147                 /// <param name="img">画像</param>\r
148                 /// <param name="b">指定領域</param>\r
149                 /// <param name="ia">ImageAttributes。nullでもかまわない</param>\r
150                 public static void Graphics_DrawCenterImage(Graphics g, Image img, Rectangle b, ImageAttributes ia)\r
151                 {\r
152                         int x = b.Left + (b.Width  - img.Width ) / 2;\r
153                         int y = b.Top  + (b.Height - img.Height) / 2;\r
154                         \r
155                         g.DrawImage(img,\r
156                                     new Rectangle(x, y, img.Width, img.Height),\r
157                                     0, 0, img.Width, img.Height,\r
158                                     GraphicsUnit.Pixel, ia);\r
159                 }\r
160 \r
161         }\r
162 }\r