-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
211 lines (203 loc) · 7.49 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
using Microsoft.Extensions.DependencyInjection;
using MoqWord.Helpers;
using MoqWord.Repository.Interface;
using MoqWord.Services;
using MoqWord.Utils;
using MoqWord.WpfComponents.Page;
using SqlSugar;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MoqWord
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ISqlSugarClient sqlSugar { get; set; }
public ISettingRepository settingRepository { get; set; }
public IShortcutKeysService shortcutKeysService { get; set; }
public IPopupConfigService popupConfigService { get; set; }
public MainWindow()
{
InitializeComponent();
InitHwnd();
Resources.Add("services", ServiceHelper.getService());
// 注册窗口事件类
WindowHelper.Init();
}
public MainWindow(ISqlSugarClient _sqlSugar, ISettingRepository _settingRepository, IShortcutKeysService _shortcutKeysService, IPopupConfigService popupConfigService) : this()
{
sqlSugar = _sqlSugar;
settingRepository = _settingRepository;
shortcutKeysService = _shortcutKeysService;
this.popupConfigService = popupConfigService;
// 初始化数据库
init();
//
NotifyIconHelper.Icon();
// 注册热键
var handle = new WindowInteropHelper(this).Handle;
ShortcutKeyHelper.Register(handle);
}
private void InitHwnd()
{
var helper = new WindowInteropHelper(this);
helper.EnsureHandle();
}
private void init()
{
if (sqlSugar.DbMaintenance.CreateDatabase())
{
sqlSugar.CodeFirst.InitTables(Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetCustomAttributes<SugarTable>().Any()).ToArray());
}
else
{
MessageBox.Show("创建失败");
}
// 初始化setting配置
var firstSetting = settingRepository.GetSingle(x => x.Id >= 0);
if (firstSetting is null or default(Setting))
{
settingRepository.Insert(new Setting
{
DarkNess = false,
UsingSound = false,
EverDayCount = 20,
RepeatCount = RepeatType.Three,
TimeInterval = 365,
DesiredRetension = 0.9,
Difficulty = 0.5,
SoundName = "",
SoundSource = Sound.Default,
SecondSoundName = "2",
SecondSoundSource = Sound.Youdao,
SpeechSpeed = 0,
SoundVolume = 100,
SuggestedCodec = "",
StartWithWindows = false,
SelectionInterval = 400
});
}
// 初始化快捷键
var firstShortKeys = shortcutKeysService.First();
if (firstShortKeys is null or default(ShortcutKeys))
{
shortcutKeysService.InsertList(new List<ShortcutKeys>
{
new ShortcutKeys
{
Name = "打开/关闭单词",
ShortcutName = "Ctrl,Alt,D",
Key = 68,
Modifiers = NativeMethod.KeyModifiers.Ctrl | NativeMethod.KeyModifiers.Alt
},
new ShortcutKeys
{
Name = "开始/停止播放",
ShortcutName = "Ctrl,Alt,P",
Key = 80,
Interface = "IPlayService",
Method = "Collapse",
Modifiers = NativeMethod.KeyModifiers.Ctrl | NativeMethod.KeyModifiers.Alt
},
new ShortcutKeys
{
Name = "上一个单词",
ShortcutName = "Ctrl,Alt,Left",
Key = 37,
Interface = "IPlayService",
Method = "Previous",
Modifiers = NativeMethod.KeyModifiers.Ctrl | NativeMethod.KeyModifiers.Alt
},
new ShortcutKeys
{
Name = "下一个单词",
ShortcutName = "Ctrl,Alt,Right",
Key = 39,
Interface = "IPlayService",
Method = "Next",
Modifiers = NativeMethod.KeyModifiers.Ctrl | NativeMethod.KeyModifiers.Alt
},
new ShortcutKeys
{
Name = "划词翻译",
ShortcutName = "Ctrl,Space",
Key = 32,
Modifiers = NativeMethod.KeyModifiers.Ctrl | NativeMethod.KeyModifiers.Alt
}
});
}
// 初始化窗口样式
var firstPopupConfig = popupConfigService.First();
if (firstPopupConfig is null or default(PopupConfig))
{
popupConfigService.InsertOrUpdate(new PopupConfig()
{
Background = "#B6EEEEEE",
Color = "#FFCC3737",
TranslationFontSize = 14.5,
WordNameFontSize = 32.5,
Opacity = 1,
IsLock = false,
IsPenetrate = false,
CreateDT = DateTime.Now,
UpdateDT = DateTime.Now,
});
}
}
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var handle = new WindowInteropHelper(this).Handle;
var source = HwndSource.FromHwnd(handle);
source?.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle)
{
if (msg != 0x0312)
{
return IntPtr.Zero;
}
var play = ServiceHelper.getService().GetService<IPlayService>();
switch (wParam)
{
case 10087:
NotifyIconHelper.Show();
break;
case 10088:
play?.Collapse();
break;
case 10089:
play?.Previous();
break;
case 10090:
play?.Next();
break;
case 10091:
NotifyIconHelper.ShowHoverTranslate();
break;
}
return IntPtr.Zero;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}
}
}