OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmGenPreview.cs
1 using System;\r
2 using System.Windows.Forms;\r
3 using System.Threading;\r
4 using System.Diagnostics;\r
5 using System.IO;\r
6 \r
7 namespace Handbrake\r
8 {\r
9     public partial class frmGenPreview : Form\r
10     {\r
11         private delegate void UpdateHandler();\r
12         QueryGenerator queryGen = new QueryGenerator();\r
13         Functions.Encode process = new Functions.Encode();\r
14         Process hbProc;\r
15         frmMain mainWindow;\r
16 \r
17         public frmGenPreview(frmMain mw)\r
18         {\r
19             InitializeComponent();\r
20             this.mainWindow = mw;\r
21             cb_duration.SelectedIndex = 0;\r
22             cb_preview.SelectedIndex = 0;\r
23         }\r
24 \r
25         private void btn_play_Click(object sender, EventArgs e)\r
26         {\r
27             // Get the Destination of the sample video.\r
28              String currently_playing = "";\r
29             if (mainWindow.text_destination.Text != "")\r
30                 currently_playing = mainWindow.text_destination.Text.Replace(".m", "_sample.m").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm");\r
31 \r
32             // Launch VLC and play video.\r
33             if (currently_playing != "")\r
34             {\r
35                 if (File.Exists(currently_playing))\r
36                 {\r
37                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
38                     {\r
39                         String args = "\"" + currently_playing + "\"";\r
40                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
41                         Process.Start(vlc);\r
42                         lbl_status.Text = "VLC will now launch.";\r
43                     }\r
44                     else\r
45                         MessageBox.Show("Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in the program options is correct.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
46                 } else\r
47                     MessageBox.Show("Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
48             }\r
49         }\r
50 \r
51         private void btn_encode_Click(object sender, EventArgs e)\r
52         {\r
53             String query = queryGen.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
54             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
55         }\r
56         private void procMonitor(object state)\r
57         {\r
58             // Make sure we are not already encoding and if we are then display an error.\r
59             if (hbProc != null)\r
60                 MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
61             else\r
62             {\r
63                 encodingMessage();\r
64                 hbProc = process.runCli(this, (string)state);\r
65                 hbProc.WaitForExit();\r
66                 hbProc = null;\r
67                 updateUIElements();\r
68             }\r
69         }\r
70         // Update the UI now that the encode has finished.\r
71         private void encodingMessage()\r
72         {\r
73             try\r
74             {\r
75                 if (InvokeRequired)\r
76                 {\r
77                     BeginInvoke(new UpdateHandler(encodingMessage));\r
78                     return;\r
79                 }\r
80                 lbl_status.Text = "Encoding, Please wait ...";\r
81             }\r
82             catch (Exception exc)\r
83             {\r
84                 MessageBox.Show(exc.ToString());\r
85             }\r
86         }\r
87 \r
88         // Update the UI now that the encode has finished.\r
89         private void updateUIElements()\r
90         {\r
91             try\r
92             {\r
93                 if (InvokeRequired)\r
94                 {\r
95                     BeginInvoke(new UpdateHandler(updateUIElements));\r
96                     return;\r
97                 }\r
98 \r
99                 btn_play.Visible = true;\r
100                 toolStripSeparator1.Visible = true;\r
101                 lbl_status.Text = "Your sample is ready to play.";\r
102             }\r
103             catch (Exception exc)\r
104             {\r
105                 MessageBox.Show(exc.ToString());\r
106             }\r
107         }\r
108 \r
109 \r
110     }\r
111 }\r