-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
354 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 48 additions & 22 deletions
70
src/WPFDevelopers.Samples.Shared/ExampleViews/ScreenCutExample.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,53 @@ | ||
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.ScreenCutExample" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews" | ||
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls" | ||
mc:Ignorable="d" | ||
d:DesignHeight="450" d:DesignWidth="800"> | ||
<UserControl | ||
x:Class="WPFDevelopers.Samples.ExampleViews.ScreenCutExample" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
mc:Ignorable="d"> | ||
<controls:CodeViewer> | ||
<UniformGrid Rows="2"> | ||
<CheckBox Content="截图时隐藏当前窗口" | ||
VerticalAlignment="Bottom" HorizontalAlignment="Center" | ||
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ScreenCutExample}}, Path=IsChecked}"/> | ||
<Button Content="截屏" VerticalAlignment="Top" HorizontalAlignment="Center" Click="Button_Click" | ||
Margin="0,10"/> | ||
</UniformGrid> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition /> | ||
<RowDefinition /> | ||
</Grid.RowDefinitions> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition /> | ||
<ColumnDefinition /> | ||
</Grid.ColumnDefinitions> | ||
<CheckBox | ||
Margin="0,10,0,10" | ||
HorizontalAlignment="Center" | ||
Content="截图时隐藏当前窗口" | ||
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ScreenCutExample}}, Path=IsChecked}" /> | ||
<Button | ||
Grid.Row="1" | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Top" | ||
Click="Button_Click" | ||
Content="截屏" /> | ||
<Button | ||
Grid.Row="1" | ||
Grid.Column="1" | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Top" | ||
Click="ButtonExt_Click" | ||
Content="截屏Ext" | ||
Style="{StaticResource WD.SuccessPrimaryButton}" /> | ||
<Image | ||
x:Name="myImage" | ||
Grid.Row="2" | ||
Grid.ColumnSpan="2" | ||
Stretch="Uniform" /> | ||
</Grid> | ||
<controls:CodeViewer.SourceCodes> | ||
<controls:SourceCodeModel | ||
CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ScreenCutExample.xaml" | ||
CodeType="Xaml"/> | ||
<controls:SourceCodeModel | ||
CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ScreenCutExample.xaml.cs" | ||
CodeType="CSharp"/> | ||
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ScreenCutExample.xaml" CodeType="Xaml" /> | ||
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ScreenCutExample.xaml.cs" CodeType="CSharp" /> | ||
</controls:CodeViewer.SourceCodes> | ||
</controls:CodeViewer> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCaptureExt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Windows; | ||
using System.Windows.Interop; | ||
using System.Windows.Media.Imaging; | ||
|
||
namespace WPFDevelopers.Controls | ||
{ | ||
public class ScreenCaptureExt : Window | ||
{ | ||
const int WM_USER = 0x03FC; | ||
const int MY_MESSAGE = WM_USER + 1; | ||
/// <summary> | ||
/// 截图完成委托 | ||
/// </summary> | ||
public delegate void ScreenShotDone(BitmapSource bitmap); | ||
/// <summary> | ||
/// 截图完成事件 | ||
/// </summary> | ||
public event ScreenShotDone SnapCompleted; | ||
/// <summary> | ||
/// 截图取消委托 | ||
/// </summary> | ||
public delegate void ScreenShotCanceled(); | ||
/// <summary> | ||
/// 截图取消事件 | ||
/// </summary> | ||
public event ScreenShotCanceled SnapCanceled; | ||
|
||
public ScreenCaptureExt() | ||
{ | ||
Width = 0; | ||
Height = 0; | ||
Left = int.MinValue; | ||
Top = int.MinValue; | ||
WindowStyle = WindowStyle.None; | ||
ShowInTaskbar = false; | ||
ShowActivated = false; | ||
Title = "ScreenShot"; | ||
Show(); | ||
ShowScreenShot(); | ||
} | ||
|
||
protected override void OnSourceInitialized(EventArgs e) | ||
{ | ||
base.OnSourceInitialized(e); | ||
var hwndSource = PresentationSource.FromVisual(this) as HwndSource; | ||
if (hwndSource != null) hwndSource.AddHook(WndProc); | ||
} | ||
|
||
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) | ||
{ | ||
switch (msg) | ||
{ | ||
case WM_USER: | ||
GetClipboard(); | ||
Close(); | ||
break; | ||
case MY_MESSAGE: | ||
if (SnapCanceled != null) | ||
SnapCanceled(); | ||
Close(); | ||
break; | ||
} | ||
return IntPtr.Zero; | ||
} | ||
|
||
void GetClipboard() | ||
{ | ||
if (Clipboard.ContainsImage()) | ||
{ | ||
var dataObject = Clipboard.GetDataObject(); | ||
if (dataObject == null) return; | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
var encoder = new PngBitmapEncoder(); | ||
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)dataObject.GetData(DataFormats.Bitmap))); | ||
encoder.Save(memoryStream); | ||
memoryStream.Position = 0; | ||
var bitmapImage = new BitmapImage(); | ||
bitmapImage.BeginInit(); | ||
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; | ||
bitmapImage.StreamSource = memoryStream; | ||
bitmapImage.EndInit(); | ||
bitmapImage.Freeze(); | ||
if (SnapCompleted != null) | ||
SnapCompleted(bitmapImage); | ||
} | ||
} | ||
} | ||
|
||
void ShowScreenShot() | ||
{ | ||
if (Helper.GetTempPathVersionExt != null && File.Exists(Helper.GetTempPathVersionExt)) | ||
{ | ||
var process = new Process(); | ||
process.StartInfo.FileName = Helper.GetTempPathVersionExt; | ||
process.StartInfo.Arguments = Title; | ||
process.Start(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.