OSDN Git Service

.Net4対応
[filenamechenger/filenamechanger.git] / UserControl / UCFileSelectTextBox.xaml.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14
15 namespace UserControls
16 {
17     /// <summary>
18     /// UCFileSelectTextBox.xaml の相互作用ロジック
19     /// </summary>
20     public partial class UCFileSelectTextBox : System.Windows.Controls.UserControl
21     {
22         public UCFileSelectTextBox()
23         {
24             InitializeComponent();
25             textBoxFileName.DragEnter += new DragEventHandler(textBoxFileName_DragEnter);
26
27             textBoxFileName.PreviewDragEnter += new DragEventHandler(textBoxFileName_PreviewDragEnter);
28             textBoxFileName.PreviewDrop += new DragEventHandler(textBoxFileName_PreviewDrop);
29
30             BitmapImage myBitmapImage = new BitmapImage();
31
32             //myBitmapImage.BeginInit();
33             //myBitmapImage.UriSource = new Uri(@"dir.JPG");
34             //myBitmapImage.EndInit();
35
36             //imgFileDirSelecter.Source = myBitmapImage;
37
38         }
39          
40
41         void textBoxFileName_PreviewDrop(object sender, DragEventArgs e)
42         {
43             //throw new NotImplementedException();
44             textBox_Drop(sender, e);
45         }
46
47         void textBoxFileName_PreviewDragEnter(object sender, DragEventArgs e)
48         {
49             //throw new NotImplementedException();
50             textBox_DragEnter(sender, e);
51         }
52
53         void textBoxFileName_DragEnter(object sender, DragEventArgs e)
54         {
55             //throw new NotImplementedException();
56             textBox_DragEnter(sender, e);
57         }
58
59         public string Text
60         {
61             get
62             {
63                 return textBoxFileName.Text;
64             }
65
66             set
67             {
68                 textBoxFileName.Text = value;
69             }
70         }
71
72         private void button_Click(object sender, RoutedEventArgs e)
73         {
74             //TODO:フォルダー選択かっこ悪いのでカスタムを作成したい
75             System.Windows.Forms.FolderBrowserDialog fbd =
76                 new System.Windows.Forms.FolderBrowserDialog();
77
78             fbd.SelectedPath = textBoxFileName.Text;
79             System.Windows.Forms.DialogResult result = fbd.ShowDialog();
80             
81             if (result == System.Windows.Forms.DialogResult.OK)
82             {
83                 if (sender == buttonFileDirSelecter)
84                 {
85                     textBoxFileName.Text = fbd.SelectedPath;
86                 }
87             }
88         }
89
90         private void textBox_MouseEnter(object sender, MouseEventArgs e)
91         {
92             TextBox textBox = (TextBox)sender;
93             if (textBox.Text == "" || textBox.Text == null)
94             {
95                 textBox.ToolTip = null;
96             }
97             else
98             {
99                 textBox.ToolTip = textBox.Text;
100             }
101         }
102
103         private void textBox_Drop(object sender, DragEventArgs e)
104         {
105             //TODO:ドラッグアンドドロップ対応したのだが動かないので調査が必要
106             /*
107             if (e.Effects == System.Windows.DragDropEffects.Copy)
108             {
109                 TextBox textBox = (TextBox)sender;
110
111                 textBox.Text = e.Data.GetData(System.Windows.DataFormats.FileDrop).ToString();
112             }
113             */
114             // ファイルドロップした場合のみ処理
115             if (e.Data.GetDataPresent(DataFormats.FileDrop))
116             {
117                 foreach (string fileName in (string[])e.Data.GetData(DataFormats.FileDrop))
118                 {
119                     TextBox textBox = (TextBox)sender;
120                     textBox.Text = fileName;
121
122                     break;
123                 }
124             }
125
126         }
127
128         private void textBox_DragEnter(object sender, DragEventArgs e)
129         {
130             //textBoxFrom.Text = "test";
131             if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop)
132                 || e.Data.GetDataPresent(System.Windows.DataFormats.Text)
133                 || e.Data.GetDataPresent(System.Windows.DataFormats.UnicodeText))
134             {
135                e.Effects = System.Windows.DragDropEffects.Copy;
136             }
137         }
138     }
139 }