Skip to content

Commit f29d4a8

Browse files
committed
Fix issue #123
Get the path after taking a screenshot and saving the file @chen2well
1 parent 35608ef commit f29d4a8

File tree

7 files changed

+73
-15
lines changed

7 files changed

+73
-15
lines changed

src/WPFDevelopers.Samples.Shared/ExampleViews/ScreenCutExample.xaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows.Controls;
55
using WPFDevelopers.Controls;
66
using WPFDevelopers.Controls.ScreenCapturer;
7+
using static System.Net.Mime.MediaTypeNames;
78

89
namespace WPFDevelopers.Samples.ExampleViews
910
{
@@ -50,11 +51,17 @@ private void Button_Click(object sender, RoutedEventArgs e)
5051
screenCapturer = new ScreenCapture();
5152
screenCapturer.SnapCompleted += ScreenCapturer_SnapCompleted;
5253
screenCapturer.SnapCanceled += ScreenCapturer_SnapCanceled;
54+
screenCapturer.SnapSaveFullPath += ScreenCapturer_SnapSaveFullPath;
5355
screenCapturer.Capture();
5456
}));
5557

5658
}
5759

60+
private void ScreenCapturer_SnapSaveFullPath(string text)
61+
{
62+
WPFDevelopers.Controls.MessageBox.Show($"截图路径:{text}","Info",MessageBoxImage.Information);
63+
}
64+
5865
private void ScreenCapturer_SnapCanceled()
5966
{
6067
App.CurrentMainWindow.WindowState = WindowState.Normal;
@@ -76,6 +83,12 @@ private void ButtonExt_Click(object sender, RoutedEventArgs e)
7683
var screenCaptureExt = new ScreenCaptureExt();
7784
screenCaptureExt.SnapCanceled += ScreenCaptureExt_SnapCanceled;
7885
screenCaptureExt.SnapCompleted += ScreenCaptureExt_SnapCompleted;
86+
screenCaptureExt.SnapSaveFullPath += ScreenCaptureExt_SnapSaveFullPath;
87+
}
88+
89+
private void ScreenCaptureExt_SnapSaveFullPath(string text)
90+
{
91+
Message.Push($"截图路径:{text}", MessageBoxImage.Information);
7992
}
8093

8194
private void ScreenCaptureExt_SnapCompleted(System.Windows.Media.Imaging.BitmapSource bitmap)

src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCapture.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Windows;
34
using System.Windows.Forms;
45
using System.Windows.Media.Imaging;
@@ -24,6 +25,10 @@ public class ScreenCapture
2425
/// </summary>
2526
public event ScreenShotCanceled SnapCanceled;
2627
/// <summary>
28+
/// 获取保存的图片全路径
29+
/// </summary>
30+
public event Action<string> SnapSaveFullPath;
31+
/// <summary>
2732
/// 是否将截图结果复制
2833
/// 默认复制
2934
/// </summary>
@@ -49,10 +54,16 @@ private ScreenCut CaptureScreen(int index)
4954
ScreenCut screenCut = new ScreenCut(index);
5055
screenCut.CutCompleted += ScreenCut_CutCompleted;
5156
screenCut.CutCanceled += ScreenCut_CutCanceled;
57+
screenCut.CutFullPath += ScreenCut_CutFullPath;
5258
screenCut.Closed += ScreenCut_Closed;
5359
return screenCut;
5460
}
5561

