OSDN Git Service

Visual Studio 2019対応
[filenamechenger/filenamechanger.git] / FileNameChenger / WorkView.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.Media.Animation;
13 using System.Windows.Shapes;
14 using System.Threading;
15 using System.IO;
16
17
18
19 namespace FileNameChenger
20 {
21     /// <summary>
22     /// WorkView.xaml の相互作用ロジック
23     /// </summary>
24     public partial class WorkView : Window
25     {
26
27         //Timer stateTimer = null;
28         System.Windows.Forms.Timer stateTimer = null;
29         public WorkView()
30         {
31             InitializeComponent();
32 //            Run();
33
34             //this.Content = FileNameChenger.Properties.Resources.LineageII.darkelf;
35             //BitmapImage myBitmapImage = new BitmapImage();
36             //myBitmapImage.StreamSource = FileNameChenger.Properties.Resources.LineageII.darkelf.;
37
38             string[] files = Directory.GetFiles(@".\img", "*.jpg", SearchOption.AllDirectories);
39             
40
41             BitmapImage myBitmapImage = new BitmapImage();
42
43             if (files.Length == 0)
44             {
45                 myBitmapImage.BeginInit();
46                 myBitmapImage.UriSource = new Uri(@"FileNameChenger.ico", UriKind.RelativeOrAbsolute);
47                 myBitmapImage.EndInit();
48             }
49             else
50             {
51                 Random r = new Random();
52                 int index = r.Next(files.Length - 1);
53                 myBitmapImage.BeginInit();
54                 myBitmapImage.UriSource = new Uri(@"file://" + Directory.GetCurrentDirectory() + files[index], UriKind.RelativeOrAbsolute);
55                 myBitmapImage.EndInit();
56             }
57             
58             imageWork.Source = myBitmapImage;
59
60
61             //imagStoryboard.Storyboard.Stop();
62             progressBarWork.Minimum = 0;
63             progressBarWork.Maximum = 100;
64
65             /*
66             AutoResetEvent autoEvent     = new AutoResetEvent(false);
67
68                  // Create the delegate that invokes methods for the timer.
69             TimerCallback timerDelegate = 
70                 new TimerCallback(CheckStatus);
71
72
73             stateTimer =
74                      new Timer(timerDelegate, autoEvent, 1000, 250);
75              */
76             stateTimer = new System.Windows.Forms.Timer();
77             stateTimer.Interval = 1000;
78             stateTimer.Tick += new EventHandler(stateTimer_Tick);
79             stateTimer.Start();
80         }
81
82         /*
83         public override ~WorkView()
84         {
85             if (stateTimer != null)
86             {
87                 stateTimer.Dispose();
88             }
89         }
90          */
91
92         void stateTimer_Tick(object sender, EventArgs e)
93         {
94             //throw new NotImplementedException();
95             lock (workingImage)
96             {
97                 workingImage.Angle = angle;
98             }
99
100             lock (progressBarWork)
101             {
102                 progressBarWork.Value = angle;
103             }
104         }
105         /*
106         public void CheckStatus(Object stateInfo)
107         {
108             lock (workingImage)
109             {
110                 workingImage.Angle = angle;
111             }
112
113             lock (progressBarWork)
114             {
115                 progressBarWork.Value = angle;
116             }
117         }
118          */
119
120         private void Run()
121         {
122             DoWorkingEvent += new EventHandler<EventArgs>(WorkView_DoWorkingEvent);
123
124             
125             ThreadStart threadDelegate = new ThreadStart(DoWork);
126             Thread newThread = new Thread(threadDelegate);
127             newThread.Start();
128             
129             //DoWork();
130
131             /*四角アニメーション
132             NameScope.SetNameScope(this, new NameScope());
133
134             this.Title = "Fading Rectangle Example";
135             StackPanel myPanel = new StackPanel();
136             myPanel.Margin = new Thickness(10);
137
138             Rectangle myRectangle = new Rectangle();
139             myRectangle.Name = "myRectangle";
140             this.RegisterName(myRectangle.Name, myRectangle);
141             myRectangle.Width = 100;
142             myRectangle.Height = 100;
143             myRectangle.Fill = Brushes.Blue;
144
145             DoubleAnimation myDoubleAnimation = new DoubleAnimation();
146             myDoubleAnimation.From = 1.0;
147             myDoubleAnimation.To = 0.0;
148             myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
149             myDoubleAnimation.AutoReverse = true;
150             myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
151
152             myStoryboard = new Storyboard();
153             myStoryboard.Children.Add(myDoubleAnimation);
154             Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
155             //Storyboard.SetTargetName(myDoubleAnimation, imageWork.Name);
156             Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty));
157
158             // Use the Loaded event to start the Storyboard.
159             myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded);
160             //imageWork.Loaded += new RoutedEventHandler(myRectangleLoaded);
161
162             myPanel.Children.Add(myRectangle);
163             //myPanel.Children.Add(imageWork);
164             this.Content = myPanel;
165             */
166
167             /* ボタン アニメーション
168             StackPanel myStackPanel = new StackPanel();
169             myStackPanel.Margin = new Thickness(20);
170
171
172             // Create and set the Button.
173             Button aButton = new Button();
174             aButton.Content = "A Button";
175
176             // Animate the Button's Width.
177             DoubleAnimation myDoubleAnimation = new DoubleAnimation();
178             myDoubleAnimation.From = 75;
179             myDoubleAnimation.To = 300;
180             myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
181             myDoubleAnimation.AutoReverse = true;
182             myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
183
184             // Apply the animation to the button's Width property.
185             aButton.BeginAnimation(Button.WidthProperty, myDoubleAnimation);
186
187             // Create and animate a Brush to set the button's Background.
188             SolidColorBrush myBrush = new SolidColorBrush();
189             myBrush.Color = Colors.Blue;
190
191             ColorAnimation myColorAnimation = new ColorAnimation();
192             myColorAnimation.From = Colors.Blue;
193             myColorAnimation.To = Colors.Red;
194             myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(7000));
195             myColorAnimation.AutoReverse = true;
196             myColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
197
198             // Apply the animation to the brush's Color property.
199             myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation);
200             aButton.Background = myBrush;
201
202             // Add the Button to the panel.
203             myStackPanel.Children.Add(aButton);
204             this.Content = myStackPanel;
205             */
206
207
208             //imagStoryboard.Storyboard.Begin(this);
209             //imagStoryboard.Storyboard.Stop(this);
210             //imagStoryboard.Storyboard.Pause(this);
211         }
212
213         /*
214         private Storyboard myStoryboard;
215         private void myRectangleLoaded(object sender, RoutedEventArgs e)
216         {
217             myStoryboard.Begin(this);
218         }
219          */
220
221
222         int angle = 0;
223         void WorkView_DoWorkingEvent(object sender, EventArgs e)
224         {
225             //throw new NotImplementedException();
226             /*
227             lock (workingImage)
228             {
229                 workingImage.Angle++;
230             }
231              */
232             angle++;
233            
234        }
235
236         bool IsCancel = false;
237         private void DoWork() 
238         {
239             while(!IsCancel)
240             {
241                 OnDoWorkingEvent(new EventArgs());
242                 Thread.Sleep(1000);
243             }
244         }
245
246         public event EventHandler<EventArgs> DoWorkingEvent;
247         protected virtual void OnDoWorkingEvent(EventArgs e)
248         {
249             EventHandler<EventArgs> handler = DoWorkingEvent;
250
251             if (handler != null)
252             {
253                 handler(this, e);
254             }
255         }
256
257         private void buttonCancel_Click(object sender, RoutedEventArgs e)
258         {
259             IsCancel = true;
260         }
261
262         private void buttonRun_Click(object sender, RoutedEventArgs e)
263         {
264             Run();
265         }
266
267
268
269         private void imageWork_Loaded(object sender, RoutedEventArgs e)
270         {
271             imagStoryboard.Storyboard.Stop(this);
272         }
273
274         private void buttonStop_Click(object sender, RoutedEventArgs e)
275         {
276             imagStoryboard.Storyboard.Stop(this);
277         }
278     }
279 }
280