main
syneffort 2 years ago
parent 2f47c6b1e6
commit bf67edb4cc
  1. 9
      PacticeSolution/Capture/App.xaml
  2. 17
      PacticeSolution/Capture/App.xaml.cs
  3. 10
      PacticeSolution/Capture/AssemblyInfo.cs
  4. 14
      PacticeSolution/Capture/Capture.csproj
  5. 25
      PacticeSolution/Capture/MainWindow.xaml
  6. 84
      PacticeSolution/Capture/MainWindow.xaml.cs
  7. 16
      PacticeSolution/Capture/OptionContext.cs
  8. 12
      PacticeSolution/PacticeSolution.sln
  9. 9
      PacticeSolution/WPFCanvas/App.xaml
  10. 17
      PacticeSolution/WPFCanvas/App.xaml.cs
  11. 10
      PacticeSolution/WPFCanvas/AssemblyInfo.cs
  12. 64
      PacticeSolution/WPFCanvas/MainWindow.xaml
  13. 161
      PacticeSolution/WPFCanvas/MainWindow.xaml.cs
  14. 10
      PacticeSolution/WPFCanvas/WPFCanvas.csproj

@ -0,0 +1,9 @@
<Application x:Class="Capture.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Capture"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Capture
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>
</Project>

@ -0,0 +1,25 @@
<Window x:Class="Capture.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Capture"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" AllowsTransparency="True" WindowStyle="None" ResizeMode="CanResizeWithGrip"
MouseDown="Window_MouseDown">
<Window.Background>
<SolidColorBrush Color="White" Opacity="0.002"/>
</Window.Background>
<Window.DataContext>
<local:OptionContext/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="{Binding ButtonHeight}"/>
</Grid.RowDefinitions>
<Border BorderThickness="{Binding FrameThickness}" BorderBrush="{Binding FrameColor}"/>
<Button x:Name="btnCapture" Grid.Row="1" Content="Capture" Click="btnCapture_Click"/>
</Grid>
</Window>

@ -0,0 +1,84 @@
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.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Security.Cryptography;
namespace Capture
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private OptionContext _options;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_options = this.DataContext as OptionContext;
}
private void CaptureScreen()
{
int width = (int)this.Width - _options.FrameThickness * 2;
int height = (int)this.Height - _options.FrameThickness * 2 - _options.ButtonHeight;
int left = (int)this.Left + _options.FrameThickness;
int top = (int)this.Top + _options.FrameThickness;
string path = @"C:\Temp\test.bmp";
using (Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics gr = Graphics.FromImage(bitmap))
{
gr.CopyFromScreen(left, top, 0, 0, bitmap.Size);
}
bitmap.Save(path, ImageFormat.Png);
}
OpenFile(path);
}
private void OpenFile(string path)
{
Process pr = new Process();
pr.StartInfo = new ProcessStartInfo(path)
{
UseShellExecute = true
};
pr.Start();
}
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
CaptureScreen();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;
this.DragMove();
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace Capture
{
public class OptionContext
{
public int ButtonHeight { get; set; } = 20;
public Brush FrameColor { get; set; } = Brushes.Red;
public int FrameThickness { get; set; } = 2;
}
}