62+
private void ScreenCut_CutFullPath(string text)
63+
{
64+
if(SnapSaveFullPath != null) SnapSaveFullPath(text);
65+
}
66+
5667
private void ScreenCut_CutCanceled()
5768
{
5869
if (SnapCanceled != null) SnapCanceled();

src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCaptureExt.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Linq;
45
using System.Windows;
56
using System.Windows.Interop;
67
using System.Windows.Media.Imaging;
8+
using WPFDevelopers.Helpers;
79

810
namespace WPFDevelopers.Controls
911
{
1012
public class ScreenCaptureExt : Window
1113
{
12-
14+
private ThemeType? _theme;
1315
/// <summary>
1416
/// 截图完成委托
1517
/// </summary>
@@ -26,9 +28,23 @@ public class ScreenCaptureExt : Window
2628
/// 截图取消事件
2729
/// </summary>
2830
public event ScreenShotCanceled SnapCanceled;
31+
/// <summary>
32+
/// 获取保存的图片全路径
33+
/// </summary>
34+
public event Action<string> SnapSaveFullPath;
2935

30-
public ScreenCaptureExt()
36+
public ScreenCaptureExt(ThemeType? themeType = null)
3137
{
38+
if(themeType == null)
39+
{
40+
var existingResourceDictionary =
41+
(Resources)Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is Resources);
42+
if (existingResourceDictionary != null)
43+
themeType = existingResourceDictionary.Theme;
44+
else
45+
themeType = ThemeType.Dark;
46+
}
47+
_theme = themeType;
3248
Width = 0;
3349
Height = 0;
3450
Left = int.MinValue;
@@ -61,6 +77,10 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
6177
if (SnapCanceled != null)
6278
SnapCanceled();
6379
break;
80+
case Helper.MY_MESSAGEFULLPATH:
81+
Close();
82+
GetClipboard();
83+
break;
6484
}
6585
return IntPtr.Zero;
6686
}
@@ -88,15 +108,22 @@ void GetClipboard()
88108
SnapCompleted(bitmapImage);
89109
}
90110
}
111+
else if (Clipboard.ContainsText())
112+
{
113+
var clipboardText = Clipboard.GetText();
114+
if (SnapSaveFullPath != null)
115+
SnapSaveFullPath(clipboardText);
116+
}
91117
}
92118

93119
void ShowScreenShot()
94120
{
121+
string[] args = { Title, _theme.ToString() };//1.窗体tilte 2.Light或Dark
95122
if (Helper.GetTempPathVersionExt != null && File.Exists(Helper.GetTempPathVersionExt))
96123
{
97124
var process = new Process();
98125
process.StartInfo.FileName = Helper.GetTempPathVersionExt;
99-
process.StartInfo.Arguments = Title;
126+
process.StartInfo.Arguments = string.Join(" ", args);
100127
process.Start();
101128
}
102129
}

src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCut.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public class ScreenCut : Window, IDisposable
183183
/// 截图取消事件
184184
/// </summary>
185185
public event ScreenShotCanceled CutCanceled;
186+
/// <summary>
187+
/// 获取保存的图片全路径
188+
/// </summary>
189+
public event Action<string> CutFullPath;
186190
private double _y1;
187191
private int _screenIndex;
188192
public static int CaptureScreenID = -1;
@@ -428,6 +432,8 @@ private void ButtonSave_Click(object sender, RoutedEventArgs e)
428432
fs.Dispose();
429433
fs.Close();
430434
Close();
435+
if (CutFullPath != null)
436+
CutFullPath(dlg.FileName);
431437
}
432438
}
433439
}

