fasdfsa commited on
Commit
8cd514d
·
1 Parent(s): 68dd90e

Alt+S 截图

Browse files
src/ScreenGrab/Extensions/ImageExtensions.cs CHANGED
@@ -63,6 +63,11 @@ public static class ImageExtensions
63
 
64
  public static Bitmap GetRegionOfScreenAsBitmap(this Rectangle region)
65
  {
 
 
 
 
 
66
  Bitmap bmp = new(region.Width, region.Height, PixelFormat.Format32bppArgb);
67
  using var g = Graphics.FromImage(bmp);
68
 
 
63
 
64
  public static Bitmap GetRegionOfScreenAsBitmap(this Rectangle region)
65
  {
66
+ if (region.Width <= 1 || region.Height <= 1)
67
+ {
68
+ return null;
69
+ }
70
+
71
  Bitmap bmp = new(region.Width, region.Height, PixelFormat.Format32bppArgb);
72
  using var g = Graphics.FromImage(bmp);
73
 
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -181,16 +181,16 @@ namespace WpfEditor
181
 
182
  Editor.TextChanged += Editor_TextChanged;
183
  Editor.CursorPositionChanged += Editor_CursorPositionChanged;
184
-
185
  // 添加图片尺寸变化监听
186
  image.SizeChanged += Image_SizeChanged;
187
-
188
  // 初始化选择画布
189
  InitializeSelectionCanvas();
190
-
191
  this._mode = mode;
192
  Editor._mode = mode;
193
-
194
 
195
  if (this._mode == EditorMode.ImageOCR)
196
  {
@@ -205,13 +205,19 @@ namespace WpfEditor
205
  this.Title = "目录拆解";
206
  this.BorderDirectoryTreeView.Visibility = Visibility.Visible;
207
  this.DirectoryTreeView.Visibility = Visibility.Visible;
208
-
209
  // 为目录拆解模式添加示例数据
210
  //InitializeDirectoryTreeData();
211
  }
212
 
213
  // 自动选择最佳的编程字体
214
  FontManager.SetProgrammingFont(Editor, 14);
 
 
 
 
 
 
215
  }
216
 
217
 
 
181
 
182
  Editor.TextChanged += Editor_TextChanged;
183
  Editor.CursorPositionChanged += Editor_CursorPositionChanged;
184
+
185
  // 添加图片尺寸变化监听
186
  image.SizeChanged += Image_SizeChanged;
187
+
188
  // 初始化选择画布
189
  InitializeSelectionCanvas();
190
+
191
  this._mode = mode;
192
  Editor._mode = mode;
193
+
194
 
195
  if (this._mode == EditorMode.ImageOCR)
196
  {
 
205
  this.Title = "目录拆解";
206
  this.BorderDirectoryTreeView.Visibility = Visibility.Visible;
207
  this.DirectoryTreeView.Visibility = Visibility.Visible;
208
+
209
  // 为目录拆解模式添加示例数据
210
  //InitializeDirectoryTreeData();
211
  }
212
 
213
  // 自动选择最佳的编程字体
214
  FontManager.SetProgrammingFont(Editor, 14);
215
+
216
+ //GlobalHotKey.RegisterHotKey("Alt + S", () => {
217
+
218
+ // int a = 1;
219
+
220
+ //});
221
  }
222
 
223
 
src/WpfEditor/TreeMenuPage.xaml.cs CHANGED
@@ -1,8 +1,10 @@
 
1
  using System.Windows;
2
  using System.Windows.Controls;
3
  using System.Windows.Input;
4
  using System.Windows.Media;
5
  using System.Windows.Media.Animation;
 
6
 
7
  namespace WpfEditor
8
  {
@@ -19,6 +21,26 @@ namespace WpfEditor
19
 
20
  // 添加点击其他区域隐藏菜单的事件
21
  this.MouseDown += TreeMenuPage_MouseDown;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  }
23
 
24
  private void MenuButton_Click(object sender, RoutedEventArgs e)
 
1
+ using ScreenGrab;
2
  using System.Windows;
3
  using System.Windows.Controls;
4
  using System.Windows.Input;
5
  using System.Windows.Media;
6
  using System.Windows.Media.Animation;
7
+ using Bitmap = System.Drawing.Bitmap;
8
 
9
  namespace WpfEditor
10
  {
 
21
 
22
  // 添加点击其他区域隐藏菜单的事件
23
  this.MouseDown += TreeMenuPage_MouseDown;
24
+
25
+ GlobalHotKey.RegisterHotKey("Alt + S", () => {
26
+
27
+ if (ScreenGrabber.IsCapturing) return;
28
+ // https://github.com/wuqinchao/ScreenCapturer
29
+ // 替代品
30
+ ScreenGrabber.OnCaptured = async bitmap => await ScreenshotCallbackAsync(bitmap, null);
31
+ ScreenGrabber.Capture(true);
32
+
33
+ });
34
+
35
+ }
36
+
37
+ private async Task ScreenshotCallbackAsync(Bitmap? bitmap, CancellationToken? token = null)
38
+ {
39
+ if (bitmap == null)
40
+ {
41
+ //ShowBalloonTip("图像不存在");
42
+ return;
43
+ }
44
  }
45
 
46
  private void MenuButton_Click(object sender, RoutedEventArgs e)
src/WpfEditor/WpfEditor.csproj CHANGED
@@ -24,6 +24,7 @@
24
  </ItemGroup>
25
 
26
  <ItemGroup>
 
27
  <ProjectReference Include="..\WeChatOcrCpp\WeChatOcrCpp.vcxproj" />
28
  </ItemGroup>
