OSDN Git Service

d70989ba879833f3aeb17ccbee0b5c63eae4c135
[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                                 try {\r
97                                         ico = ExtractIcon(null, iconPath);\r
98                                 } catch (FileNotFoundException) {\r
99                                         ico = null;\r
100                                 }\r
101                         } else if (pkg.Type == InstallerType.ARCHIVE) {\r
102                                 string progGrp = Path.Combine(NaGet.Env.ArchiveProgramGroup, pkg.Name);\r
103                                 if (Directory.Exists(progGrp)) {\r
104                                         string[] lnkFiles = Directory.GetFiles(progGrp, "*.lnk");\r
105                                         \r
106                                         if (lnkFiles.Length >= 1) {\r
107                                                 try {\r
108                                                         using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) {\r
109                                                                 if (File.Exists(link.GetPath(0))) {\r
110                                                                         ico = Icon.ExtractAssociatedIcon(link.GetPath(0));\r
111                                                                 }\r
112                                                         }\r
113                                                 } catch (System.Runtime.InteropServices.COMException) {\r
114                                                         // ShellLinkのオープンあるいは、リンク先解決に失敗した場合\r
115                                                 }\r
116                                         }\r
117                                 }\r
118                         }\r
119                         return ico;\r
120                 }\r
121                 \r
122                 /// <summary>\r
123                 /// グレーアウトアイコンを作るための ImageAttributes を作って返す。\r
124                 /// </summary>\r
125                 /// <param name="alpha">透明度。(1.0が不透明、0.0が完全透明)</param>\r
126                 /// <returns>生成されたImageAttributes</returns>\r
127                 public static ImageAttributes GetImageAttributeToGrayOut(float alpha)\r
128                 {\r
129                         // RGB比率として、YIQカラーモデルの値を採用する\r
130                         const float r = 0.298912f;\r
131                         const float g = 0.586611f;\r
132                         const float b = 0.114478f;\r
133                         \r
134                         ColorMatrix cm = new ColorMatrix(new float[][]{\r
135                                 new float[]{r, r, r,     0, 0},\r
136                                 new float[]{g, g, g,     0, 0},\r
137                                 new float[]{b, b, b,     0, 0},\r
138                                 new float[]{0, 0, 0, alpha, 0},\r
139                                 new float[]{0, 0, 0,     0, 1},\r
140                          });\r
141                         ImageAttributes ia = new ImageAttributes();\r
142                         ia.SetColorMatrix(cm);\r
143                         \r
144                         return ia;\r
145                 }\r
146                 \r
147                 /// <summary>\r
148                 /// 画像を指定領域の真中に描く\r
149                 /// </summary>\r
150                 /// <param name="g">描画対象のグラフィックス</param>\r
151                 /// <param name="img">画像</param>\r
152                 /// <param name="b">指定領域</param>\r
153                 /// <param name="ia">ImageAttributes。nullでもかまわない</param>\r
154                 public static void Graphics_DrawCenterImage(Graphics g, Image img, Rectangle b, ImageAttributes ia)\r
155                 {\r
156                         int x = b.Left + (b.Width  - img.Width ) / 2;\r
157                         int y = b.Top  + (b.Height - img.Height) / 2;\r
158                         \r
159                         g.DrawImage(img,\r
160                                     new Rectangle(x, y, img.Width, img.Height),\r
161                                     0, 0, img.Width, img.Height,\r
162                                     GraphicsUnit.Pixel, ia);\r
163                 }\r
164 \r
165         }\r
166 }\r