diff --git a/.vs/ShotApp/v16/.suo b/.vs/ShotApp/v16/.suo index 5cc040c..3a6085f 100644 Binary files a/.vs/ShotApp/v16/.suo and b/.vs/ShotApp/v16/.suo differ diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index a3d9410..db48934 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -60,6 +60,16 @@ public class Resources { } } + /// + /// 查找 System.Drawing.Bitmap 类型的本地化资源。 + /// + public static System.Drawing.Bitmap eraserIcon { + get { + object obj = ResourceManager.GetObject("eraserIcon", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// 查找类似于 (图标) 的 System.Drawing.Icon 类型的本地化资源。 /// @@ -69,5 +79,15 @@ public class Resources { return ((System.Drawing.Icon)(obj)); } } + + /// + /// 查找 System.Drawing.Bitmap 类型的本地化资源。 + /// + public static System.Drawing.Bitmap penIcon { + get { + object obj = ResourceManager.GetObject("penIcon", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } } } diff --git a/Properties/Resources.resx b/Properties/Resources.resx index fc6111e..36e1f06 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -118,7 +118,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\Resources\eraserIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\Resources\logo.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\penIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + \ No newline at end of file diff --git a/Resources/eraserIcon.png b/Resources/eraserIcon.png new file mode 100644 index 0000000..df259d9 Binary files /dev/null and b/Resources/eraserIcon.png differ diff --git a/Resources/penIcon.png b/Resources/penIcon.png new file mode 100644 index 0000000..a8b2561 Binary files /dev/null and b/Resources/penIcon.png differ diff --git a/Screenshot.csproj b/Screenshot.csproj index 064e791..db84fbc 100644 --- a/Screenshot.csproj +++ b/Screenshot.csproj @@ -111,6 +111,11 @@ MSBuild:Compile Designer + + True + True + Resources.resx + Designer MSBuild:Compile @@ -123,6 +128,7 @@ App.xaml Code + ImgBox.xaml @@ -135,11 +141,6 @@ Code - - True - True - Resources.resx - True Settings.settings @@ -180,7 +181,16 @@ false - + + + PreserveNewest + + + + + PreserveNewest + + diff --git a/View/ImgBox.cs b/View/ImgBox.cs new file mode 100644 index 0000000..bdac1bd --- /dev/null +++ b/View/ImgBox.cs @@ -0,0 +1,160 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace WpfApp.View +{ + public partial class ImgBox : Window + { + private void Pen_Click(object sender, RoutedEventArgs e) + { + if (!mode.Equals(Mode.Pen)) + { + ShutdownEraserMode(); + OpenPenMode(); + } + else + { + ShutdownPenMode(); + } + + } + + private void Eraser_Click(object sender, RoutedEventArgs e) + { + if (!mode.Equals(Mode.Eraser)) + { + ShutdownPenMode(); + OpenEraserMode(); + } + else + { + ShutdownEraserMode(); + } + } + + private void OpenEraserMode() + { + mode = Mode.Eraser; + this.MouseLeftButtonDown += RemoveLine; + } + + private void ShutdownEraserMode() + { + mode = Mode.Drag; + this.MouseLeftButtonDown -= RemoveLine; + } + + private void RemoveLine(object sender, MouseButtonEventArgs e) + { + Point p = e.GetPosition(this); + IEnumerator ie = this.Panel.Children.GetEnumerator(); + Line cur = null; + double min = 10; + while (ie.MoveNext()) + { + var elem = ie.Current as Line; + double dis = GetDistance(p, elem); + if (dis < min) + { + cur = elem; + min = dis; + } + } + if(cur != null) + { + this.Panel.Children.Remove(cur); + } + } + + private double GetDistance(Point p, Line l) + { + double k = (l.Y2 - l.Y1) / (l.X2 - l.X1); + double a = k; + double b = -1; + double c = l.Y1 - k * l.X1; + double fz = Math.Abs(a * p.X + b * p.Y + c); + double fm = Math.Sqrt(a * a + b * b); + return fz / fm; + } + + private void OpenPenMode() + { + mode = Mode.Pen; + this.Cursor = Cursors.Pen; + this.MouseLeftButtonUp += StopDrawLine; + this.MouseLeftButtonDown += StartDrawLine; + this.MouseMove += DrawLine; + } + + private void ShutdownPenMode() + { + mode = Mode.Drag; + this.Cursor = Cursors.Arrow; + this.MouseLeftButtonUp -= StopDrawLine; + this.MouseLeftButtonDown -= StartDrawLine; + this.MouseMove -= DrawLine; + } + + private Line line; + + private void StartDrawLine(object sender, MouseEventArgs e) + { + var p = e.GetPosition(this); + line = new Line(); + line.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); + line.StrokeThickness = 3; + line.X1 = p.X; + line.Y1 = p.Y; + this.Panel.Children.Add(line); + } + + private void DrawLine(object sender, MouseEventArgs e) + { + if(e.LeftButton == MouseButtonState.Pressed) + { + Point p = e.GetPosition(this); + if (line != null) + { + line.X2 = p.X; + line.Y2 = p.Y; + } + } + } + + private void StopDrawLine(object sender, MouseEventArgs e) + { + line = null; + } + + private void Bar_MouseEnter(object sender, MouseEventArgs e) + { + this.Bar.Width = rw; + } + + private void Bar_MouseLeave(object sender, MouseEventArgs e) + { + this.Bar.Width = 15; + } + + private void Bar_KeyDown(object sender, KeyEventArgs e) + { + Console.WriteLine(e.Key); + if(e.Key.Equals(Key.Escape)) + { + if (!mode.Equals(Mode.Drag)) + { + ShutdownEraserMode(); + ShutdownPenMode(); + } + } + } + } +} diff --git a/View/ImgBox.xaml b/View/ImgBox.xaml index cc29e78..1f22fc8 100644 --- a/View/ImgBox.xaml +++ b/View/ImgBox.xaml @@ -5,13 +5,53 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp.View" mc:Ignorable="d" - Title="ImgBox" MouseMove="WindowDrag" Topmost="True" SizeChanged="img_SizeChanged" WindowStyle="None" Background="White" + KeyDown="Bar_KeyDown" + Title="ImgBox" MouseMove="Mouse_Move" Topmost="True" SizeChanged="img_SizeChanged" WindowStyle="None" Background="{x:Null}" ResizeMode="NoResize" BorderThickness="0.5" Height="424" Width="663"> + + + - + + + + + + + + + diff --git a/View/ImgBox.xaml.cs b/View/ImgBox.xaml.cs index 05e51f0..cdd9687 100644 --- a/View/ImgBox.xaml.cs +++ b/View/ImgBox.xaml.cs @@ -10,12 +10,22 @@ namespace WpfApp.View { + + enum Mode + { + Drag, + Pen, + Eraser, + } + /// /// ImgBox.xaml 的交互逻辑 /// public partial class ImgBox : Window { private double scale; + private Mode mode = Mode.Drag; + private double rw; public ImgBox(ImageSource image, double width, double height) { @@ -29,12 +39,21 @@ public ImgBox(ImageSource image, double width, double height) scale = width / height; img.Source = image; + + rw = this.Bar.Width; + this.Bar.Width = 15; } - private void WindowDrag(object sender, MouseEventArgs e) + private void Mouse_Move(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) - this.DragMove(); + { + if (mode.Equals(Mode.Drag)) + { + this.DragMove(); + } + } + } private void ExitItem_Click(object sender, RoutedEventArgs e) @@ -50,7 +69,7 @@ private void CopyItem_Click(object sender, RoutedEventArgs e) private void SaveItem_Click(object sender, RoutedEventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); - sfd.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg | All Files | *.*"; + sfd.Filter = "Image Files (*.png, *.bmp, *.jpg)|*.png;*.bmp;*.jpg | All Files | *.*"; sfd.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录 if (sfd.ShowDialog().GetValueOrDefault()) { @@ -86,5 +105,10 @@ private void Grid_MouseWheel(object sender, MouseWheelEventArgs e) this.Height = this.Width / scale; } } + + private void ClearItem_Click(object sender, RoutedEventArgs e) + { + this.Panel.Children.Clear(); + } } } diff --git a/View/MainWindow.xaml b/View/MainWindow.xaml index fa3c1b9..6d24307 100644 --- a/View/MainWindow.xaml +++ b/View/MainWindow.xaml @@ -13,6 +13,6 @@ - + \ No newline at end of file