X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=AppliStation%2FAppliStation.Util%2FGUIUtils.cs;h=bd2a8ff28e83a5739d174a40b69772b647d6c03c;hb=3033b219e37faf6ab6795219c2f6acfdaf47fec6;hp=7e235d6e38aba285da52692481137135fcabd037;hpb=b84c03d933677cd9b7f685b8d44330b7e96155a7;p=applistation%2FAppliStation.git diff --git a/AppliStation/AppliStation.Util/GUIUtils.cs b/AppliStation/AppliStation.Util/GUIUtils.cs index 7e235d6..bd2a8ff 100644 --- a/AppliStation/AppliStation.Util/GUIUtils.cs +++ b/AppliStation/AppliStation.Util/GUIUtils.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; +using System.Windows.Forms; using NaGet.Packages; using NaGet.Packages.Install; @@ -12,6 +14,42 @@ namespace AppliStation.Util /// public sealed class GUIUtils { + #region アイコン関連 + + #region ExtraIcon関連 + + /// + /// アイコンファイル(実行ファイル・DLL)を開いてアイコンを作る + /// + /// ハンドラ + /// 対象ファイルとアイコンインデックスの文字列表現 + /// 生成されたアイコン。 + public static Icon ExtractIcon(Form form, string lpszExeFileNameAndIndex) + { + int index = lpszExeFileNameAndIndex.LastIndexOf(','); + if (index >= 0) { + uint nIconIndex = uint.Parse(lpszExeFileNameAndIndex.Substring(index+1)); + return NativeMethods.ExtractIcon(form, lpszExeFileNameAndIndex.Substring(0, index), nIconIndex); + } else { + return Icon.ExtractAssociatedIcon(lpszExeFileNameAndIndex); + } + } + + /// + /// シェルからフォルダーアイコンを生成して返す + /// + /// フォルダーアイコン + public static Icon ShellIconForFolder + { + get { + // Vista以降ならば、SHGetStockIconInfo(SIID_FOLDER, SHGSI_ICON, &sInfo); をP/Invoke呼び出しするのが王道かと + string windir = Environment.GetEnvironmentVariable("windir"); + return NativeMethods.ExtractIcon(null, Path.Combine(windir, @"system32\shell32.dll"), 3); + } + } + + #endregion + /// /// パッケージに対応するアイコンを返す /// @@ -19,29 +57,31 @@ namespace AppliStation.Util /// 対応するアイコン。検出できなかった場合はnull。 public static Icon GetIconForPackage(InstalledPackage pkg) { - string iconPath = pkg.UninstallInfo.IconPath; - if (! string.IsNullOrEmpty(iconPath)) { - if (iconPath.EndsWith(",0") || iconPath.EndsWith(",-0")) { - iconPath = iconPath.Substring(0, iconPath.LastIndexOf(',')); - } - if (File.Exists(iconPath)) { - return Icon.ExtractAssociatedIcon(iconPath); - } - } else if (pkg.Type == InstallerType.ARCHIVE) { - string progGrp = Path.Combine(NaGet.Env.ArchiveProgramGroup, pkg.Name); - if (Directory.Exists(progGrp)) { - string[] lnkFiles = Directory.GetFiles(progGrp, "*.lnk"); - - if (lnkFiles.Length >= 1) { - using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) { - if (File.Exists(link.GetPath(0))) { - return Icon.ExtractAssociatedIcon(link.GetPath(0)); + Icon ico = null; + try { + string iconPath = pkg.UninstallInfo.IconPath; + if (! string.IsNullOrEmpty(iconPath)) { + ico = ExtractIcon(null, iconPath); + } else if ((pkg.Type == InstallerType.ARCHIVE) + ||(pkg.Type == InstallerType.ITSELF)) { + string progGrp = Path.Combine(NaGet.Env.ArchiveProgramGroup, pkg.Name); + if (Directory.Exists(progGrp)) { + string[] lnkFiles = Directory.GetFiles(progGrp, "*.lnk"); + + if (lnkFiles.Length >= 1) { + using (NaGet.InteropServices.ShellLink link = new NaGet.InteropServices.ShellLink(lnkFiles[0])) { + if (File.Exists(link.GetPath(0))) { + ico = Icon.ExtractAssociatedIcon(link.GetPath(0)); + } } } } } + } catch (System.Runtime.InteropServices.COMException) { + // ShellLinkのオープンあるいは、リンク先解決に失敗した場合 + } catch (Exception) { } - return null; + return ico; } /// @@ -57,11 +97,11 @@ namespace AppliStation.Util const float b = 0.114478f; ColorMatrix cm = new ColorMatrix(new float[][]{ - new float[]{r, r, r, 0, 0}, - new float[]{g, g, g, 0, 0}, - new float[]{b, b, b, 0, 0}, + new float[]{r, r, r, 0, 0}, + new float[]{g, g, g, 0, 0}, + new float[]{b, b, b, 0, 0}, new float[]{0, 0, 0, alpha, 0}, - new float[]{0, 0, 0, 0, 1}, + new float[]{0, 0, 0, 0, 1}, }); ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); @@ -69,6 +109,13 @@ namespace AppliStation.Util return ia; } + /// + /// 画像を指定領域の真中に描く + /// + /// 描画対象のグラフィックス + /// 画像 + /// 指定領域 + /// ImageAttributes。nullでもかまわない public static void Graphics_DrawCenterImage(Graphics g, Image img, Rectangle b, ImageAttributes ia) { int x = b.Left + (b.Width - img.Width ) / 2; @@ -79,5 +126,30 @@ namespace AppliStation.Util 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); } + + #endregion + + #region CheckedListBox関連 + + /// + /// チェックリストボックスのアイテムをスワップする + /// + /// 操作対象のCheckedListBox + /// アイテムインデックス + /// アイテムインデックス + /// インデックス値のチェックは本メソッドでは行っていない + public static void CheckedListBox_SwapItems(CheckedListBox checkedListBox, int indexA, int indexB) { + int itemCount = checkedListBox.Items.Count; + object tempItem = checkedListBox.Items[indexA]; + CheckState tempState = checkedListBox.GetItemCheckState(indexA); + + checkedListBox.Items[indexA] = checkedListBox.Items[indexB]; + checkedListBox.SetItemCheckState(indexA, checkedListBox.GetItemCheckState(indexB)); + + checkedListBox.Items[indexB] = tempItem; + checkedListBox.SetItemCheckState(indexB, tempState); + } + + #endregion } }