@ -139,6 +139,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChartSample", "ChartSample\
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LiveChartPractice", "LiveChartPractice\LiveChartPractice.csproj", "{4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LiveChartPractice", "LiveChartPractice\LiveChartPractice.csproj", "{4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFCanvas", "WPFCanvas\WPFCanvas.csproj", "{D407D4BF-1BE7-4C02-B2A8-CF15A92793CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Capture", "Capture\Capture.csproj", "{08694854-976A-4551-A4CA-7BA80CFCC33C}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -417,6 +421,14 @@ Global
{4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Debug|Any CPU.Build.0 = Debug|Any CPU {4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Release|Any CPU.ActiveCfg = Release|Any CPU {4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Release|Any CPU.Build.0 = Release|Any CPU {4B945CF3-2E74-46AA-8EFC-21B6434A4AC2}.Release|Any CPU.Build.0 = Release|Any CPU
{D407D4BF-1BE7-4C02-B2A8-CF15A92793CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D407D4BF-1BE7-4C02-B2A8-CF15A92793CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D407D4BF-1BE7-4C02-B2A8-CF15A92793CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D407D4BF-1BE7-4C02-B2A8-CF15A92793CF}.Release|Any CPU.Build.0 = Release|Any CPU
{08694854-976A-4551-A4CA-7BA80CFCC33C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08694854-976A-4551-A4CA-7BA80CFCC33C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08694854-976A-4551-A4CA-7BA80CFCC33C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08694854-976A-4551-A4CA-7BA80CFCC33C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,9 @@
<Application x:Class="WPFCanvas.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFCanvas"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WPFCanvas
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,64 @@
<Window x:Class="WPFCanvas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFCanvas"
mc:Ignorable="d"
Title="Paint" Height="450" Width="800"
MouseMove="Window_MouseMove"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
MouseLeftButtonUp="Window_MouseLeftButtonUp">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnLine" Grid.Column="0" Margin="5" Content="Line" Click="btnLine_Click"/>
<Button x:Name="btnSquare" Grid.Column="1" Margin="5" Content="Square" Click="btnSquare_Click"/>
<ComboBox x:Name="cboThickness" Grid.Column="4" SelectedValuePath="Content" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" SelectedIndex="0" SelectionChanged="cboThickness_SelectionChanged">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="10"/>
<ComboBoxItem Content="20"/>
</ComboBox>
<Grid Grid.Column="5">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="btnBlack" Grid.Column="0" Background="Black" Margin="10 15 10 15" Click="btnBlack_Click"/>
<Button x:Name="btnRed" Grid.Column="1" Background="Red" Margin="10 15 10 15" Click="btnRed_Click"/>
<Button x:Name="btnBlue" Grid.Column="2" Background="Blue" Margin="10 15 10 15" Click="btnBlue_Click"/>
<Button x:Name="btnGreen" Grid.Column="3" Background="Green" Margin="10 15 10 15" Click="btnGreen_Click"/>
<Button x:Name="btnYellow" Grid.Column="4" Background="Yellow" Margin="10 15 10 15" Click="btnYellow_Click"/>
</Grid>
</Grid>
<Border Grid.Row="1" BorderBrush="Salmon" BorderThickness="2" CornerRadius="10" Margin="10">
<Canvas x:Name="canvas"/>
</Border>
</Grid>
</Window>

@ -0,0 +1,161 @@
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;
namespace WPFCanvas
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private enum DrawMode
{
Line,
Circle,
Square,
Eraser,
};
private bool _mouseClicked;
private Point _prePosition;
private Brush _myBrush;
private double _thickness;
private Rectangle _tmpRect;
private DrawMode MODE;
public MainWindow()
{
InitializeComponent();
}
private void btnLine_Click(object sender, RoutedEventArgs e)
{
MODE = DrawMode.Line;
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_mouseClicked = true;
_prePosition = e.GetPosition(canvas);
}
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_mouseClicked = false;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Point nowPosition = e.GetPosition(canvas);
if (!_mouseClicked)
return;
switch (MODE)
{
case DrawMode.Line:
Line line = new Line()
{
X1 = _prePosition.X,
Y1 = _prePosition.Y,
X2 = nowPosition.X,
Y2 = nowPosition.Y,
Stroke = _myBrush == null ? Brushes.Black : _myBrush,
StrokeThickness = _thickness <= 0 ? 1 : _thickness
};
canvas.Children.Add(line);
_prePosition = nowPosition;
break;
case DrawMode.Square:
if (e.MouseDevice.LeftButton != MouseButtonState.Pressed)
_tmpRect = null;
else
{
Rectangle rect = new Rectangle();
rect.Stroke = Brushes.Gray;
rect.Fill = _myBrush == null ? Brushes.Black : _myBrush;
rect.Opacity = 0.8;
double left = _prePosition.X;
double top = _prePosition.Y;
double width = nowPosition.X - _prePosition.X;
double height = nowPosition.Y - _prePosition.Y;
if (nowPosition.X < _prePosition.X)
{
left = nowPosition.X;
width *= -1;
}
if (nowPosition.Y < _prePosition.Y)
{
top = nowPosition.Y;
height *= -1;
}
rect.Margin = new Thickness(left, top, 0, 0);
rect.Width = width;
rect.Height = height;
canvas.Children.Remove(_tmpRect);
_tmpRect = rect;
canvas.Children.Add(rect);
}
break;
}
}
private void btnBlack_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Black;
}
private void btnRed_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Red;
}
private void btnBlue_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Blue;
}
private void btnGreen_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Green;
}
private void btnYellow_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Yellow;
}
private void cboThickness_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_thickness = double.Parse(cboThickness.SelectedValue.ToString());
}
private void btnSquare_Click(object sender, RoutedEventArgs e)
{
MODE = DrawMode.Square;
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Loading…
Cancel
Save