src/WPFDevelopers.Shared/Core/Helpers/Helper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Helper
1313
{
1414
public const int WM_USER = 0x03FC;
1515
public const int MY_MESSAGE = WM_USER + 1;
16+
public const int MY_MESSAGEFULLPATH = MY_MESSAGE + 1;
1617
public static string GetTempPath = Path.Combine(Path.GetTempPath(), Assembly.GetExecutingAssembly().GetName().Name);
1718
public static string GetTempPathVersion = Path.Combine(GetTempPath, GetMD5Hash(GetCurrentDllPath()));
1819
public const string GetExtName = "WPFDevelopersExt.exe";
16.3 KB
Binary file not shown.

src/WPFDevelopers.Shared/Themes/ScreenCut.xaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<RadioButton
8080
x:Name="PART_RadioButtonRectangle"
8181
Margin="4,0"
82-
Style="{DynamicResource WD.PathRadioButton}"
82+
Style="{StaticResource WD.PathRadioButton}"
8383
ToolTip="{Binding [Rectangle], Source={x:Static resx:LanguageManager.Instance}}">
8484
<controls:PathIcon
8585
Width="18"
@@ -90,7 +90,7 @@
9090
<RadioButton
9191
x:Name="PART_RadioButtonEllipse"
9292
Margin="4,0"
93-
Style="{DynamicResource WD.PathRadioButton}"
93+
Style="{StaticResource WD.PathRadioButton}"
9494
ToolTip="{Binding [Ellipse], Source={x:Static resx:LanguageManager.Instance}}">
9595
<Ellipse
9696
Width="19"
@@ -103,7 +103,7 @@
103103
<RadioButton
104104
x:Name="PART_RadioButtonArrow"
105105
Margin="4,0"
106-
Style="{DynamicResource WD.PathRadioButton}"
106+
Style="{StaticResource WD.PathRadioButton}"
107107
ToolTip="{Binding [Arrow], Source={x:Static resx:LanguageManager.Instance}}">
108108
<controls:PathIcon
109109
Width="18"
@@ -114,7 +114,7 @@
114114
<RadioButton
115115
x:Name="PART_RadioButtonInk"
116116
Margin="4,0"
117-
Style="{DynamicResource WD.PathRadioButton}"
117+
Style="{StaticResource WD.PathRadioButton}"
118118
ToolTip="{Binding [Ink], Source={x:Static resx:LanguageManager.Instance}}">
119119
<controls:PathIcon
120120
Width="18"
@@ -141,7 +141,7 @@
141141
<Button
142142
x:Name="PART_ButtonSave"
143143
Margin="4,0"
144-
Style="{DynamicResource WD.PathButton}"
144+
Style="{StaticResource WD.PathButton}"
145145
ToolTip="{Binding [Save], Source={x:Static resx:LanguageManager.Instance}}">
146146
<controls:PathIcon
147147
Width="18"
@@ -152,7 +152,7 @@
152152
<Button
153153
x:Name="PART_ButtonCancel"
154154
Margin="4,0"
155-
Style="{DynamicResource WD.PathButton}"
155+
Style="{StaticResource WD.PathButton}"
156156
ToolTip="{Binding [Cancel], Source={x:Static resx:LanguageManager.Instance}}">
157157
<controls:PathIcon
158158
Width="14"
@@ -163,7 +163,7 @@
163163
<Button
164164
x:Name="PART_ButtonComplete"
165165
Margin="4,0"
166-
Style="{DynamicResource WD.PathButton}"
166+
Style="{StaticResource WD.PathButton}"
167167
ToolTip="{Binding [Complete], Source={x:Static resx:LanguageManager.Instance}}">
168168
<controls:PathIcon
169169
Width="20"
@@ -202,19 +202,19 @@
202202
Margin="4,0"
203203
Background="Red"
204204
IsChecked="True"
205-
Style="{DynamicResource WD.ColorRadioButton}" />
205+
Style="{StaticResource WD.ColorRadioButton}" />
206206
<RadioButton
207207
Margin="4,0"
208208
Background="DodgerBlue"
209-
Style="{DynamicResource WD.ColorRadioButton}" />
209+
Style="{StaticResource WD.ColorRadioButton}" />
210210
<RadioButton
211211
Margin="4,0"
212212
Background="LimeGreen"
213-
Style="{DynamicResource WD.ColorRadioButton}" />
213+
Style="{StaticResource WD.ColorRadioButton}" />
214214
<RadioButton
215215
Margin="4,0"
216216
Background="Yellow"
217-
Style="{DynamicResource WD.ColorRadioButton}" />
217+
Style="{StaticResource WD.ColorRadioButton}" />
218218
</WrapPanel>
219219
</controls:SmallPanel>
220220
</Border>

0 commit comments

Comments
 (0)