29
 
 
24
  </ItemGroup>
25
 
26
  <ItemGroup>
27
+ <ProjectReference Include="..\ScreenGrab\ScreenGrab.csproj" />
28
  <ProjectReference Include="..\WeChatOcrCpp\WeChatOcrCpp.vcxproj" />
29
  </ItemGroup>
30
 
src/WpfEditor/hotkey.cs ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Runtime.InteropServices;
4
+ using System.Windows.Input;
5
+
6
+ namespace WpfEditor
7
+ {
8
+
9
+ public class GlobalHotKey : IDisposable
10
+ {
11
+ /// <summary>
12
+ /// Registers a global hotkey
13
+ /// </summary>
14
+ /// <param name="aKeyGesture">e.g. Alt + Shift + Control + Win + S</param>
15
+ /// <param name="aAction">Action to be called when hotkey is pressed</param>
16
+ /// <returns>true, if registration succeeded, otherwise false</returns>
17
+ public static bool RegisterHotKey(string aKeyGestureString, Action aAction)
18
+ {
19
+ var c = new KeyGestureConverter();
20
+ KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
21
+ return RegisterHotKey(aKeyGesture.Modifiers, aKeyGesture.Key, aAction);
22
+ }
23
+
24
+ public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction)
25
+ {
26
+ if (aModifier == ModifierKeys.None)
27
+ {
28
+ throw new ArgumentException("Modifier must not be ModifierKeys.None");
29
+ }
30
+ if (aAction is null)
31
+ {
32
+ throw new ArgumentNullException(nameof(aAction));
33
+ }
34
+
35
+
36
+
37
+ System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
38
+ currentID = currentID + 1;
39
+ bool aRegistered = RegisterHotKey(window.Handle, currentID,
40
+ (uint)aModifier | MOD_NOREPEAT,
41
+ (uint)aVirtualKeyCode);
42
+
43
+ if (aRegistered)
44
+ {
45
+ registeredHotKeys.Add(new HotKeyWithAction(aModifier, aKey, aAction));
46
+ }
47
+ return aRegistered;
48
+ }
49
+
50
+ public void Dispose()
51
+ {
52
+ // unregister all the registered hot keys.
53
+ for (int i = currentID; i > 0; i--)
54
+ {
55
+ UnregisterHotKey(window.Handle, i);
56
+ }
57
+
58
+ // dispose the inner native window.
59
+ window.Dispose();
60
+ }
61
+
62
+ static GlobalHotKey()
63
+ {
64
+ window.KeyPressed += (s, e) =>
65
+ {
66
+ registeredHotKeys.ForEach(x =>
67
+ {
68
+ if (e.Modifier == x.Modifier && e.Key == x.Key)
69
+ {
70
+ x.Action();
71
+ }
72
+ });
73
+ };
74
+ }
75
+
76
+ private static readonly InvisibleWindowForMessages window = new InvisibleWindowForMessages();
77
+ private static int currentID;
78
+ private static uint MOD_NOREPEAT = 0x4000;
79
+ private static List<HotKeyWithAction> registeredHotKeys = new List<HotKeyWithAction>();
80
+
81
+ private class HotKeyWithAction
82
+ {
83
+
84
+ public HotKeyWithAction(ModifierKeys modifier, Key key, Action action)
85
+ {
86
+ Modifier = modifier;
87
+ Key = key;
88
+ Action = action;
89
+ }
90
+
91
+ public ModifierKeys Modifier { get; }
92
+ public Key Key { get; }
93
+ public Action Action { get; }
94
+ }
95
+
96
+ // Registers a hot key with Windows.
97
+ [DllImport("user32.dll")]
98
+ private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
99
+ // Unregisters the hot key with Windows.
100
+ [DllImport("user32.dll")]
101
+ private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
102
+
103
+ private class InvisibleWindowForMessages : System.Windows.Forms.NativeWindow, IDisposable
104
+ {
105
+ public InvisibleWindowForMessages()
106
+ {
107
+ CreateHandle(new System.Windows.Forms.CreateParams());
108
+ }
109
+
110
+ private static int WM_HOTKEY = 0x0312;
111
+ protected override void WndProc(ref System.Windows.Forms.Message m)
112
+ {
113
+ base.WndProc(ref m);
114
+
115
+ if (m.Msg == WM_HOTKEY)
116
+ {
117
+ var aWPFKey = KeyInterop.KeyFromVirtualKey(((int)m.LParam >> 16) & 0xFFFF);
118
+ ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
119
+ if (KeyPressed != null)
120
+ {
121
+ KeyPressed(this, new HotKeyPressedEventArgs(modifier, aWPFKey));
122
+ }
123
+ }
124
+ }
125
+
126
+ public class HotKeyPressedEventArgs : EventArgs
127
+ {
128
+ private ModifierKeys _modifier;
129
+ private Key _key;
130
+
131
+ internal HotKeyPressedEventArgs(ModifierKeys modifier, Key key)
132
+ {
133
+ _modifier = modifier;
134
+ _key = key;
135
+ }
136
+
137
+ public ModifierKeys Modifier
138
+ {
139
+ get { return _modifier; }
140
+ }
141
+
142
+ public Key Key
143
+ {
144
+ get { return _key; }
145
+ }
146
+ }
147
+
148
+
149
+ public event EventHandler<HotKeyPressedEventArgs> KeyPressed;
150
+
151
+ #region IDisposable Members
152
+
153
+ public void Dispose()
154
+ {
155
+ this.DestroyHandle();
156
+ }
157
+
158
+ #endregion
159
+ }
160
+ }
161
+
162
+ }