public void Launcher_Load() { Console.WriteLine("Load_Start"); //config.xmlが存在しない場合新規作成 string configXML = @"config.xml"; if (!File.Exists(configXML)) { CreateConfigXML(); } //toolフォルダーが存在しない場合新規作成 string toolFolder = @"tool"; if (!Directory.Exists(toolFolder)) { CreateToolFolder(); } try { //tabタグをid順にソート XDocument doc = XDocument.Load(@"config.xml"); var baseElement = doc.XPathSelectElement("tabs"); var sortedElements = baseElement.Elements() .OrderBy(e => (int)e.Attribute("id")) .ToList(); baseElement.ReplaceAll(sortedElements); //appタグをid順にソート var baseElements = doc.XPathSelectElements("tabs/tab"); foreach (XElement el in baseElements) { var sortedElement = el.Elements() .OrderBy(e => (int)e.Attribute("id")) .ToList(); el.ReplaceNodes(sortedElement); } doc.Save(@"config.xml"); //タブ情報ファイルの読み込み var xmlDoc = new XmlDocument(); xmlDoc.Load(@"config.xml"); var tabs = xmlDoc.SelectNodes("tabs/tab"); //tabタグ情報 //tabタグのidをソート順に再割り振り for (var i = 0; i < tabs.Count; i++) { tabs[i].Attributes[0].Value = i.ToString(); var apps = xmlDoc.SelectNodes("tabs/tab[@id='" + i + "']/app"); //appタグ情報 //appタグのidをソート順に再割り振り for (var j = 0; j < apps.Count; j++) { apps[j].Attributes[0].Value = j.ToString(); } } xmlDoc.Save(@"config.xml"); //tabタグの数分タブコントロールにタブページ追加 for (var i = 0; i < tabs.Count; i++) { var tabname = tabs[i].SelectSingleNode("tabname").InnerText; //tabタグの名前 var apps = xmlDoc.SelectNodes("tabs/tab[@id='" + i + "']/app"); //appタグ情報 //タブページの設定 TabPage tabPage = new TabPage(); tabPage.Name = i.ToString(); tabPage.Text = tabname; //タブコントロールにタブページ追加 tabEx.TabPages.Add(tabPage); //appタグの数分タブページにアプリケーション表示用パネルとラベル追加 for (var j = 0; j < apps.Count; j++) { var appPath = apps[j].SelectSingleNode("path").InnerText; //アプリケーションのパス var appName = apps[j].SelectSingleNode("name").InnerText; //アプリケーションの名前 //アプリケーション表示用パネルの設定 PanelEx panelEx = new PanelEx(); panelEx.Location = new Point(25 + (j % 6) * 100, 25 + (j / 6) * 100); panelEx.Size = new Size(50, 50); panelEx.Name = i.ToString() + "_p_" + j.ToString(); panelEx.LoadPath(appPath, appName); panelEx.ContextMenuStrip = appContextMenuStrip; //アプリケーションのラベル設定 Label label = new Label(); label.Name = i.ToString() + "_l_" + j.ToString(); if (appName.Length > appNameMax) { label.Text = appName.Substring(0, appNameMax); } else { label.Text = appName; } label.TextAlign = ContentAlignment.MiddleCenter; label.AutoSize = false; label.Size = new Size(85, 25); label.Location = new Point(8 + (j % 6) * 100, 78 + (j / 6) * 100); //アプリケーションのテキストボックス設定 TextBox textBox = new TextBox(); textBox.Location = new Point(8 + (j % 6) * 100, 78 + (j / 6) * 100); textBox.Size = new Size(85, 50); textBox.Name = i.ToString() + "_t_" + j.ToString(); if (appName.Length > appNameMax) { textBox.Text = appName.Substring(0, appNameMax); } else { textBox.Text = appName; } textBox.Visible = false; textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress); //textBox.Leave += Leave_TextBox; tabPage.Controls.Add(label); tabPage.Controls.Add(textBox); tabPage.Controls.Add(panelEx); if (j > appMax) { break; } } if (i > tabMax - 1) { break; } } Controls.Add(tabEx); } catch (System.IO.FileNotFoundException ex) { //FileNotFoundExceptionをキャッチした時 System.Console.WriteLine("設定ファイルが見つかりませんでした。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルが見つかりませんでした。"); } catch (System.IO.IOException ex) { //IOExceptionをキャッチした時 System.Console.WriteLine("設定ファイルがロックされている可能性があります。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルがロックされている可能性があります。"); } catch (System.UnauthorizedAccessException ex) { //UnauthorizedAccessExceptionをキャッチした時 System.Console.WriteLine("設定ファイルのアクセス許可がありません。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルのアクセス許可がありません。"); } catch (Exception ex) { MessageBox.Show("アプリケーションを実行できません。"); MessageBox.Show(ex.Message); } }
//アプリケーションのドラッグアンドドロップ public void AppDragDrop(object sender, DragEventArgs e) { Console.WriteLine("AppDragDrop_Start"); try { TabEx tabEx = sender as TabEx; //ファイルのドラッグアンドドロップ時 if (e.Data.GetDataPresent(DataFormats.FileDrop)) { //ファイルの絶対パスと名前の取得 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); var appPath = files[0]; char[] separator = new char[] { '\\' }; string[] arr = appPath.Split(separator); string appName; if (arr[arr.Length - 1].Length > appNameMax) { appName = arr[arr.Length - 1].Substring(0, appNameMax); } else { appName = arr[arr.Length - 1]; } //タブ情報ファイルの読み込み var xmlDoc = new XmlDocument(); xmlDoc.Load(@"config.xml"); var tabNodes = xmlDoc.SelectNodes("tabs/tab"); //タブ情報ファイルの更新とアプリケーション表示用パネルの設置 for (var i = 0; i < tabNodes.Count; i++) { var tabId = ((XmlElement)tabNodes[i]).GetAttribute("id"); //tabタグのid //現在選択中のタブとtabタグのidが一致した場合 if (tabEx.SelectedTab.Name == tabId) { var appNodes = xmlDoc.SelectNodes("tabs/tab[@id='" + i + "']/app"); //appタグ情報 var appId = appNodes.Count; //登録されているアプリケーションが最大値未満の場合 if (appId < appMax) { //タブ情報ファイルの更新 XmlNode rootNode = xmlDoc.SelectSingleNode("tabs/tab[@id='" + i + "']"); XmlNode newAppNode = xmlDoc.CreateNode(XmlNodeType.Element, "app", null); ((XmlElement)newAppNode).SetAttribute("id", appId.ToString()); XmlElement pathElement = xmlDoc.CreateElement("path"); XmlElement nameElement = xmlDoc.CreateElement("name"); XmlText pathText = xmlDoc.CreateTextNode(appPath); //相対パスの登録 var curPath = System.Windows.Forms.Application.StartupPath; if (appPath.Contains(curPath + "\\tool\\")) { string relPath = null; string[] arrCurPath = curPath.Split(separator); for (int curPathLen = arrCurPath.Length; curPathLen < arr.Length; curPathLen++) { Console.WriteLine(arr[curPathLen]); relPath += "\\" + arr[curPathLen]; } Console.Write(relPath); appPath = relPath; pathText = xmlDoc.CreateTextNode(relPath); } XmlText nameText = xmlDoc.CreateTextNode(appName); pathElement.AppendChild(pathText); nameElement.AppendChild(nameText); newAppNode.AppendChild(pathElement); newAppNode.AppendChild(nameElement); rootNode.AppendChild(newAppNode); xmlDoc.Save(@"config.xml"); //アプリケーション表示用パネルの設置 PanelEx panelEx = new PanelEx(); panelEx.Location = new Point(25 + (appId % 6) * 100, 25 + (appId / 6) * 100); panelEx.Size = new Size(50, 50); panelEx.Name = i.ToString() + "_p_" + appId.ToString(); panelEx.SetPath(appPath, appName); panelEx.ContextMenuStrip = appContextMenuStrip; //アプリケーションのラベル設定 Label label = new Label(); label.Location = new Point(8 + (appId % 6) * 100, 78 + (appId / 6) * 100); label.Size = new Size(85, 25); label.Name = i.ToString() + "_l_" + appId.ToString(); if (appName.Length > appNameMax) { label.Text = appName.Substring(0, appNameMax); } else { label.Text = appName; } label.TextAlign = ContentAlignment.MiddleCenter; label.AutoSize = false; //アプリケーションのテキストボックス設定 TextBox textBox = new TextBox(); textBox.Location = new Point(8 + (appId % 6) * 100, 78 + (appId / 6) * 100); textBox.Size = new Size(85, 50); textBox.Name = i.ToString() + "_t_" + appId.ToString(); if (appName.Length > appNameMax) { textBox.Text = appName.Substring(0, appNameMax); } else { textBox.Text = appName; } textBox.Visible = false; textBox.KeyPress += new KeyPressEventHandler(textBox_KeyPress); tabEx.SelectedTab.Controls.Add(panelEx); tabEx.SelectedTab.Controls.Add(label); tabEx.SelectedTab.Controls.Add(textBox); } else { MessageBox.Show("アプリケーションをこれ以上登録できません。"); } } } } } catch (System.IO.FileNotFoundException ex) { //FileNotFoundExceptionをキャッチした時 System.Console.WriteLine("設定ファイルが見つかりませんでした。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルが見つかりませんでした。"); } catch (System.IO.IOException ex) { //IOExceptionをキャッチした時 System.Console.WriteLine("設定ファイルがロックされている可能性があります。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルがロックされている可能性があります。"); } catch (System.UnauthorizedAccessException ex) { //UnauthorizedAccessExceptionをキャッチした時 System.Console.WriteLine("設定ファイルのアクセス許可がありません。"); System.Console.WriteLine(ex.Message); MessageBox.Show("設定ファイルのアクセス許可がありません。"); } catch (Exception ex) { MessageBox.Show("アプリケーションを実行できません。"); MessageBox.Show(ex.Message); } }