実際に自作フォトビューアーを使ったのですが、TreeNodeにファイルパスを表示させた場合、深い階層にある同じフォルダのファイルを登録した場合、最初のほうは全部同じなので見分けが付きにくいです。そこでファイル名はフルパスとファイル名だけから選べるようにしてみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
public partial class Form1 : Form { public Form1() { // コンストラクタ内に以下を追加 checkBoxIsFullPath.Checked = IsNodeTextFullPath; } private void checkBoxIsFullPath_CheckedChanged(object sender, EventArgs e) { IsNodeTextFullPath = checkBoxIsFullPath.Checked; } bool _isNodeTextFullPath = false; bool IsNodeTextFullPath { set { _isNodeTextFullPath = value; foreach (TreeNode node in treeView1.Nodes) { Data data = (Data)node.Tag; SetNodeText(node, data.FilePath); } } get { return _isNodeTextFullPath; } } void SetNodeText(TreeNode node, string filePath) { if (IsNodeTextFullPath) { node.Text = filePath; } else { FileInfo info = new FileInfo(filePath); node.Text = info.Name; } } private void TreeView1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { TreeView1_DragDropFles(e); } if (e.Data.GetDataPresent(typeof(TreeNode))) { Point point = treeView1.PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = treeView1.GetNodeAt(point); int targetIndex = treeView1.Nodes.IndexOf(targetNode); TreeNode sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); treeView1.BeginUpdate(); treeView1.Nodes.Remove(sourceNode); treeView1.Nodes.Insert(targetIndex, sourceNode); treeView1.SelectedNode = sourceNode; treeView1.EndUpdate(); } } void TreeView1_DragDropFles(DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); Point point = treeView1.PointToClient(new Point(e.X, e.Y)); if (treeView1.SelectedNode == null) { files = files.OrderBy(x => x).ToArray(); treeView1.BeginUpdate(); foreach (var path in files) { if (IsImageFile(path)) { TreeNode node = new TreeNode(); SetNodeText(node, path); // 変更 node.Tag = new Data(path, false); treeView1.Nodes.Add(node); } } treeView1.EndUpdate(); } else { int index = treeView1.Nodes.IndexOf(treeView1.SelectedNode); files = files.OrderByDescending(x => x).ToArray(); treeView1.BeginUpdate(); foreach (var path in files) { if (IsImageFile(path)) { TreeNode node = new TreeNode(); SetNodeText(node, path); // 変更 node.Tag = new Data(path, false); treeView1.Nodes.Insert(index + 1, node); } } treeView1.EndUpdate(); } } private void MenuItemThumbnail_Click(object sender, EventArgs e) { TreeNode oldNode = treeView1.SelectedNode; Data oldData = null; if (oldNode != null) { Data data = (Data)oldNode.Tag; oldData = data; data.Comment = textBox1.Text; treeView1.SelectedNode = null; } List<Data> datas = new List<Data>(); foreach (TreeNode node in treeView1.Nodes) datas.Add((Data)node.Tag); FormThumbnail form2 = new FormThumbnail(); form2.Datas = datas; form2.ShowDialog(); treeView1.BeginUpdate(); treeView1.Nodes.Clear(); TreeNode newNode = null; foreach (Data data in form2.Datas) { TreeNode node = new TreeNode(); SetNodeText(node, data.FilePath); // 変更 node.Checked = data.IsChecked; node.Tag = data; treeView1.Nodes.Add(node); if (oldData != null && oldData == data) newNode = node; } treeView1.EndUpdate(); treeView1.SelectedNode = newNode; } void OpenFile(string filePath) { if (!File.Exists(filePath)) return; XmlSerializer xml = new XmlSerializer(typeof(Doc)); StreamReader sr = new StreamReader(filePath); Doc doc = (Doc)xml.Deserialize(sr); sr.Close(); treeView1.BeginUpdate(); treeView1.Nodes.Clear(); foreach (Data data in doc.Datas) { TreeNode node = new TreeNode(); node.Tag = data; SetNodeText(node, data.FilePath); // 変更 node.Checked = data.IsChecked; treeView1.Nodes.Add(node); } treeView1.EndUpdate(); if (treeView1.Nodes.Count > 0) treeView1.SelectedNode = treeView1.Nodes[0]; else { treeView1.SelectedNode = null; pictureBox1.Image = null; textBox1.Text = null; } } } |
それからサムネイルを表示するときも同じように
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public partial class UserControlEx : UserControl { public void SetText(string path, bool isTextFullPath) { if (isTextFullPath) textBox1.Text = path; else { System.IO.FileInfo info = new System.IO.FileInfo(path); textBox1.Text = info.Name; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public partial class FormThumbnail : Form { public FormThumbnail() { // コンストラクタ内に以下を追加 checkBoxIsFullPath.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; checkBoxIsFullPath.Checked = IsItemTextFullPath; checkBoxIsFullPath.CheckedChanged += CheckBoxIsFullPath_CheckedChanged; } private void CheckBoxIsFullPath_CheckedChanged(object sender, EventArgs e) { IsItemTextFullPath = checkBoxIsFullPath.Checked; } bool _isItemTextFullPath = true; // 最初にtrueにしておけば他を書き換える必要がない bool IsItemTextFullPath { get { return _isItemTextFullPath; } set { _isItemTextFullPath = value; foreach (UserControlEx ctrl in userControlIces) { Data data = (Data)ctrl.Tag; ctrl.SetText(data.FilePath, value); } } } } |