OSDN Git Service

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