-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
297 lines (271 loc) · 12.2 KB
/
MainWindow.xaml.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections;
namespace BiuBiuClick
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow
{
private DirectoryInfo ButtonsFolder;
private List<String> buttonImages;
private List<ListViewItem> items;
static String DEFAULT_CONFIG_DIR = "app/config";
//static String CLICK_APP_PATH = "app/click.exe";
private KeyController controller;
public MainWindow()
{
InitializeComponent();
this.listbox_Menu.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(MenuListBoxItem_MouseDown), true);
this.button_list.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(button_list_MouseDown), true);
if (System.Windows.Forms.SystemInformation.MonitorCount < 2)
{
button_align_2.IsEnabled = false;
}
this.ButtonsFolder = new DirectoryInfo(DEFAULT_CONFIG_DIR);
this.buttonImages = new List<String>();
this.items = new List<ListViewItem>();
this.controller = new KeyController(this.Dispatcher);
loadButtonConfig();
}
private void loadButtonConfig()
{
String selectedConfig = comboBox.SelectedItem == null ? null : comboBox.SelectedItem.ToString();
int selectedIndex = -1;
List<String> config = new List<String>();
try
{
foreach (DirectoryInfo NextDir in this.ButtonsFolder.GetDirectories())
{
config.Add(NextDir.Name);
if (NextDir.Name.Equals(selectedConfig))
{
selectedIndex = comboBox.SelectedIndex;
}
}
}
catch (Exception e)
{
MessageBox.Show("加载按钮配置出现错误:" + e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
comboBox.ItemsSource = config;
if (config.Count > 0)
{
comboBox.SelectedIndex = selectedIndex > -1 ? selectedIndex : 0;
}
}
private void loadButtons(String configName)
{
if (null != configName)
{
try
{
this.buttonImages.Clear();
this.items.Clear();
this.button_list.ItemsSource = this.items;
this.button_list.Items.Refresh();
foreach (FileInfo NextFile in new DirectoryInfo(DEFAULT_CONFIG_DIR + "/" + configName).GetFiles())
{
if (NextFile.Extension.ToLower().Equals(".png"))
{
System.Drawing.Image button_image = System.Drawing.Image.FromFile(NextFile.FullName);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(button_image);
IntPtr hBitmap = bitmap.GetHbitmap();
Image image = new Image();
ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image.Source = wpfBitmap;
ListViewItem item = new ListViewItem();
//item.Width = button_image.Width;
item.Height = button_image.Height;
item.Background = Brushes.LightGray;
item.Content = image;
this.items.Add(item);
this.buttonImages.Add(NextFile.FullName);
}
}
this.button_list.ItemsSource = this.items;
this.button_list.Items.Refresh();
}
catch (Exception e)
{
MessageBox.Show("加载按钮图片出现错误:" + e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}
private void refresh_click(object sender, RoutedEventArgs e)
{
loadButtonConfig();
loadButtons(comboBox.SelectedItem == null ? null : comboBox.SelectedItem.ToString());
}
private struct Config
{
internal string filePath;
internal string className;
internal string macthClassNameFromRight;
internal string processName;
internal Dictionary<string, string> keys;
internal Config init()
{
keys = new Dictionary<string, string>();
return this;
}
}
private Config readConfig()
{
string iniPath = System.Environment.CurrentDirectory + "/" + DEFAULT_CONFIG_DIR + "/" + comboBox.SelectedItem.ToString() + "/config.ini";
IniFile iniFile = new IniFile();
iniFile.Load(iniPath);
Config config = new Config().init();
config.filePath = iniPath;
config.className = iniFile["common"]["className"].Value;
config.macthClassNameFromRight = iniFile["common"]["macthClassNameFromRight"].Value;
config.processName = iniFile["common"]["processName"].Value;
List<KeyValuePair<string, IniValue>> pairs = iniFile["keys"].ToList();
foreach (KeyValuePair<string, IniValue> pair in pairs)
{
config.keys.Add(pair.Key, pair.Value.ToString());
}
return config;
}
private void DoClick(String buttonImagePath)
{
try
{
string buttonImageName = System.IO.Path.GetFileNameWithoutExtension(buttonImagePath);
string[] nameParts = buttonImageName.Split('-');
if (nameParts.Length != 2)
{
MessageBox.Show("invalid button image name: '" + buttonImageName + "' at " + buttonImagePath, "图片名称不符合规范", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
Config config = readConfig();
string buttonKey = nameParts[1];
if (config.keys.Keys.Contains(buttonKey))
{
string key = config.keys[buttonKey];
if (this.controller.sendKeyToProcessAll(config.processName, key) == 0)
{
MessageBox.Show("未找到名为 '" + config.processName + "' 的进程,请检查播放器是否已开启,或是否选对配置", "要操作的进程不存在", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else {
MessageBox.Show("button key: '" + buttonKey + "' not found in " + config.filePath, "按钮未在配置文件中定义", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/*Process proc = Process.Start(System.Environment.CurrentDirectory + "/" + CLICK_APP_PATH, buttonImagePath);
if (proc != null)
{
proc.WaitForExit(5000);
if (!proc.HasExited)
{
// 如果外部程序没有结束运行则强行终止之。
proc.Kill();
//MessageBox.Show(String.Format("程序 {0} 运行时间太长被强行终止!", CLICK_APP_PATH), "执行点击按钮出现错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}*/
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, "执行点击按钮出现错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void align_button_Click(object sender, RoutedEventArgs e)
{
if (comboBox.SelectedItem != null)
{
try
{
Config config = readConfig();
Button btn = (Button)sender;
WindowHelper.AlignWindow(config.className, (WindowHelper.MoveMode)Enum.Parse(typeof(WindowHelper.MoveMode), btn.CommandParameter.ToString()));
}
catch (Exception e1)
{
MessageBox.Show("运行对齐窗口程序出现错误:" + e1.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
loadButtons(comboBox.SelectedItem == null ? null : comboBox.SelectedItem.ToString());
}
private void button_list_MouseDown(object sender, MouseButtonEventArgs e)
{
String buttonImage = this.buttonImages[button_list.SelectedIndex];
var background = this.items[button_list.SelectedIndex].Background;
this.items[button_list.SelectedIndex].Background = Brushes.LightBlue;
button_list.Items.Refresh();
DoClick(buttonImage);
this.items[this.button_list.SelectedIndex].Background = background;
button_list.Items.Refresh();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void button_WindowMinimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Minimized;
}
private void button_WindowClose_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
private void MenuListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
{
string content = (listbox_Menu.SelectedValue as ListBoxItem).Content.ToString();
switch (content)
{
case "退出":
Environment.Exit(Environment.ExitCode);
break;
case "窗口信息工具":
{
WindowInfoTool window = new WindowInfoTool();
window.Show();
window.Activate();
}
break;
case "关于":
{
MessageBox.Show("BiubiuClick V0.1.0 \n" +
"BiubiuClick是一个用于在对比两个视频时控制同步播放的工具,功能如下:\n " +
"+ 一键控制两个播放器同时播放、暂停或停止 \n " +
"+ 一键将屏幕平分展示两个窗口 \n " +
"+ 一键将两个窗口移动到两个屏幕上,并且全屏",
"关于", MessageBoxButton.OK, MessageBoxImage.Information);
}
break;
case "帮助":
{
if (MessageBox.Show("即将打开默认浏览器访问项目主页查看帮助,确认继续吗?", "帮助", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
Process.Start("https://github.com/huhai463127310/BiuBiuClick");
}
}
break;
}
}
}
}