OSDN Git Service

AppliStation-GUI,コマンドプロンプトで開く、のも追加。
authorttp <ttp@users.sourceforge.jp>
Sat, 23 Aug 2008 12:59:58 +0000 (12:59 +0000)
committerttp <ttp@users.sourceforge.jp>
Sat, 23 Aug 2008 12:59:58 +0000 (12:59 +0000)
 * CUIプログラムの存在が確認されたときのみ、出現する
 * 下記の2つに、インストール先フォルダがセットされる。
   * カレントディレクトリ
   * パス変数

git-svn-id: http://localhost/svn/AppliStation/trunk@953 34ed2c89-c49f-4a4b-abdb-c318350108cf

AppliStation/AppliStation.Util/ToolStripPetitLauncherMenuItem.cs

index 07c6451..df0dd13 100644 (file)
@@ -55,27 +55,41 @@ namespace AppliStation.Util
                                DropDownItems.Add(item);\r
                        }\r
                }\r
+               private delegate void ToolStripItemCollection_InsertDelegate(int index, ToolStripItem item);\r
+               private void _insertToItemsInv(int index, ToolStripItem item)\r
+               {\r
+                       if (Parent.InvokeRequired) {\r
+                               Parent.Invoke(new ToolStripItemCollection_InsertDelegate(DropDownItems.Insert), index, item);\r
+                       } else {\r
+                               DropDownItems.Insert(index, item);\r
+                       }\r
+               }\r
+               \r
                        \r
                private void buildItems()\r
                {\r
                        try {\r
                                string folderPath = baseFolderPath;\r
                                ToolStripSeparator sep = new ToolStripSeparator();\r
+                               bool cmdIsAdded = false;\r
                                \r
-                               {\r
-                                       ToolStripMenuItem item = CreateMenuItemForFile(folderPath);\r
-                                       item.Text = "フォルダを開く(&O)";\r
-                                       \r
-                                       _addToItemsInv(item);\r
-                               }\r
+                               _addToItemsInv(CreateMenuItemForFolder(folderPath));\r
                                _addToItemsInv(sep);\r
                                \r
                                if (Directory.Exists(folderPath)) {\r
                                        foreach (string exeFile in Directory.GetFiles(folderPath, "*.exe")) {\r
                                                if (baseFolderPath != folderPath) return; // 途中でなんか操作されているならば終了\r
                                                \r
-                                               if (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile) == NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI) {\r
-                                                       _addToItemsInv(CreateMenuItemForFile(exeFile));\r
+                                               switch (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile)) {\r
+                                                       case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI:\r
+                                                               _addToItemsInv(CreateMenuItemForFile(exeFile));\r
+                                                               break;\r
+                                                       case NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinConsole:\r
+                                                               if (! cmdIsAdded) {\r
+                                                                       _insertToItemsInv(0, CreateMenuItemForCmdAt(folderPath));\r
+                                                                       cmdIsAdded = true;\r
+                                                               }\r
+                                                               break;\r
                                                }\r
                                        }\r
                                }\r
@@ -83,6 +97,18 @@ namespace AppliStation.Util
                }\r
                \r
                #endregion\r
+               \r
+               /// <summary>\r
+               /// 与えられたフォルダを開くメニューアイテムを生成する\r
+               /// </summary>\r
+               /// <param name="folderPath"></param>\r
+               /// <returns></returns>\r
+               public static ToolStripMenuItem CreateMenuItemForFolder(string folderPath)\r
+               {\r
+                       ToolStripMenuItem item = CreateMenuItemForFile(folderPath);\r
+                       item.Text = "フォルダを開く(&O)";\r
+                       return item;\r
+               }\r
 \r
                /// <summary>\r
                /// 与えられたファイルのランチャーに相当するメニューアイテムを生成する\r
@@ -135,5 +161,45 @@ namespace AppliStation.Util
                        procInfo.WorkingDirectory = Path.GetDirectoryName(procInfo.FileName);\r
                        Process.Start(procInfo);\r
                }\r
+               \r
+               /// <summary>\r
+               /// 与えられたフォルダのところでコマンドプロンプトを開く。\r
+               /// </summary>\r
+               /// <param name="folderPath">カレントフォルダ及びパスに追加されるフォルダ</param>\r
+               /// <returns>生成されたメニューアイテム</returns>\r
+               public static ToolStripMenuItem CreateMenuItemForCmdAt(string folderPath)\r
+               {\r
+                       string cmdPath = Environment.GetEnvironmentVariable("comspec");\r
+                       if (string.IsNullOrEmpty(cmdPath) || (!File.Exists(cmdPath)))\r
+                               throw new FileNotFoundException(null, Environment.ExpandEnvironmentVariables("%comspec%"));\r
+                       \r
+                       ToolStripMenuItem item = new ToolStripMenuItem();\r
+                       \r
+                       item.Text = string.Format("この場所で{0}を開く(&C)", (cmdPath.EndsWith(@"\command.com"))? "MS-DOSプロンプト" : "コマンドプロンプト");\r
+                       item.Tag = folderPath;\r
+                       item.Click += new System.EventHandler(menuItemForCmdAtClicked);\r
+                       try {\r
+                               item.Image = Icon.ExtractAssociatedIcon(cmdPath).ToBitmap();\r
+                       } catch (Exception) {}\r
+                       \r
+                       return item;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// <see cref="CreateMenuItemForCmdAt" />により使われるイベントハンドラ\r
+               /// </summary>\r
+               /// <param name="sender"></param>\r
+               /// <param name="e"></param>\r
+               private static void menuItemForCmdAtClicked(object sender, EventArgs e)\r
+               {\r
+                       ToolStripMenuItem item = (ToolStripMenuItem) sender;\r
+                       string cmdPath = Environment.GetEnvironmentVariable("comspec");\r
+                       \r
+                       ProcessStartInfo procInfo = new ProcessStartInfo(cmdPath);\r
+                       procInfo.UseShellExecute = false;\r
+                       procInfo.WorkingDirectory = item.Tag.ToString();\r
+                       procInfo.EnvironmentVariables["PATH"] = Environment.ExpandEnvironmentVariables(string.Format("%path%;\"{0}\"",item.Tag.ToString()));\r
+                       Process.Start(procInfo);\r
+               }\r
        }\r
 }\r