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
9 namespace AppliStation.Util
\r
14 public sealed class GUIUtils
\r
18 [DllImport("shell32.dll")]
\r
19 private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
\r
21 [DllImport("user32.dll", SetLastError=true)]
\r
22 [return: MarshalAs(UnmanagedType.Bool)]
\r
23 private static extern bool DestroyIcon(IntPtr hIcon);
\r
26 /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る
\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
37 if (! File.Exists(lpszExeFileName)) {
\r
38 ico = Icon.ExtractAssociatedIcon(lpszExeFileName); // ExtractAssociatedIconに例外を吐いてもらう
\r
40 IntPtr hInst = (form != null)? form.Handle : IntPtr.Zero;
\r
41 IntPtr hIcon = IntPtr.Zero;
\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
49 } catch (System.Runtime.InteropServices.COMException) {
\r
50 // ExtraIconのP/Invoke失敗時用
\r
57 /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る
\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
64 int index = lpszExeFileNameAndIndex.LastIndexOf(',');
\r
66 uint nIconIndex = uint.Parse(lpszExeFileNameAndIndex.Substring(index+1));
\r
67 return ExtractIcon(form, lpszExeFileNameAndIndex.Substring(0, index), nIconIndex);
\r
69 return Icon.ExtractAssociatedIcon(lpszExeFileNameAndIndex);
\r
74 /// シェルからフォルダアイコンを生成して返す
\r
76 /// <returns>フォルダアイコン</returns>
\r
77 public static Icon GetShellIconForFolder()
\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
87 /// パッケージに対応するアイコンを返す
\r
89 /// <param name="pkg">パッケージ</param>
\r
90 /// <returns>対応するアイコン。検出できなかった場合はnull。</returns>
\r
91 public static Icon GetIconForPackage(InstalledPackage pkg)
\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
102 if (lnkFiles.Length >= 1) {
\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
109 } catch (System.Runtime.InteropServices.COMException) {
\r
110 // ShellLinkのオープンあるいは、リンク先解決に失敗した場合
\r
119 /// グレーアウトアイコンを作るための ImageAttributes を作って返す。
\r
121 /// <param name="alpha">透明度。(1.0が不透明、0.0が完全透明)</param>
\r
122 /// <returns>生成されたImageAttributes</returns>
\r
123 public static ImageAttributes GetImageAttributeToGrayOut(float alpha)
\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
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
137 ImageAttributes ia = new ImageAttributes();
\r
138 ia.SetColorMatrix(cm);
\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
152 int x = b.Left + (b.Width - img.Width ) / 2;
\r
153 int y = b.Top + (b.Height - img.Height) / 2;
\r
156 new Rectangle(x, y, img.Width, img.Height),
\r
157 0, 0, img.Width, img.Height,
\r
158 GraphicsUnit.Pixel, ia);
\r