InkCanvasForClass/Ink Canvas/MainWindow.xaml.cs

2838 lines
104 KiB
C#
Raw Normal View History

2021-09-10 01:35:39 +08:00
using ModernWpf;
using System;
2020-11-22 11:42:46 +08:00
using System.IO;
2020-11-22 11:05:12 +08:00
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.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
2021-09-10 01:35:39 +08:00
using Microsoft.Office.Interop.PowerPoint;
2021-09-11 01:23:03 +08:00
using System.Runtime.InteropServices;
2021-09-12 13:18:10 +08:00
using Application = System.Windows.Application;
using System.Timers;
using System.Threading;
using Timer = System.Timers.Timer;
2021-09-12 18:28:26 +08:00
using System.Diagnostics;
using Newtonsoft.Json;
using IWshRuntimeLibrary;
using File = System.IO.File;
using System.Collections.ObjectModel;
2021-09-17 23:09:00 +08:00
using System.Net;
2021-09-18 00:10:16 +08:00
using Microsoft.VisualBasic;
using System.Reflection;
2021-09-25 16:05:22 +08:00
using System.Collections.Generic;
using Point = System.Windows.Point;
2021-10-10 11:50:11 +08:00
using System.Windows.Input.StylusPlugIns;
2020-11-22 11:05:12 +08:00
namespace Ink_Canvas
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
2021-09-17 23:09:00 +08:00
BorderSettings.Visibility = Visibility.Collapsed;
2020-11-22 11:05:12 +08:00
}
2021-09-12 13:18:10 +08:00
Timer timerCheckPPT = new Timer();
2021-09-12 18:28:26 +08:00
Settings Settings = new Settings();
2020-11-22 11:05:12 +08:00
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
Color Ink_DefaultColor = Colors.Red;
DrawingAttributes drawingAttributes;
private void loadPenCanvas()
{
try
{
2021-09-11 01:23:03 +08:00
//drawingAttributes = new DrawingAttributes();
drawingAttributes = inkCanvas.DefaultDrawingAttributes;
2020-11-22 11:05:12 +08:00
drawingAttributes.Color = Ink_DefaultColor;
2020-11-22 11:42:46 +08:00
drawingAttributes.Height = 2.5;
drawingAttributes.Width = 2.5;
2020-11-22 11:05:12 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
inkCanvas.Gesture += InkCanvas_Gesture;
2020-11-22 11:05:12 +08:00
}
catch { }
}
ApplicationGesture lastApplicationGesture = ApplicationGesture.AllGestures;
DateTime lastGestureTime = DateTime.Now;
private void InkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
{
ReadOnlyCollection<GestureRecognitionResult> gestures = e.GetGestureRecognitionResults();
foreach (GestureRecognitionResult gest in gestures)
{
//Trace.WriteLine(string.Format("Gesture: {0}, Confidence: {1}", gest.ApplicationGesture, gest.RecognitionConfidence));
if ((DateTime.Now - lastGestureTime).TotalMilliseconds <= 1500 &&
StackPanelPPTControls.Visibility == Visibility.Visible &&
lastApplicationGesture == gest.ApplicationGesture)
{
if (gest.ApplicationGesture == ApplicationGesture.Left)
{
BtnPPTSlidesDown_Click(BtnPPTSlidesDown, null);
}
if (gest.ApplicationGesture == ApplicationGesture.Right)
{
BtnPPTSlidesDown_Click(BtnPPTSlidesDown, null);
}
}
lastApplicationGesture = gest.ApplicationGesture;
lastGestureTime = DateTime.Now;
}
inkCanvas.Strokes.Add(e.Strokes);
}
2020-11-22 11:05:12 +08:00
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
bool isInkCanvasVisible = true;
2021-09-22 00:46:57 +08:00
bool isAutoUpdateEnabled = false;
2020-11-22 11:05:12 +08:00
private void Window_Loaded(object sender, RoutedEventArgs e)
{
2021-09-18 00:10:16 +08:00
//检查
2021-09-25 17:32:58 +08:00
new Thread(new ThreadStart(() =>
{
2021-09-19 01:12:13 +08:00
try
2021-09-18 00:10:16 +08:00
{
string response = GetWebClient("http://ink.wxriw.cn:1957");
2021-09-19 01:12:13 +08:00
if (response.Contains("Special Version"))
2021-09-18 00:10:16 +08:00
{
2021-09-22 00:46:57 +08:00
isAutoUpdateEnabled = true;
2021-09-19 01:12:13 +08:00
if (response.Contains("<notice>"))
2021-09-18 00:10:16 +08:00
{
2021-09-19 01:12:13 +08:00
string str = Strings.Mid(response, response.IndexOf("<notice>") + 9);
if (str.Contains("<notice>"))
2021-09-18 00:10:16 +08:00
{
2021-09-19 01:12:13 +08:00
str = Strings.Left(str, str.IndexOf("<notice>")).Trim();
if (str.Length > 0)
2021-09-18 00:10:16 +08:00
{
2021-09-19 01:12:13 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
GroupBoxMASEZVersion.Visibility = Visibility.Visible;
TextBlockMASEZNotice.Text = str;
});
}
2021-09-18 00:10:16 +08:00
}
}
2021-10-07 00:25:12 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
TextBlockVersion.Text = version.ToString();
string lastVersion = "";
try
{
lastVersion = File.ReadAllText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "versions.ini");
}
catch { }
if (!lastVersion.Contains(version.ToString()))
{
new ChangeLogWindow().ShowDialog();
2021-10-07 00:25:12 +08:00
lastVersion += "\n" + version.ToString();
File.WriteAllText("versions.ini", lastVersion.Trim());
}
});
2021-09-25 13:59:38 +08:00
try
{
2021-09-25 13:59:38 +08:00
if (response.Contains("<update>"))
{
2021-09-25 13:59:38 +08:00
string str = Strings.Mid(response, response.IndexOf("<update>") + 9);
if (str.Contains("<update>"))
{
str = Strings.Left(str, str.IndexOf("<update>")).Trim();
if (str.Length > 0)
{
string updateIP;
int updatePort;
string[] vs = str.Split(':');
updateIP = vs[0];
updatePort = int.Parse(vs[1]);
if (OAUS.Core.VersionHelper.HasNewVersion(GetIp(updateIP), updatePort))
{
string updateExePath = AppDomain.CurrentDomain.BaseDirectory + "AutoUpdater\\AutoUpdater.exe";
System.Diagnostics.Process myProcess = System.Diagnostics.Process.Start(updateExePath);
Application.Current.Dispatcher.Invoke(() =>
{
Application.Current.Shutdown();
2021-09-25 13:59:38 +08:00
});
}
}
}
}
}
2021-09-25 13:59:38 +08:00
catch { }
2021-09-18 00:10:16 +08:00
}
}
2021-09-19 01:12:13 +08:00
catch { }
2021-09-18 00:10:16 +08:00
})).Start();
loadPenCanvas();
2021-09-12 18:28:26 +08:00
//加载设置
LoadSettings();
2021-10-08 00:52:15 +08:00
ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light;
2021-10-07 00:25:12 +08:00
TextBlockVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
isLoaded = true;
}
private void LoadSettings(bool isStartup = true)
{
2021-10-07 00:25:12 +08:00
if (File.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + settingsFileName))
2021-09-12 18:28:26 +08:00
{
try
{
string text = File.ReadAllText(settingsFileName);
Settings = JsonConvert.DeserializeObject<Settings>(text);
}
catch { }
}
if (Settings.Startup.IsAutoEnterModeFinger)
{
ToggleSwitchModeFinger.IsOn = true;
ToggleSwitchAutoEnterModeFinger.IsOn = true;
}
else
{
ToggleSwitchAutoEnterModeFinger.IsOn = false;
}
2021-09-12 18:28:26 +08:00
if (Settings.Startup.IsAutoHideCanvas)
{
if (isStartup)
{
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
}
2021-09-12 18:28:26 +08:00
ToggleSwitchAutoHideCanvas.IsOn = true;
}
else
{
ToggleSwitchAutoHideCanvas.IsOn = false;
}
2021-09-12 18:28:26 +08:00
if (Settings.Appearance.IsShowEraserButton)
{
BtnErase.Visibility = Visibility.Visible;
ToggleSwitchShowButtonEraser.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
BtnErase.Visibility = Visibility.Collapsed;
ToggleSwitchShowButtonEraser.IsOn = false;
}
if (Settings.Appearance.IsShowExitButton)
{
BtnExit.Visibility = Visibility.Visible;
ToggleSwitchShowButtonExit.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
BtnExit.Visibility = Visibility.Collapsed;
ToggleSwitchShowButtonExit.IsOn = false;
}
if (Settings.Appearance.IsShowHideControlButton)
{
BtnHideControl.Visibility = Visibility.Visible;
ToggleSwitchShowButtonHideControl.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
BtnHideControl.Visibility = Visibility.Collapsed;
2021-10-08 00:37:56 +08:00
ToggleSwitchShowButtonHideControl.IsOn = false;
2021-09-12 18:28:26 +08:00
}
if (Settings.Appearance.IsShowLRSwitchButton)
{
BtnSwitchSide.Visibility = Visibility.Visible;
ToggleSwitchShowButtonLRSwitch.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
BtnSwitchSide.Visibility = Visibility.Collapsed;
2021-10-08 00:37:56 +08:00
ToggleSwitchShowButtonLRSwitch.IsOn = false;
2021-09-12 18:28:26 +08:00
}
if (Settings.Appearance.IsShowModeFingerToggleSwitch)
{
StackPanelModeFinger.Visibility = Visibility.Visible;
ToggleSwitchShowButtonModeFinger.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
StackPanelModeFinger.Visibility = Visibility.Collapsed;
ToggleSwitchShowButtonModeFinger.IsOn = false;
}
if (Settings.Appearance.IsTransparentButtonBackground)
2021-09-25 13:38:27 +08:00
{
BtnExit.Background = new SolidColorBrush(StringToColor("#7F909090"));
}
else
{
if (BtnSwitchTheme.Content.ToString() == "深色")
{
//Light
BtnExit.Background = new SolidColorBrush(StringToColor("#FFCCCCCC"));
}
else
{
//Dark
BtnExit.Background = new SolidColorBrush(StringToColor("#FF555555"));
}
2021-09-25 13:38:27 +08:00
}
2021-09-12 18:28:26 +08:00
if (Settings.Behavior.PowerPointSupport)
{
timerCheckPPT.Elapsed += TimerCheckPPT_Elapsed;
timerCheckPPT.Interval = 1000;
timerCheckPPT.Start();
}
else
{
ToggleSwitchSupportPowerPoint.IsOn = false;
timerCheckPPT.Stop();
2021-09-12 18:28:26 +08:00
}
if (Settings.Behavior.IsShowCanvasAtNewSlideShow)
{
ToggleSwitchShowCanvasAtNewSlideShow.IsOn = true;
}
else
2021-09-12 18:28:26 +08:00
{
ToggleSwitchShowCanvasAtNewSlideShow.IsOn = false;
}
if (Settings.Gesture == null)
{
Settings.Gesture = new Gesture();
}
2021-09-25 22:58:50 +08:00
if (Settings.Gesture.IsEnableTwoFingerRotation)
{
ToggleSwitchEnableTwoFingerRotation.IsOn = true;
}
else
{
ToggleSwitchEnableTwoFingerRotation.IsOn = false;
}
2021-09-25 22:58:50 +08:00
if (Settings.Gesture.IsEnableTwoFingerGestureInPresentationMode)
{
ToggleSwitchEnableTwoFingerGestureInPresentationMode.IsOn = true;
}
else
{
ToggleSwitchEnableTwoFingerGestureInPresentationMode.IsOn = false;
}
2021-09-25 22:58:50 +08:00
2021-09-12 18:28:26 +08:00
if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\InkCanvas" + ".lnk"))
{
ToggleSwitchRunAtStartup.IsOn = true;
}
if (Settings.Canvas != null)
{
drawingAttributes.Height = Settings.Canvas.InkWidth;
drawingAttributes.Width = Settings.Canvas.InkWidth;
InkWidthSlider.Value = Settings.Canvas.InkWidth * 2;
2021-09-25 13:38:27 +08:00
if (Settings.Canvas.IsShowCursor)
{
ToggleSwitchShowCursor.IsOn = true;
inkCanvas.ForceCursor = true;
}
else
{
ToggleSwitchShowCursor.IsOn = false;
inkCanvas.ForceCursor = false;
}
2021-09-25 13:38:27 +08:00
}
else
{
Settings.Canvas = new Canvas();
}
2020-11-22 11:05:12 +08:00
}
2021-09-12 18:28:26 +08:00
string settingsFileName = "settings.json";
bool isLoaded = false;
2020-11-22 11:05:12 +08:00
private void back_HotKey(object sender, ExecutedRoutedEventArgs e)
{
try
{
inkCanvas.Strokes.Remove(inkCanvas.Strokes[inkCanvas.Strokes.Count - 1]);
}
catch { }
}
2021-09-25 17:32:58 +08:00
2021-09-09 13:12:34 +08:00
private void KeyExit(object sender, ExecutedRoutedEventArgs e)
{
2021-09-12 13:18:10 +08:00
//if (isInkCanvasVisible)
//{
// Main_Grid.Visibility = Visibility.Hidden;
// isInkCanvasVisible = false;
// //inkCanvas.Strokes.Clear();
// WindowState = WindowState.Minimized;
//}
//else
//{
// Main_Grid.Visibility = Visibility.Visible;
// isInkCanvasVisible = true;
// inkCanvas.Strokes.Clear();
// WindowState = WindowState.Maximized;
//}
2021-09-09 13:12:34 +08:00
}
2021-09-25 17:32:58 +08:00
2021-09-09 13:12:34 +08:00
private void Window_KeyDown(object sender, KeyEventArgs e)
{
2021-09-25 17:32:58 +08:00
if (e.Key == Key.Escape)
2021-09-09 13:12:34 +08:00
{
KeyExit(null, null);
}
}
private void BtnExit_Click(object sender, RoutedEventArgs e)
{
Close();
}
2021-10-07 00:25:12 +08:00
private void BtnRestart_Click(object sender, RoutedEventArgs e)
{
Process.Start(System.Windows.Forms.Application.ExecutablePath);
Application.Current.Shutdown();
}
2021-09-12 18:28:26 +08:00
private void BtnSettings_Click(object sender, RoutedEventArgs e)
{
if (BorderSettings.Visibility == Visibility.Visible)
{
BorderSettings.Visibility = Visibility.Collapsed;
}
else
{
BorderSettings.Visibility = Visibility.Visible;
}
}
2021-09-09 13:12:34 +08:00
private void BtnThickness_Click(object sender, RoutedEventArgs e)
{
2021-09-25 17:32:58 +08:00
2021-09-09 13:12:34 +08:00
}
2021-09-10 01:35:39 +08:00
bool forceEraser = false;
2021-09-09 13:12:34 +08:00
private void BtnErase_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
forceEraser = true;
2021-09-09 13:12:34 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
2021-09-25 17:32:58 +08:00
drawingShapeMode = 0;
inkCanvas_EditingModeChanged(inkCanvas, null);
2021-09-09 13:12:34 +08:00
}
private void BtnClear_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
forceEraser = false;
2021-10-10 11:50:11 +08:00
if (inkCanvas.Strokes.Count != 0)
2021-10-10 11:50:11 +08:00
{
int whiteboardIndex = CurrentWhiteboardIndex;
if (currentMode == 0)
{
whiteboardIndex = 0;
}
strokeCollections[whiteboardIndex] = inkCanvas.Strokes.Clone();
2021-10-10 11:50:11 +08:00
BtnUndo.IsEnabled = true;
BtnUndo.Visibility = Visibility.Visible;
2021-10-10 11:50:11 +08:00
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
}
2021-10-10 11:50:11 +08:00
2021-09-09 13:12:34 +08:00
inkCanvas.Strokes.Clear();
}
private void BtnHideControl_Click(object sender, RoutedEventArgs e)
{
if (StackPanelControl.Visibility == Visibility.Visible)
{
StackPanelControl.Visibility = Visibility.Hidden;
}
else
{
StackPanelControl.Visibility = Visibility.Visible;
}
}
2021-09-12 13:18:10 +08:00
#region Buttons - Color
int inkColor = 1;
2021-09-10 01:35:39 +08:00
2021-09-11 01:23:03 +08:00
private void ColorSwitchCheck()
{
if (Main_Grid.Background == Brushes.Transparent)
{
if (currentMode == 1)
{
currentMode = 0;
GridBackgroundCover.Visibility = Visibility.Hidden;
}
2021-10-08 00:48:07 +08:00
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
2021-09-11 01:23:03 +08:00
}
2021-09-25 14:14:01 +08:00
inkCanvas.IsManipulationEnabled = true;
2021-09-25 17:32:58 +08:00
drawingShapeMode = 0;
2021-09-19 01:12:13 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
2021-09-11 01:23:03 +08:00
}
2021-09-09 13:12:34 +08:00
private void BtnColorBlack_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
inkColor = 0;
forceEraser = false;
2021-09-12 13:18:10 +08:00
if (BtnSwitchTheme.Content.ToString() == "浅色")
2021-09-10 01:35:39 +08:00
{
inkCanvas.DefaultDrawingAttributes.Color = Colors.White;
}
else
{
inkCanvas.DefaultDrawingAttributes.Color = Colors.Black;
}
2021-09-11 01:23:03 +08:00
ColorSwitchCheck();
2021-09-09 13:12:34 +08:00
}
private void BtnColorRed_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
inkColor = 1;
forceEraser = false;
2021-09-09 13:12:34 +08:00
inkCanvas.DefaultDrawingAttributes.Color = Colors.Red;
2021-09-11 01:23:03 +08:00
ColorSwitchCheck();
2021-09-09 13:12:34 +08:00
}
private void BtnColorGreen_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
inkColor = 2;
forceEraser = false;
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FF1ED760");
BtnColorGreen.Background = new SolidColorBrush(StringToColor("#FF1ED760"));
}
else
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FF169141");
BtnColorGreen.Background = new SolidColorBrush(StringToColor("#FF169141"));
}
2021-09-11 01:23:03 +08:00
ColorSwitchCheck();
2021-09-09 13:12:34 +08:00
}
private void BtnColorBlue_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
inkColor = 3;
forceEraser = false;
2021-09-09 13:12:34 +08:00
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FF239AD6");
2021-09-11 01:23:03 +08:00
ColorSwitchCheck();
2021-09-09 13:12:34 +08:00
}
private void BtnColorYellow_Click(object sender, RoutedEventArgs e)
{
2021-09-10 01:35:39 +08:00
inkColor = 4;
forceEraser = false;
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FFFFC000");
BtnColorYellow.Background = new SolidColorBrush(StringToColor("#FFFFC000"));
}
else
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FFF38B00");
BtnColorYellow.Background = new SolidColorBrush(StringToColor("#FFF38B00"));
}
2021-09-11 01:23:03 +08:00
ColorSwitchCheck();
2021-09-09 13:12:34 +08:00
}
private Color StringToColor(string colorStr)
{
Byte[] argb = new Byte[4];
for (int i = 0; i < 4; i++)
{
char[] charArray = colorStr.Substring(i * 2 + 1, 2).ToCharArray();
//string str = "11";
Byte b1 = toByte(charArray[0]);
Byte b2 = toByte(charArray[1]);
argb[i] = (Byte)(b2 | (b1 << 4));
}
return Color.FromArgb(argb[0], argb[1], argb[2], argb[3]);//#FFFFFFFF
}
private static byte toByte(char c)
{
byte b = (byte)"0123456789ABCDEF".IndexOf(c);
return b;
}
2021-09-12 13:18:10 +08:00
#endregion
#region Touch Events
2021-10-13 00:09:43 +08:00
int lastTouchDownTime = 0, lastTouchUpTime = 0;
2021-09-25 17:32:58 +08:00
bool isTouchDown = false; Point iniP = new Point(0, 0);
2021-09-26 12:56:41 +08:00
bool isLastTouchEraser = false;
2021-09-09 13:12:34 +08:00
private void Main_Grid_TouchDown(object sender, TouchEventArgs e)
{
2021-09-25 17:32:58 +08:00
iniP = e.GetTouchPoint(inkCanvas).Position;
2021-10-09 23:36:00 +08:00
double boundsWidth = e.GetTouchPoint(null).Bounds.Width;
if (boundsWidth > BoundsWidth)
2021-09-09 13:12:34 +08:00
{
2021-09-26 12:56:41 +08:00
isLastTouchEraser = true;
2021-09-28 00:19:39 +08:00
if (drawingShapeMode == 0 && forceEraser) return;
2021-10-09 23:36:00 +08:00
if (boundsWidth > BoundsWidth * 1.7)
{
inkCanvas.EraserShape = new EllipseStylusShape(boundsWidth * 1.5, boundsWidth * 1.5);
inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
}
else
{
inkCanvas.EraserShape = new RectangleStylusShape(8, 8);
//inkCanvas.EraserShape = new EllipseStylusShape(boundsWidth * 1.5, boundsWidth * 1.5);
inkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
}
2021-09-09 13:12:34 +08:00
}
else
{
2021-09-26 12:56:41 +08:00
isLastTouchEraser = false;
if (forceEraser) return;
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
2021-09-09 13:12:34 +08:00
}
}
2021-09-25 16:05:22 +08:00
//记录触摸设备ID
private List<int> dec = new List<int>();
//中心点
System.Windows.Point centerPoint;
2021-09-25 17:32:58 +08:00
InkCanvasEditingMode lastInkCanvasEditingMode = InkCanvasEditingMode.Ink;
2021-10-10 11:50:11 +08:00
bool isSingleFingerDragMode = false;
2021-09-25 16:05:22 +08:00
private void inkCanvas_PreviewTouchDown(object sender, TouchEventArgs e)
{
dec.Add(e.TouchDevice.Id);
//设备1个的时候记录中心点
if (dec.Count == 1)
{
TouchPoint touchPoint = e.GetTouchPoint(inkCanvas);
centerPoint = touchPoint.Position;
2021-10-10 11:50:11 +08:00
//记录第一根手指点击时的 StrokeCollection
lastTouchDownStrokeCollection = inkCanvas.Strokes.Clone();
2021-09-25 16:05:22 +08:00
}
//设备两个及两个以上,将画笔功能关闭
2021-10-10 11:50:11 +08:00
if (dec.Count > 1 || isSingleFingerDragMode)
2021-09-25 16:05:22 +08:00
{
2021-09-28 23:08:20 +08:00
if (inkCanvas.EditingMode != InkCanvasEditingMode.None && inkCanvas.EditingMode != InkCanvasEditingMode.Select)
2021-09-25 16:05:22 +08:00
{
2021-09-25 17:32:58 +08:00
lastInkCanvasEditingMode = inkCanvas.EditingMode;
2021-09-25 16:05:22 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.None;
}
}
}
private void inkCanvas_PreviewTouchUp(object sender, TouchEventArgs e)
{
//手势完成后切回之前的状态
if (dec.Count > 1)
{
if (inkCanvas.EditingMode == InkCanvasEditingMode.None)
{
2021-09-25 17:32:58 +08:00
inkCanvas.EditingMode = lastInkCanvasEditingMode;
2021-09-25 16:05:22 +08:00
}
}
dec.Remove(e.TouchDevice.Id);
2021-10-10 11:50:11 +08:00
if (dec.Count == 0)
{
if (lastTouchDownStrokeCollection != inkCanvas.Strokes)
{
int whiteboardIndex = CurrentWhiteboardIndex;
if (currentMode == 0)
{
whiteboardIndex = 0;
}
strokeCollections[whiteboardIndex] = lastTouchDownStrokeCollection;
BtnUndo.IsEnabled = true;
BtnUndo.Visibility = Visibility.Visible;
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
}
}
2021-09-25 16:05:22 +08:00
}
private void inkCanvas_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.Mode = ManipulationModes.All;
}
2021-09-28 23:08:20 +08:00
private void inkCanvas_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
}
private void Main_Grid_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
if (e.Manipulators.Count() == 0)
{
2021-09-25 13:38:27 +08:00
if (forceEraser) return;
2021-09-23 01:02:44 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
}
}
2021-09-25 16:05:22 +08:00
private MatrixTransform imageTransform;
private void Main_Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
2021-10-13 00:09:43 +08:00
if ((dec.Count >= 2 && (Settings.Gesture.IsEnableTwoFingerGestureInPresentationMode || StackPanelPPTControls.Visibility != Visibility.Visible || StackPanelPPTButtons.Visibility == Visibility.Collapsed)) || isSingleFingerDragMode)
2021-09-25 16:05:22 +08:00
{
ManipulationDelta md = e.DeltaManipulation;
Vector trans = md.Translation; // 获得位移矢量
double rotate = md.Rotation; // 获得旋转角度
Vector scale = md.Scale; // 获得缩放倍数
Matrix m = new Matrix();
// Find center of element and then transform to get current location of center
FrameworkElement fe = e.Source as FrameworkElement;
Point center = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
center = m.Transform(center); // 转换为矩阵缩放和旋转的中心点
// Update matrix to reflect translation/rotation
m.Translate(trans.X, trans.Y); // 移动
2021-09-25 22:58:50 +08:00
if (Settings.Gesture.IsEnableTwoFingerRotation)
{
m.RotateAt(rotate, center.X, center.Y); // 旋转
}
2021-09-25 16:05:22 +08:00
m.ScaleAt(scale.X, scale.Y, center.X, center.Y); // 缩放
2021-09-28 23:08:20 +08:00
StrokeCollection strokes = inkCanvas.GetSelectedStrokes();
if (strokes.Count != 0)
2021-09-25 16:05:22 +08:00
{
2021-09-28 23:08:20 +08:00
foreach (Stroke stroke in strokes)
{
stroke.Transform(m, false);
2021-09-25 22:58:50 +08:00
2021-10-07 00:25:12 +08:00
try
{
stroke.DrawingAttributes.Width *= md.Scale.X;
stroke.DrawingAttributes.Height *= md.Scale.Y;
}
catch { }
2021-09-28 23:08:20 +08:00
}
}
else
{
foreach (Stroke stroke in inkCanvas.Strokes)
{
stroke.Transform(m, false);
2021-10-07 00:25:12 +08:00
try
{
stroke.DrawingAttributes.Width *= md.Scale.X;
stroke.DrawingAttributes.Height *= md.Scale.Y;
}
catch { }
2021-09-28 23:08:20 +08:00
}
2021-09-25 16:05:22 +08:00
}
}
}
#endregion Touch Events
2021-09-09 13:12:34 +08:00
int currentMode = 0;
private void BtnSwitch_Click(object sender, RoutedEventArgs e)
{
2021-09-11 01:23:03 +08:00
if (Main_Grid.Background == Brushes.Transparent)
2021-09-09 13:12:34 +08:00
{
2021-09-11 01:23:03 +08:00
if (currentMode == 0)
{
currentMode++;
GridBackgroundCover.Visibility = Visibility.Visible;
2021-10-08 00:37:56 +08:00
SaveStrokes(true);
inkCanvas.Strokes.Clear();
RestoreStrokes();
2021-10-09 23:36:00 +08:00
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
BtnSwitch.Content = "黑板";
}
else
{
BtnSwitch.Content = "白板";
}
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Visible;
2021-09-11 01:23:03 +08:00
}
2021-10-08 00:48:07 +08:00
BtnHideInkCanvas_Click(BtnHideInkCanvas, e);
2021-09-11 01:23:03 +08:00
}
else
{
switch ((++currentMode) % 2)
{
2021-10-10 11:50:11 +08:00
case 0: //屏幕模式
currentMode = 0;
2021-09-11 01:23:03 +08:00
GridBackgroundCover.Visibility = Visibility.Hidden;
2021-10-08 00:37:56 +08:00
SaveStrokes();
inkCanvas.Strokes.Clear();
RestoreStrokes(true);
2021-10-08 00:37:56 +08:00
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
BtnSwitch.Content = "黑板";
}
else
{
BtnSwitch.Content = "白板";
}
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Visible;
2021-09-11 01:23:03 +08:00
break;
2021-10-10 11:50:11 +08:00
case 1: //黑板或白板模式
currentMode = 1;
2021-09-11 01:23:03 +08:00
GridBackgroundCover.Visibility = Visibility.Visible;
SaveStrokes(true);
inkCanvas.Strokes.Clear();
RestoreStrokes();
2021-10-08 00:37:56 +08:00
BtnSwitch.Content = "屏幕";
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Collapsed;
2021-09-11 01:23:03 +08:00
break;
}
}
2021-10-10 11:50:11 +08:00
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Visible;
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
2021-09-11 01:23:03 +08:00
}
private void BtnSwitchTheme_Click(object sender, RoutedEventArgs e)
{
if (BtnSwitchTheme.Content.ToString() == "深色")
{
BtnSwitchTheme.Content = "浅色";
2021-10-08 00:48:07 +08:00
if (BtnSwitch.Content.ToString() != "屏幕")
{
BtnSwitch.Content = "黑板";
}
2021-09-11 01:23:03 +08:00
BtnExit.Foreground = Brushes.White;
GridBackgroundCover.Background = new SolidColorBrush(StringToColor("#FF1A1A1A"));
BtnColorBlack.Background = Brushes.White;
2021-10-13 00:09:43 +08:00
BtnColorGreen.Background = new SolidColorBrush(StringToColor("#FF1ED760"));
BtnColorYellow.Background = new SolidColorBrush(StringToColor("#FFFFC000"));
2021-09-11 01:23:03 +08:00
ThemeManager.Current.ApplicationTheme = ApplicationTheme.Dark;
if (inkColor == 0)
{
inkCanvas.DefaultDrawingAttributes.Color = Colors.White;
}
else if (inkColor == 2)
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FF1ED760");
}
else if (inkColor == 4)
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FFFFC000");
}
2021-09-11 01:23:03 +08:00
}
else
{
BtnSwitchTheme.Content = "深色";
2021-10-08 00:48:07 +08:00
if (BtnSwitch.Content.ToString() != "屏幕")
{
BtnSwitch.Content = "白板";
}
2021-09-11 01:23:03 +08:00
BtnExit.Foreground = Brushes.Black;
GridBackgroundCover.Background = new SolidColorBrush(StringToColor("#FFF2F2F2"));
BtnColorBlack.Background = Brushes.Black;
2021-10-13 00:09:43 +08:00
BtnColorGreen.Background = new SolidColorBrush(StringToColor("#FF169141"));
BtnColorYellow.Background = new SolidColorBrush(StringToColor("#FFF38B00"));
2021-09-11 01:23:03 +08:00
ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light;
if (inkColor == 0)
{
inkCanvas.DefaultDrawingAttributes.Color = Colors.Black;
}
else if(inkColor == 2)
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FF169141");
}
else if(inkColor == 4)
{
inkCanvas.DefaultDrawingAttributes.Color = StringToColor("#FFF38B00");
}
}
AdjustStrokeColor();
if (!Settings.Appearance.IsTransparentButtonBackground)
{
ToggleSwitchTransparentButtonBackground_Toggled(ToggleSwitchTransparentButtonBackground, null);
}
}
private void AdjustStrokeColor()
{
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
foreach (Stroke stroke in inkCanvas.Strokes)
{
if (stroke.DrawingAttributes.Color == Colors.Black)
{
stroke.DrawingAttributes.Color = Colors.White;
}
else if (stroke.DrawingAttributes.Color.Equals(StringToColor("#FF169141")))
{
stroke.DrawingAttributes.Color = StringToColor("#FF1ED760");
}
else if (stroke.DrawingAttributes.Color.Equals(StringToColor("#FFF38B00")))
{
stroke.DrawingAttributes.Color = StringToColor("#FFFFC000");
}
}
2021-09-09 13:12:34 +08:00
}
else
2021-09-25 13:38:27 +08:00
{
foreach (Stroke stroke in inkCanvas.Strokes)
{
if (stroke.DrawingAttributes.Color == Colors.White)
{
stroke.DrawingAttributes.Color = Colors.Black;
}
else if (stroke.DrawingAttributes.Color.Equals(StringToColor("#FF1ED760")))
{
stroke.DrawingAttributes.Color = StringToColor("#FF169141");
}
else if (stroke.DrawingAttributes.Color.Equals(StringToColor("#FFFFC000")))
{
stroke.DrawingAttributes.Color = StringToColor("#FFF38B00");
}
}
2021-09-25 13:38:27 +08:00
}
2021-09-09 13:12:34 +08:00
}
2021-09-17 23:09:00 +08:00
int BoundsWidth = 5;
2021-09-12 18:28:26 +08:00
private void ToggleSwitchModeFinger_Toggled(object sender, RoutedEventArgs e)
2021-09-09 13:12:34 +08:00
{
2021-09-12 18:28:26 +08:00
if (ToggleSwitchModeFinger.IsOn)
2021-09-09 13:12:34 +08:00
{
BoundsWidth = 10;
}
else
{
2021-09-17 23:09:00 +08:00
BoundsWidth = 5;
2021-09-09 13:12:34 +08:00
}
}
2021-09-10 01:35:39 +08:00
private void BtnHideInkCanvas_Click(object sender, RoutedEventArgs e)
{
2021-09-25 17:32:58 +08:00
if (Main_Grid.Background == Brushes.Transparent)
2021-09-10 01:35:39 +08:00
{
Main_Grid.Background = new SolidColorBrush(StringToColor("#01FFFFFF"));
inkCanvas.Visibility = Visibility.Visible;
2021-09-11 01:23:03 +08:00
GridBackgroundCoverHolder.Visibility = Visibility.Visible;
2021-10-07 00:25:12 +08:00
GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed;
2021-10-08 00:37:56 +08:00
2021-10-08 00:48:07 +08:00
if (GridBackgroundCover.Visibility == Visibility.Hidden)
{
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
BtnSwitch.Content = "黑板";
}
else
{
BtnSwitch.Content = "白板";
}
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Visible;
2021-10-08 00:48:07 +08:00
}
else
{
BtnSwitch.Content = "屏幕";
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Collapsed;
2021-10-08 00:48:07 +08:00
}
2021-10-08 00:37:56 +08:00
2021-09-10 01:35:39 +08:00
BtnHideInkCanvas.Content = "隐藏\n画板";
}
else
{
Main_Grid.Background = Brushes.Transparent;
inkCanvas.Visibility = Visibility.Collapsed;
2021-09-11 01:23:03 +08:00
GridBackgroundCoverHolder.Visibility = Visibility.Collapsed;
2021-10-13 00:09:43 +08:00
if (currentMode != 0) SaveStrokes();
2021-10-08 00:37:56 +08:00
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
BtnSwitch.Content = "黑板";
}
else
{
BtnSwitch.Content = "白板";
}
2021-10-10 11:50:11 +08:00
StackPanelPPTButtons.Visibility = Visibility.Visible;
2021-09-10 01:35:39 +08:00
BtnHideInkCanvas.Content = "显示\n画板";
}
}
private void BtnSwitchSide_Click(object sender, RoutedEventArgs e)
{
if (ViewBoxStackPanelMain.HorizontalAlignment == HorizontalAlignment.Right)
2021-09-10 01:35:39 +08:00
{
ViewBoxStackPanelMain.HorizontalAlignment = HorizontalAlignment.Left;
2021-10-10 11:50:11 +08:00
ViewBoxStackPanelShapes.HorizontalAlignment = HorizontalAlignment.Right;
2021-09-10 01:35:39 +08:00
}
else
{
ViewBoxStackPanelMain.HorizontalAlignment = HorizontalAlignment.Right;
2021-10-10 11:50:11 +08:00
ViewBoxStackPanelShapes.HorizontalAlignment = HorizontalAlignment.Left;
2021-09-10 01:35:39 +08:00
}
}
2021-09-11 01:23:03 +08:00
2021-09-25 22:18:58 +08:00
#region PowerPoint
2021-10-13 00:09:43 +08:00
public static Microsoft.Office.Interop.PowerPoint.Application pptApplication = null;
public static Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
public static Microsoft.Office.Interop.PowerPoint.Slides slides = null;
public static Microsoft.Office.Interop.PowerPoint.Slide slide = null;
public static int slidescount = 0;
2021-09-11 01:23:03 +08:00
private void BtnCheckPPT_Click(object sender, RoutedEventArgs e)
{
2021-09-12 13:18:10 +08:00
try
{
pptApplication = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application");
2021-09-11 01:23:03 +08:00
//pptApplication.SlideShowWindows[1].View.Next();
if (pptApplication != null)
{
//获得演示文稿对象
presentation = pptApplication.ActivePresentation;
2021-09-12 13:18:10 +08:00
pptApplication.SlideShowBegin += PptApplication_SlideShowBegin;
pptApplication.SlideShowNextSlide += PptApplication_SlideShowNextSlide;
pptApplication.SlideShowEnd += PptApplication_SlideShowEnd;
2021-09-11 01:23:03 +08:00
// 获得幻灯片对象集合
slides = presentation.Slides;
// 获得幻灯片的数量
slidescount = slides.Count;
2021-09-12 13:18:10 +08:00
memoryStreams = new MemoryStream[slidescount + 2];
2021-09-11 01:23:03 +08:00
// 获得当前选中的幻灯片
try
{
// 在普通视图下这种方式可以获得当前选中的幻灯片对象
// 然而在阅读模式下,这种方式会出现异常
slide = slides[pptApplication.ActiveWindow.Selection.SlideRange.SlideNumber];
}
catch
{
// 在阅读模式下出现异常时,通过下面的方式来获得当前选中的幻灯片对象
slide = pptApplication.SlideShowWindows[1].View.Slide;
}
}
if (pptApplication == null) throw new Exception();
2021-09-12 13:18:10 +08:00
//BtnCheckPPT.Visibility = Visibility.Collapsed;
2021-09-11 01:23:03 +08:00
StackPanelPPTControls.Visibility = Visibility.Visible;
2021-09-12 13:18:10 +08:00
}
catch
{
//BtnCheckPPT.Visibility = Visibility.Visible;
StackPanelPPTControls.Visibility = Visibility.Collapsed;
MessageBox.Show("未找到幻灯片");
}
}
2021-10-13 00:39:46 +08:00
public static bool IsShowingRestoreHiddenSlidesWindow = false;
2021-09-12 13:18:10 +08:00
private void TimerCheckPPT_Elapsed(object sender, ElapsedEventArgs e)
{
2021-10-13 00:39:46 +08:00
if (IsShowingRestoreHiddenSlidesWindow) return;
2021-09-12 13:18:10 +08:00
try
{
2021-09-22 12:38:09 +08:00
Process[] processes = Process.GetProcessesByName("wpp");
if (processes.Length > 0)
{
return;
}
2021-10-07 00:25:12 +08:00
2021-10-08 00:37:56 +08:00
//使用下方提前创建 PowerPoint 实例,将导致 PowerPoint 不再有启动界面
2021-10-07 00:25:12 +08:00
//pptApplication = (Microsoft.Office.Interop.PowerPoint.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("91493441-5A91-11CF-8700-00AA0060263B")));
//new ComAwareEventInfo(typeof(EApplication_Event), "SlideShowBegin").AddEventHandler(pptApplication, new EApplication_SlideShowBeginEventHandler(this.PptApplication_SlideShowBegin));
//new ComAwareEventInfo(typeof(EApplication_Event), "SlideShowEnd").AddEventHandler(pptApplication, new EApplication_SlideShowEndEventHandler(this.PptApplication_SlideShowEnd));
//new ComAwareEventInfo(typeof(EApplication_Event), "SlideShowNextSlide").AddEventHandler(pptApplication, new EApplication_SlideShowNextSlideEventHandler(this.PptApplication_SlideShowNextSlide));
//ConfigHelper.Instance.IsInitApplicationSuccessful = true;
2021-09-22 12:38:09 +08:00
2021-09-12 13:18:10 +08:00
pptApplication = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application");
if (pptApplication != null)
{
timerCheckPPT.Stop();
//获得演示文稿对象
presentation = pptApplication.ActivePresentation;
pptApplication.PresentationClose += PptApplication_PresentationClose;
pptApplication.SlideShowBegin += PptApplication_SlideShowBegin;
pptApplication.SlideShowNextSlide += PptApplication_SlideShowNextSlide;
pptApplication.SlideShowEnd += PptApplication_SlideShowEnd;
// 获得幻灯片对象集合
slides = presentation.Slides;
2021-10-13 00:39:46 +08:00
2021-09-12 13:18:10 +08:00
// 获得幻灯片的数量
slidescount = slides.Count;
memoryStreams = new MemoryStream[slidescount + 2];
// 获得当前选中的幻灯片
try
{
// 在普通视图下这种方式可以获得当前选中的幻灯片对象
// 然而在阅读模式下,这种方式会出现异常
slide = slides[pptApplication.ActiveWindow.Selection.SlideRange.SlideNumber];
}
catch
{
// 在阅读模式下出现异常时,通过下面的方式来获得当前选中的幻灯片对象
slide = pptApplication.SlideShowWindows[1].View.Slide;
}
}
if (pptApplication == null) throw new Exception();
//BtnCheckPPT.Visibility = Visibility.Collapsed;
2021-10-13 00:09:43 +08:00
//检查是否有隐藏幻灯片
bool isHaveHiddenSlide = false;
foreach (Slide slide in slides)
{
if (slide.SlideShowTransition.Hidden == Microsoft.Office.Core.MsoTriState.msoTrue)
{
isHaveHiddenSlide = true;
break;
}
}
2021-09-12 13:18:10 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
2021-10-13 00:39:46 +08:00
if (isHaveHiddenSlide && !IsShowingRestoreHiddenSlidesWindow)
2021-10-13 00:09:43 +08:00
{
2021-10-13 00:39:46 +08:00
IsShowingRestoreHiddenSlidesWindow = true;
2021-10-13 00:09:43 +08:00
new RestoreHiddenSlidesWindow().ShowDialog();
}
2021-09-12 13:18:10 +08:00
BtnPPTSlideShow.Visibility = Visibility.Visible;
});
//如果检测到已经开始放映,则立即进入画板模式
if (pptApplication.SlideShowWindows.Count >= 1)
{
PptApplication_SlideShowBegin(pptApplication.SlideShowWindows[1]);
}
2021-09-12 13:18:10 +08:00
}
catch
{
//StackPanelPPTControls.Visibility = Visibility.Collapsed;
Application.Current.Dispatcher.Invoke(() =>
{
BtnPPTSlideShow.Visibility = Visibility.Collapsed;
});
timerCheckPPT.Start();
}
}
private void PptApplication_PresentationClose(Presentation Pres)
{
2021-09-28 23:08:20 +08:00
pptApplication = null;
2021-09-12 13:18:10 +08:00
timerCheckPPT.Start();
BtnPPTSlideShow.Visibility = Visibility.Collapsed;
BtnPPTSlideShowEnd.Visibility = Visibility.Collapsed;
}
private void PptApplication_SlideShowBegin(SlideShowWindow Wn)
{
Application.Current.Dispatcher.Invoke(() =>
{
2021-09-28 23:08:20 +08:00
slidescount = Wn.Presentation.Slides.Count;
memoryStreams = new MemoryStream[slidescount + 2];
2021-09-12 13:18:10 +08:00
StackPanelPPTControls.Visibility = Visibility.Visible;
BtnPPTSlideShow.Visibility = Visibility.Collapsed;
BtnPPTSlideShowEnd.Visibility = Visibility.Visible;
2021-09-25 22:58:50 +08:00
ViewBoxStackPanelMain.Margin = new Thickness(10, 10, 10, 10);
2021-09-12 18:28:26 +08:00
if (Settings.Behavior.IsShowCanvasAtNewSlideShow && Main_Grid.Background == Brushes.Transparent)
{
if (currentMode != 0)
{
currentMode = 0;
GridBackgroundCover.Visibility = Visibility.Hidden;
SaveStrokes();
inkCanvas.Strokes.Clear();
if (BtnSwitchTheme.Content.ToString() == "浅色")
{
BtnSwitch.Content = "黑板";
}
else
{
BtnSwitch.Content = "白板";
}
StackPanelPPTButtons.Visibility = Visibility.Visible;
}
2021-09-12 18:28:26 +08:00
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
}
//if (GridBackgroundCover.Visibility == Visibility.Visible)
//{
// SaveStrokes();
// currentMode = 0;
// GridBackgroundCover.Visibility = Visibility.Hidden;
//}
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Visible;
2021-09-25 22:58:50 +08:00
inkCanvas.Strokes.Clear();
2021-09-12 13:18:10 +08:00
});
previousSlideID = Wn.View.CurrentShowPosition;
}
private void PptApplication_SlideShowEnd(Presentation Pres)
{
Application.Current.Dispatcher.Invoke(() =>
{
BtnPPTSlideShow.Visibility = Visibility.Visible;
BtnPPTSlideShowEnd.Visibility = Visibility.Collapsed;
StackPanelPPTControls.Visibility = Visibility.Collapsed;
2021-09-25 22:58:50 +08:00
ViewBoxStackPanelMain.Margin = new Thickness(10, 10, 10, 55);
if (currentMode != 0)
{
BtnSwitch_Click(BtnSwitch, null);
}
//if (GridBackgroundCover.Visibility == Visibility.Visible)
//{
// SaveStrokes();
//}
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Visible;
2021-09-12 13:18:10 +08:00
inkCanvas.Strokes.Clear();
2021-09-12 13:18:10 +08:00
if (Main_Grid.Background != Brushes.Transparent)
{
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
}
});
}
private void Main_Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
2021-10-13 00:09:43 +08:00
if (StackPanelPPTControls.Visibility != Visibility.Visible || currentMode != 0) return;
if (e.Key == Key.Down || e.Key == Key.PageDown || e.Key == Key.Right)
{
BtnPPTSlidesDown_Click(BtnPPTSlidesDown, null);
}
if (e.Key == Key.Up || e.Key == Key.PageUp || e.Key == Key.Left)
{
BtnPPTSlidesUp_Click(BtnPPTSlidesUp, null);
}
}
2021-09-12 13:18:10 +08:00
int previousSlideID = 0;
MemoryStream[] memoryStreams = new MemoryStream[50];
private void PptApplication_SlideShowNextSlide(SlideShowWindow Wn)
{
if (Wn.View.CurrentShowPosition != previousSlideID)
{
Application.Current.Dispatcher.Invoke(() =>
{
MemoryStream ms = new MemoryStream();
inkCanvas.Strokes.Save(ms);
ms.Position = 0;
memoryStreams[previousSlideID] = ms;
2021-09-25 17:32:58 +08:00
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Visible;
2021-09-12 13:18:10 +08:00
inkCanvas.Strokes.Clear();
2021-09-12 13:18:10 +08:00
try
{
if (memoryStreams[Wn.View.CurrentShowPosition].Length > 0)
{
inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(memoryStreams[Wn.View.CurrentShowPosition]);
}
}
catch (Exception ex)
{ }
});
previousSlideID = Wn.View.CurrentShowPosition;
}
2021-09-11 01:23:03 +08:00
}
private void BtnPPTSlidesUp_Click(object sender, RoutedEventArgs e)
{
2021-09-12 13:18:10 +08:00
if (currentMode == 1)
{
GridBackgroundCover.Visibility = Visibility.Hidden;
currentMode = 0;
}
2021-09-11 01:23:03 +08:00
try
{
2021-09-25 17:32:58 +08:00
new Thread(new ThreadStart(() =>
{
2021-09-12 18:28:26 +08:00
pptApplication.SlideShowWindows[1].View.Application.SlideShowWindows[1].Activate();
pptApplication.SlideShowWindows[1].View.Previous();
})).Start();
2021-09-11 01:23:03 +08:00
}
catch
{
2021-09-12 13:18:10 +08:00
//BtnCheckPPT.Visibility = Visibility.Visible;
2021-09-11 01:23:03 +08:00
StackPanelPPTControls.Visibility = Visibility.Collapsed;
}
}
private void BtnPPTSlidesDown_Click(object sender, RoutedEventArgs e)
{
2021-09-12 13:18:10 +08:00
if (currentMode == 1)
{
GridBackgroundCover.Visibility = Visibility.Hidden;
currentMode = 0;
}
2021-09-11 01:23:03 +08:00
try
{
2021-09-25 17:32:58 +08:00
new Thread(new ThreadStart(() =>
{
2021-09-12 18:28:26 +08:00
pptApplication.SlideShowWindows[1].View.Application.SlideShowWindows[1].Activate();
pptApplication.SlideShowWindows[1].View.Next();
})).Start();
2021-09-11 01:23:03 +08:00
}
catch (Exception ex)
{
2021-09-12 13:18:10 +08:00
//BtnCheckPPT.Visibility = Visibility.Visible;
2021-09-11 01:23:03 +08:00
StackPanelPPTControls.Visibility = Visibility.Collapsed;
2021-09-12 13:18:10 +08:00
//MessageBox.Show(ex.ToString());
2021-09-11 01:23:03 +08:00
}
}
2021-09-12 13:18:10 +08:00
private void BtnPPTSlideShow_Click(object sender, RoutedEventArgs e)
{
2021-09-25 17:32:58 +08:00
new Thread(new ThreadStart(() =>
{
try
2021-09-12 18:28:26 +08:00
{
presentation.SlideShowSettings.Run();
2021-09-12 18:28:26 +08:00
}
catch { }
})).Start();
2021-09-12 13:18:10 +08:00
}
private void BtnPPTSlideShowEnd_Click(object sender, RoutedEventArgs e)
{
2021-09-25 17:32:58 +08:00
new Thread(new ThreadStart(() =>
{
2021-09-12 13:18:10 +08:00
try
{
pptApplication.SlideShowWindows[1].View.Exit();
}
catch { }
})).Start();
}
2021-09-12 18:28:26 +08:00
2021-09-25 22:18:58 +08:00
#endregion
2021-09-12 18:28:26 +08:00
#region Settings
#region Behavior
private void ToggleSwitchRunAtStartup_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
if (ToggleSwitchRunAtStartup.IsOn)
{
StartAutomaticallyCreate("InkCanvas");
}
else
{
StartAutomaticallyDel("InkCanvas");
}
}
private void ToggleSwitchSupportPowerPoint_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Behavior.PowerPointSupport = ToggleSwitchSupportPowerPoint.IsOn;
SaveSettingsToFile();
}
private void ToggleSwitchShowCanvasAtNewSlideShow_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Behavior.IsShowCanvasAtNewSlideShow = ToggleSwitchShowCanvasAtNewSlideShow.IsOn;
SaveSettingsToFile();
}
#endregion
#region Startup
private void ToggleSwitchAutoHideCanvas_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Startup.IsAutoHideCanvas = ToggleSwitchAutoHideCanvas.IsOn;
SaveSettingsToFile();
}
private void ToggleSwitchAutoEnterModeFinger_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Startup.IsAutoEnterModeFinger = ToggleSwitchAutoEnterModeFinger.IsOn;
SaveSettingsToFile();
}
#endregion
#region Appearance
private void ToggleSwitchShowButtonExit_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsShowExitButton = ToggleSwitchShowButtonExit.IsOn;
SaveSettingsToFile();
if (ToggleSwitchShowButtonExit.IsOn)
{
BtnExit.Visibility = Visibility.Visible;
}
else
{
BtnExit.Visibility = Visibility.Collapsed;
}
}
private void ToggleSwitchShowButtonEraser_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsShowEraserButton = ToggleSwitchShowButtonEraser.IsOn;
SaveSettingsToFile();
if (ToggleSwitchShowButtonEraser.IsOn)
{
BtnErase.Visibility = Visibility.Visible;
}
else
{
BtnErase.Visibility = Visibility.Collapsed;
}
}
private void ToggleSwitchShowButtonHideControl_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsShowHideControlButton = ToggleSwitchShowButtonHideControl.IsOn;
SaveSettingsToFile();
if (ToggleSwitchShowButtonHideControl.IsOn)
{
BtnHideControl.Visibility = Visibility.Visible;
}
else
{
BtnHideControl.Visibility = Visibility.Collapsed;
}
}
private void ToggleSwitchShowButtonLRSwitch_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsShowLRSwitchButton = ToggleSwitchShowButtonLRSwitch.IsOn;
SaveSettingsToFile();
if (ToggleSwitchShowButtonLRSwitch.IsOn)
{
BtnSwitchSide.Visibility = Visibility.Visible;
}
else
{
BtnSwitchSide.Visibility = Visibility.Collapsed;
}
}
private void ToggleSwitchShowButtonModeFinger_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsShowModeFingerToggleSwitch = ToggleSwitchShowButtonModeFinger.IsOn;
2021-09-12 18:28:26 +08:00
SaveSettingsToFile();
if (ToggleSwitchShowButtonModeFinger.IsOn)
2021-09-12 18:28:26 +08:00
{
StackPanelModeFinger.Visibility = Visibility.Visible;
}
else
{
StackPanelModeFinger.Visibility = Visibility.Collapsed;
}
}
#endregion
#region Canvas
private void InkWidthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!isLoaded) return;
drawingAttributes.Height = ((Slider)sender).Value / 2;
drawingAttributes.Width = ((Slider)sender).Value / 2;
Settings.Canvas.InkWidth = ((Slider)sender).Value / 2;
SaveSettingsToFile();
}
#endregion
2021-09-12 18:28:26 +08:00
private void SaveSettingsToFile()
{
string text = JsonConvert.SerializeObject(Settings, Formatting.Indented);
try
{
File.WriteAllText(settingsFileName, text);
}
catch { }
}
private void SCManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
{
e.Handled = true;
}
private void HyperlinkSource_Click(object sender, RoutedEventArgs e)
{
Process.Start("https://github.com/WXRIW/Ink-Canvas");
}
#endregion
2021-09-17 23:09:00 +08:00
public static string GetWebClient(string url)
{
HttpWebRequest myrq = (HttpWebRequest)WebRequest.Create(url);
2021-09-19 01:12:13 +08:00
myrq.Proxy = null;
2021-09-17 23:09:00 +08:00
myrq.KeepAlive = false;
myrq.Timeout = 30 * 1000;
myrq.Method = "Get";
myrq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
myrq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36";
HttpWebResponse myrp;
try
{
myrp = (HttpWebResponse)myrq.GetResponse();
}
catch (WebException ex)
{
myrp = (HttpWebResponse)ex.Response;
}
if (myrp.StatusCode != HttpStatusCode.OK)
{
return "null";
}
using (StreamReader sr = new StreamReader(myrp.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
2021-09-12 18:28:26 +08:00
#region
/// <summary>
/// 开机自启创建
/// </summary>
/// <param name="exeName">程序名称</param>
/// <returns></returns>
public bool StartAutomaticallyCreate(string exeName)
{
try
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + exeName + ".lnk");
//设置快捷方式的目标所在的位置(源程序完整路径)
shortcut.TargetPath = System.Windows.Forms.Application.ExecutablePath;
//应用程序的工作目录
//当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
//目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)
shortcut.WindowStyle = 1;
//快捷方式的描述
shortcut.Description = exeName + "_Ink";
//设置快捷键(如果有必要的话.)
//shortcut.Hotkey = "CTRL+ALT+D";
shortcut.Save();
return true;
}
catch (Exception) { }
return false;
}
/// <summary>
/// 开机自启删除
/// </summary>
/// <param name="exeName">程序名称</param>
/// <returns></returns>
public bool StartAutomaticallyDel(string exeName)
{
try
{
System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + exeName + ".lnk");
return true;
}
catch (Exception) { }
return false;
}
#endregion
2021-09-22 00:20:27 +08:00
private void Window_Closed(object sender, EventArgs e)
{
//if (!isAutoUpdateEnabled) return;
//try
//{
// if (OAUS.Core.VersionHelper.HasNewVersion(GetIp("ink.wxriw.cn"), 19570))
// {
// string updateExePath = AppDomain.CurrentDomain.BaseDirectory + "AutoUpdater\\AutoUpdater.exe";
// System.Diagnostics.Process myProcess = System.Diagnostics.Process.Start(updateExePath);
// }
//}
//catch { }
2021-09-22 00:20:27 +08:00
}
/// <summary>
/// 传入域名返回对应的IP
/// </summary>
/// <param name="domainName">域名</param>
/// <returns></returns>
public static string GetIp(string domainName)
{
domainName = domainName.Replace("http://", "").Replace("https://", "");
IPHostEntry hostEntry = Dns.GetHostEntry(domainName);
IPEndPoint ipEndPoint = new IPEndPoint(hostEntry.AddressList[0], 0);
return ipEndPoint.Address.ToString();
}
2021-09-25 13:38:27 +08:00
private void ToggleSwitchTransparentButtonBackground_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Appearance.IsTransparentButtonBackground = ToggleSwitchTransparentButtonBackground.IsOn;
if (Settings.Appearance.IsTransparentButtonBackground)
{
BtnExit.Background = new SolidColorBrush(StringToColor("#7F909090"));
}
else
{
if (BtnSwitchTheme.Content.ToString() == "深色")
{
//Light
BtnExit.Background = new SolidColorBrush(StringToColor("#FFCCCCCC"));
}
else
{
//Dark
BtnExit.Background = new SolidColorBrush(StringToColor("#FF555555"));
}
}
SaveSettingsToFile();
}
private void ToggleSwitchShowCursor_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Canvas.IsShowCursor = ToggleSwitchShowCursor.IsOn;
inkCanvas_EditingModeChanged(inkCanvas, null);
SaveSettingsToFile();
}
2021-09-25 14:14:01 +08:00
private void inkCanvas_EditingModeChanged(object sender, RoutedEventArgs e)
{
if (Settings.Canvas.IsShowCursor)
{
if (((InkCanvas)sender).EditingMode == InkCanvasEditingMode.Ink || drawingShapeMode != 0)
{
((InkCanvas)sender).ForceCursor = true;
}
else
{
((InkCanvas)sender).ForceCursor = false;
}
}
else
{
((InkCanvas)sender).ForceCursor = false;
}
}
#region Left Side Panel
2021-09-25 17:32:58 +08:00
int drawingShapeMode = 0;
2021-09-25 22:58:50 +08:00
private void BtnPen_Click(object sender, RoutedEventArgs e)
{
forceEraser = false;
drawingShapeMode = 0;
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
inkCanvas.IsManipulationEnabled = true;
}
2021-09-25 17:32:58 +08:00
private void BtnDrawLine_Click(object sender, RoutedEventArgs e)
{
forceEraser = true;
drawingShapeMode = 1;
inkCanvas.EditingMode = InkCanvasEditingMode.None;
inkCanvas.IsManipulationEnabled = true;
}
private void BtnDrawArrow_Click(object sender, RoutedEventArgs e)
{
forceEraser = true;
drawingShapeMode = 2;
inkCanvas.EditingMode = InkCanvasEditingMode.None;
inkCanvas.IsManipulationEnabled = true;
}
private void BtnDrawRectangle_Click(object sender, RoutedEventArgs e)
{
forceEraser = true;
drawingShapeMode = 3;
inkCanvas.EditingMode = InkCanvasEditingMode.None;
inkCanvas.IsManipulationEnabled = true;
}
private void BtnDrawEllipse_Click(object sender, RoutedEventArgs e)
{
forceEraser = true;
drawingShapeMode = 4;
inkCanvas.EditingMode = InkCanvasEditingMode.None;
inkCanvas.IsManipulationEnabled = true;
}
private void inkCanvas_TouchMove(object sender, TouchEventArgs e)
{
2021-10-10 11:50:11 +08:00
if (isSingleFingerDragMode) return;
2021-09-25 17:32:58 +08:00
if (drawingShapeMode != 0)
{
2021-09-26 12:56:41 +08:00
if (isLastTouchEraser)
{
2021-10-09 23:36:00 +08:00
//if (inkCanvas.EditingMode != InkCanvasEditingMode.EraseByStroke)
//{
// inkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
//}
//MessageBox.Show(inkCanvas.EditingMode.ToString());
//if (inkCanvas.EditingMode == InkCanvasEditingMode.EraseByPoint || inkCanvas.EditingMode == InkCanvasEditingMode.EraseByStroke)
//{
//}
//double boundsWidth = e.GetTouchPoint(null).Bounds.Width;
//if (boundsWidth > BoundsWidth * 1.7)
//{
// inkCanvas.EraserShape = new EllipseStylusShape(boundsWidth * 1.5, boundsWidth * 1.5);
// inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
//}
//else
//{
// inkCanvas.EraserShape = new RectangleStylusShape(8, 8);
// //inkCanvas.EraserShape = new EllipseStylusShape(boundsWidth * 1.5, boundsWidth * 1.5);
// inkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
//}
2021-09-26 12:56:41 +08:00
return;
}
2021-09-25 17:32:58 +08:00
if (isWaitUntilNextTouchDown) return;
if (dec.Count > 1)
{
isWaitUntilNextTouchDown = true;
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
catch { }
return;
}
if (inkCanvas.EditingMode != InkCanvasEditingMode.None)
{
inkCanvas.EditingMode = InkCanvasEditingMode.None;
}
}
MouseTouchMove(e.GetTouchPoint(inkCanvas).Position);
}
private void MouseTouchMove(Point endP)
{
//System.Windows.Point endP = e.GetTouchPoint(inkCanvas).Position;
List<System.Windows.Point> pointList;
StylusPointCollection point;
Stroke stroke;
switch (drawingShapeMode)
{
case 1:
pointList = new List<System.Windows.Point>{
new System.Windows.Point(iniP.X, iniP.Y),
new System.Windows.Point(endP.X, endP.Y)
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
catch { }
lastTempStroke = stroke;
inkCanvas.Strokes.Add(stroke);
break;
case 2:
double w = 30, h = 10;
double theta = Math.Atan2(iniP.Y - endP.Y, iniP.X - endP.X);
double sint = Math.Sin(theta);
double cost = Math.Cos(theta);
pointList = new List<Point>
{
new Point(iniP.X, iniP.Y),
new Point(endP.X , endP.Y),
new Point(endP.X + (w*cost - h*sint), endP.Y + (w*sint + h*cost)),
new Point(endP.X,endP.Y),
new Point(endP.X + (w*cost + h*sint), endP.Y - (h*cost - w*sint))
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
catch { }
lastTempStroke = stroke;
inkCanvas.Strokes.Add(stroke);
break;
case 3:
pointList = new List<System.Windows.Point>{
new System.Windows.Point(iniP.X, iniP.Y),
new System.Windows.Point(iniP.X, endP.Y),
new System.Windows.Point(endP.X, endP.Y),
new System.Windows.Point(endP.X, iniP.Y),
new System.Windows.Point(iniP.X, iniP.Y)
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
catch { }
lastTempStroke = stroke;
inkCanvas.Strokes.Add(stroke);
break;
case 4:
pointList = GenerateEclipseGeometry(iniP, endP);
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
catch { }
lastTempStroke = stroke;
inkCanvas.Strokes.Add(stroke);
break;
}
}
private void Main_Grid_TouchUp(object sender, TouchEventArgs e)
{
lastTempStroke = null;
if (dec.Count == 0)
{
isWaitUntilNextTouchDown = false;
}
}
Stroke lastTempStroke = null; bool isWaitUntilNextTouchDown = false;
private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed)
{
double a = 0.5 * (ed.X - st.X);
double b = 0.5 * (ed.Y - st.Y);
List<System.Windows.Point> pointList = new List<System.Windows.Point>();
for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
{
pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
}
return pointList;
}
bool isMouseDown = false;
private void inkCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
iniP = e.GetPosition(inkCanvas);
isMouseDown = true;
}
private void inkCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
MouseTouchMove(e.GetPosition(inkCanvas));
}
}
private void inkCanvas_MouseUp(object sender, MouseButtonEventArgs e)
{
lastTempStroke = null;
isMouseDown = false;
}
2021-10-10 11:50:11 +08:00
private void BtnPenWidthDecrease_Click(object sender, RoutedEventArgs e)
{
try
{
InkWidthSlider.Value -= 1;
}
catch { }
}
private void BtnPenWidthIncrease_Click(object sender, RoutedEventArgs e)
{
try
{
InkWidthSlider.Value += 1;
}
catch { }
}
2021-09-25 22:58:50 +08:00
#region Selection Gestures
2021-09-28 23:08:20 +08:00
private void BtnSelect_Click(object sender, RoutedEventArgs e)
{
forceEraser = true;
drawingShapeMode = 0;
inkCanvas.EditingMode = InkCanvasEditingMode.Select;
inkCanvas.IsManipulationEnabled = false;
}
private void inkCanvas_SelectionChanged(object sender, EventArgs e)
{
if (inkCanvas.GetSelectedStrokes().Count == 0)
{
GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed;
}
else
{
GridInkCanvasSelectionCover.Visibility = Visibility.Visible;
//GridInkCanvasSelectionCover.Height = inkCanvas.GetSelectionBounds().Height;
//GridInkCanvasSelectionCover.Width = inkCanvas.GetSelectionBounds().Width;
//GridInkCanvasSelectionCover.Margin = new Thickness(inkCanvas.GetSelectionBounds().Left, inkCanvas.GetSelectionBounds().Top, 0, 0);
}
}
private void GridInkCanvasSelectionCover_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.Mode = ManipulationModes.All;
}
private void GridInkCanvasSelectionCover_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
}
private void GridInkCanvasSelectionCover_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (dec.Count >= 1)
{
ManipulationDelta md = e.DeltaManipulation;
Vector trans = md.Translation; // 获得位移矢量
double rotate = md.Rotation; // 获得旋转角度
Vector scale = md.Scale; // 获得缩放倍数
Matrix m = new Matrix();
// Find center of element and then transform to get current location of center
FrameworkElement fe = e.Source as FrameworkElement;
Point center = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
2021-10-07 00:25:12 +08:00
center = new Point(inkCanvas.GetSelectionBounds().Left + inkCanvas.GetSelectionBounds().Width / 2,
inkCanvas.GetSelectionBounds().Top + inkCanvas.GetSelectionBounds().Height / 2);
2021-09-28 23:08:20 +08:00
center = m.Transform(center); // 转换为矩阵缩放和旋转的中心点
// Update matrix to reflect translation/rotation
m.Translate(trans.X, trans.Y); // 移动
if (Settings.Gesture.IsEnableTwoFingerRotation)
{
m.RotateAt(rotate, center.X, center.Y); // 旋转
}
m.ScaleAt(scale.X, scale.Y, center.X, center.Y); // 缩放
StrokeCollection strokes = inkCanvas.GetSelectedStrokes();
foreach (Stroke stroke in strokes)
{
stroke.Transform(m, false);
2021-10-07 00:25:12 +08:00
try
{
stroke.DrawingAttributes.Width *= md.Scale.X;
stroke.DrawingAttributes.Height *= md.Scale.Y;
}
catch { }
2021-09-28 23:08:20 +08:00
}
}
}
private void GridInkCanvasSelectionCover_TouchDown(object sender, TouchEventArgs e)
{
}
private void GridInkCanvasSelectionCover_TouchUp(object sender, TouchEventArgs e)
{
}
Point lastTouchPointOnGridInkCanvasCover = new Point(0, 0);
private void GridInkCanvasSelectionCover_PreviewTouchDown(object sender, TouchEventArgs e)
{
dec.Add(e.TouchDevice.Id);
//设备1个的时候记录中心点
if (dec.Count == 1)
{
TouchPoint touchPoint = e.GetTouchPoint(null);
centerPoint = touchPoint.Position;
lastTouchPointOnGridInkCanvasCover = touchPoint.Position;
}
////设备两个及两个以上,将画笔功能关闭
//if (dec.Count > 1)
//{
// if (inkCanvas.EditingMode != InkCanvasEditingMode.None && inkCanvas.EditingMode != InkCanvasEditingMode.Select)
// {
// lastInkCanvasEditingMode = inkCanvas.EditingMode;
// inkCanvas.EditingMode = InkCanvasEditingMode.None;
// }
//}
}
private void GridInkCanvasSelectionCover_PreviewTouchUp(object sender, TouchEventArgs e)
{
if (lastTouchPointOnGridInkCanvasCover == e.GetTouchPoint(null).Position)
{
if (lastTouchPointOnGridInkCanvasCover.X < inkCanvas.GetSelectionBounds().Left ||
lastTouchPointOnGridInkCanvasCover.Y < inkCanvas.GetSelectionBounds().Top ||
lastTouchPointOnGridInkCanvasCover.X > inkCanvas.GetSelectionBounds().Right ||
2021-09-28 23:08:20 +08:00
lastTouchPointOnGridInkCanvasCover.Y > inkCanvas.GetSelectionBounds().Bottom)
{
inkCanvas.Select(new StrokeCollection());
}
}
////手势完成后切回之前的状态
//if (dec.Count > 1)
//{
// if (inkCanvas.EditingMode == InkCanvasEditingMode.None)
// {
// inkCanvas.EditingMode = lastInkCanvasEditingMode;
// }
//}
dec.Remove(e.TouchDevice.Id);
}
#endregion Selection Gestures
#endregion Left Side Panel
private void ToggleSwitchEnableTwoFingerRotation_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Gesture.IsEnableTwoFingerRotation = ToggleSwitchEnableTwoFingerRotation.IsOn;
SaveSettingsToFile();
}
private void ToggleSwitchEnableTwoFingerGestureInPresentationMode_Toggled(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
Settings.Gesture.IsEnableTwoFingerGestureInPresentationMode = ToggleSwitchEnableTwoFingerGestureInPresentationMode.IsOn;
SaveSettingsToFile();
}
private void BtnResetToSuggestion_Click(object sender, RoutedEventArgs e)
{
try
{
isLoaded = false;
Settings = new Settings();
Settings.Appearance.IsShowEraserButton = false;
Settings.Appearance.IsShowExitButton = false;
Settings.Startup.IsAutoHideCanvas = true;
SaveSettingsToFile();
LoadSettings(false);
isLoaded = true;
if (ToggleSwitchRunAtStartup.IsOn == false)
{
ToggleSwitchRunAtStartup.IsOn = true;
}
}
catch { }
SymbolIconResetSuggestionComplete.Visibility = Visibility.Visible;
new Thread(new ThreadStart(() => {
Thread.Sleep(5000);
Application.Current.Dispatcher.Invoke(() =>
{
SymbolIconResetSuggestionComplete.Visibility = Visibility.Collapsed;
});
})).Start();
}
private void BtnResetToDefault_Click(object sender, RoutedEventArgs e)
{
try
{
isLoaded = false;
File.Delete("settings.json");
Settings = new Settings();
LoadSettings(false);
isLoaded = true;
}
catch { }
SymbolIconResetDefaultComplete.Visibility = Visibility.Visible;
new Thread(new ThreadStart(() => {
Thread.Sleep(5000);
Application.Current.Dispatcher.Invoke(() =>
{
SymbolIconResetDefaultComplete.Visibility = Visibility.Collapsed;
});
})).Start();
}
#region Whiteboard Controls
int CurrentWhiteboardIndex = 1;
int WhiteboardTotalCount = 1;
2021-10-11 00:17:27 +08:00
MemoryStream[] WhiteboardStrokesStreams = new MemoryStream[101]; //最多99页0用来存储非白板时的墨迹以便还原
private void SaveStrokes(bool isBackupMain = false)
{
MemoryStream ms = new MemoryStream();
inkCanvas.Strokes.Save(ms);
ms.Position = 0;
if (isBackupMain)
{
WhiteboardStrokesStreams[0] = ms;
}
else
{
WhiteboardStrokesStreams[CurrentWhiteboardIndex] = ms;
}
}
private void RestoreStrokes(bool isBackupMain = false)
{
try
{
if (isBackupMain)
{
if (WhiteboardStrokesStreams[0].Length > 0)
{
inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(WhiteboardStrokesStreams[0]);
}
}
else
{
if (WhiteboardStrokesStreams[CurrentWhiteboardIndex].Length > 0)
{
inkCanvas.Strokes = new System.Windows.Ink.StrokeCollection(WhiteboardStrokesStreams[CurrentWhiteboardIndex]);
}
}
AdjustStrokeColor();
}
catch { }
}
private void BtnWhiteBoardSwitchPrevious_Click(object sender, RoutedEventArgs e)
{
if (CurrentWhiteboardIndex <= 1) return;
SaveStrokes();
inkCanvas.Strokes.Clear();
CurrentWhiteboardIndex--;
RestoreStrokes();
UpdateIndexInfoDisplay();
}
private void BtnWhiteBoardSwitchNext_Click(object sender, RoutedEventArgs e)
{
if (CurrentWhiteboardIndex >= WhiteboardTotalCount) return;
SaveStrokes();
inkCanvas.Strokes.Clear();
CurrentWhiteboardIndex++;
RestoreStrokes();
UpdateIndexInfoDisplay();
}
private void BtnWhiteBoardAdd_Click(object sender, RoutedEventArgs e)
{
SaveStrokes();
inkCanvas.Strokes.Clear();
WhiteboardTotalCount++;
CurrentWhiteboardIndex++;
if (CurrentWhiteboardIndex != WhiteboardTotalCount)
{
for (int i = WhiteboardTotalCount; i > CurrentWhiteboardIndex; i--)
{
WhiteboardStrokesStreams[i] = WhiteboardStrokesStreams[i - 1];
}
}
WhiteboardStrokesStreams[CurrentWhiteboardIndex] = new MemoryStream();
UpdateIndexInfoDisplay();
2021-10-10 11:50:11 +08:00
if (WhiteboardTotalCount >= 99) BtnWhiteBoardAdd.IsEnabled = false;
}
private void BtnWhiteBoardDelete_Click(object sender, RoutedEventArgs e)
{
inkCanvas.Strokes.Clear();
if (CurrentWhiteboardIndex != WhiteboardTotalCount)
{
for (int i = CurrentWhiteboardIndex; i <= WhiteboardTotalCount; i++)
{
WhiteboardStrokesStreams[i] = WhiteboardStrokesStreams[i + 1];
}
}
else
{
CurrentWhiteboardIndex--;
}
WhiteboardTotalCount--;
RestoreStrokes();
UpdateIndexInfoDisplay();
2021-10-10 11:50:11 +08:00
if (WhiteboardTotalCount < 99) BtnWhiteBoardAdd.IsEnabled = true;
}
private void UpdateIndexInfoDisplay()
{
2021-10-10 11:50:11 +08:00
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Visible;
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
TextBlockWhiteBoardIndexInfo.Text = string.Format("{0} / {1}", CurrentWhiteboardIndex, WhiteboardTotalCount);
if (CurrentWhiteboardIndex == 1)
{
BtnWhiteBoardSwitchPrevious.IsEnabled = false;
}
else
{
BtnWhiteBoardSwitchPrevious.IsEnabled = true;
}
if (CurrentWhiteboardIndex == WhiteboardTotalCount)
{
BtnWhiteBoardSwitchNext.IsEnabled = false;
}
else
{
BtnWhiteBoardSwitchNext.IsEnabled = true;
}
if (WhiteboardTotalCount == 1)
{
BtnWhiteBoardDelete.IsEnabled = false;
}
else
{
BtnWhiteBoardDelete.IsEnabled = true;
}
}
#endregion Whiteboard Controls
2021-10-13 00:09:43 +08:00
//此函数中的所有代码版权所有 WXRIW在其他项目中使用前必须提前联系wxriw@outlook.com谢谢
2021-10-10 11:50:11 +08:00
private void inkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
2021-10-13 00:09:43 +08:00
//Label.Visibility = Visibility.Visible;
//Label.Content = e.Stroke.StylusPoints.Count.ToString();
//if (!ToggleSwitchSimulatePressure.IsOn) return;
int mode = 1;
switch (mode)
{
2021-10-13 00:09:43 +08:00
case 0:
try
{
StylusPointCollection stylusPoints = new StylusPointCollection();
int n = e.Stroke.StylusPoints.Count - 1;
string s = "";
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
for (int i = 0; i <= n; i++)
{
double speed = GetPointSpeed(e.Stroke.StylusPoints[Math.Max(i - 1, 0)].ToPoint(), e.Stroke.StylusPoints[i].ToPoint(), e.Stroke.StylusPoints[Math.Min(i + 1, n)].ToPoint());
s += speed.ToString() + "\t";
StylusPoint point = new StylusPoint();
if (speed >= 0.25)
{
point.PressureFactor = (float)(0.5 - 0.3 * (Math.Min(speed, 1.5) - 0.3) / 1.2);
}
else if (speed >= 0.05)
{
point.PressureFactor = (float)0.5;
}
else
{
point.PressureFactor = (float)(0.5 + 0.4 * (0.05 - speed) / 0.05);
}
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
//Label.Visibility = Visibility.Visible;
//Label.Content = s;
e.Stroke.StylusPoints = stylusPoints;
}
catch
2021-10-11 00:17:27 +08:00
{
2021-10-13 00:09:43 +08:00
2021-10-11 00:17:27 +08:00
}
2021-10-13 00:09:43 +08:00
break;
case 1:
try
2021-10-10 11:50:11 +08:00
{
2021-10-13 00:09:43 +08:00
StylusPointCollection stylusPoints = new StylusPointCollection();
int n = e.Stroke.StylusPoints.Count - 1;
double pressure = 0.1;
int x = 8;
if(n >= x)
{
for (int i = 0; i < n - x; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)0.5;
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
for (int i = n - x; i <= n; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)((0.5 - pressure) * (n - i) / x + pressure);
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
}
else
{
for (int i = 0; i <= n; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)(0.4 * (n - i) / n + pressure);
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
}
e.Stroke.StylusPoints = stylusPoints;
2021-10-10 11:50:11 +08:00
}
2021-10-13 00:09:43 +08:00
catch
2021-10-10 11:50:11 +08:00
{
2021-10-13 00:09:43 +08:00
2021-10-10 11:50:11 +08:00
}
2021-10-13 00:09:43 +08:00
break;
case 2:
try
{
StylusPointCollection stylusPoints = new StylusPointCollection();
int n = e.Stroke.StylusPoints.Count - 1;
double pressure = 0.1;
int x = 8;
if (lastTouchDownTime < lastTouchUpTime)
{
double k = (lastTouchUpTime - lastTouchDownTime) / (n + 1); // 每个点之间间隔 k 毫秒
Label.Visibility = Visibility.Visible;
Label.Content = k.ToString();
x = (int)(1000 / k); // 取 1000 ms 内的点
}
if(n >= x)
{
for (int i = 0; i < n - x; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)0.5;
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
for (int i = n - x; i <= n; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)((0.5 - pressure) * (n - i) / x + pressure);
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
}
else
{
for (int i = 0; i <= n; i++)
{
StylusPoint point = new StylusPoint();
point.PressureFactor = (float)(0.4 * (n - i) / n + pressure);
point.X = e.Stroke.StylusPoints[i].X;
point.Y = e.Stroke.StylusPoints[i].Y;
stylusPoints.Add(point);
}
}
e.Stroke.StylusPoints = stylusPoints;
}
catch
{
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
}
break;
}
}
2021-10-10 11:50:11 +08:00
public double GetPointSpeed(Point point1, Point point2, Point point3)
{
return (Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y))
+ Math.Sqrt((point3.X - point2.X) * (point3.X - point2.X) + (point3.Y - point2.Y) * (point3.Y - point2.Y)))
/ 20;
}
private void BtnFingerDragMode_Click(object sender, RoutedEventArgs e)
{
if (isSingleFingerDragMode)
{
isSingleFingerDragMode = false;
BtnFingerDragMode.Content = "单指\n拖动";
}
else
{
isSingleFingerDragMode = true;
BtnFingerDragMode.Content = "多指\n拖动";
}
}
StrokeCollection[] strokeCollections = new StrokeCollection[100];
bool[] whiteboadLastModeIsRedo = new bool[100];
int currentStrokeCollectionIndex = 0;
StrokeCollection lastTouchDownStrokeCollection = new StrokeCollection();
private void BtnUndo_Click(object sender, RoutedEventArgs e)
{
int whiteboardIndex = CurrentWhiteboardIndex;
if (currentMode == 0)
{
whiteboardIndex = 0;
}
StrokeCollection strokes = inkCanvas.Strokes.Clone();
inkCanvas.Strokes = strokeCollections[whiteboardIndex].Clone();
strokeCollections[whiteboardIndex] = strokes;
BtnRedo.IsEnabled = true;
BtnRedo.Visibility = Visibility.Visible;
BtnUndo.IsEnabled = false;
BtnUndo.Visibility = Visibility.Collapsed;
}
private void BtnRedo_Click(object sender, RoutedEventArgs e)
{
int whiteboardIndex = CurrentWhiteboardIndex;
if (currentMode == 0)
{
whiteboardIndex = 0;
}
StrokeCollection strokes = inkCanvas.Strokes.Clone();
inkCanvas.Strokes = strokeCollections[whiteboardIndex].Clone();
strokeCollections[whiteboardIndex] = strokes;
BtnUndo.IsEnabled = true;
BtnUndo.Visibility = Visibility.Visible;
BtnRedo.IsEnabled = false;
BtnRedo.Visibility = Visibility.Collapsed;
}
private void Btn_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
try
{
2021-10-10 11:50:11 +08:00
if (((Button)sender).IsEnabled)
{
((StackPanel)((Button)sender).Content).Opacity = 1;
}
else
{
((StackPanel)((Button)sender).Content).Opacity = 0.2;
}
}
catch { }
}
private void ToggleSwitchSimulatePressure_Toggled(object sender, RoutedEventArgs e)
{
}
2021-10-13 00:09:43 +08:00
private void GridInkCanvasSelectionCover_MouseUp(object sender, MouseButtonEventArgs e)
{
GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed;
}
private void BtnClear_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//BtnClear_Click(BtnClear, null);
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
}
2020-11-22 11:05:12 +08:00
}
enum HotkeyModifiers
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}
2021-10-10 11:50:11 +08:00
#region Test for pen
// A StylusPlugin that renders ink with a linear gradient brush effect.
class CustomDynamicRenderer : DynamicRenderer
{
2021-10-13 00:09:43 +08:00
[ThreadStatic]
static private Brush brush = null;
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
[ThreadStatic]
static private Pen pen = null;
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
private Point prevPoint;
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
protected override void OnStylusDown(RawStylusInput rawStylusInput)
2021-10-10 11:50:11 +08:00
{
2021-10-13 00:09:43 +08:00
// Allocate memory to store the previous point to draw from.
prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
base.OnStylusDown(rawStylusInput);
}
//protected override void OnDraw(System.Windows.Media.DrawingContext drawingContext, System.Windows.Input.StylusPointCollection stylusPoints, System.Windows.Media.Geometry geometry, System.Windows.Media.Brush fillBrush)
//{
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// ImageSource img = new BitmapImage(new Uri("pack://application:,,,/Resources/maobi.png"));
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// //前一个点的绘制。
// Point prevPoint = new Point(double.NegativeInfinity,
// double.NegativeInfinity);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// var w = Global.StrokeWidth + 15; //输出时笔刷的实际大小
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// Point pt = new Point(0, 0);
// Vector v = new Vector(); //前一个点与当前点的距离
// var subtractY = 0d; //当前点处前一点的Y偏移
// var subtractX = 0d; //当前点处前一点的X偏移
// var pointWidth = Global.StrokeWidth;
// double x = 0, y = 0;
// for (int i = 0; i < stylusPoints.Count; i++)
// {
// pt = (Point)stylusPoints[i];
// v = Point.Subtract(prevPoint, pt);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// Debug.WriteLine("X " + pt.X + "\t" + pt.Y);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// subtractY = (pt.Y - prevPoint.Y) / v.Length; //设置stylusPoints两个点之间需要填充的XY偏移
// subtractX = (pt.X - prevPoint.X) / v.Length;
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// if (w - v.Length < Global.StrokeWidth) //控制笔刷大小
// {
// pointWidth = Global.StrokeWidth;
// }
// else
// {
// pointWidth = w - v.Length; //在两个点距离越大的时候,笔刷所展示的大小越小
// }
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// for (double j = 0; j < v.Length; j = j + 1d) //填充stylusPoints两个点之间
// {
// x = 0; y = 0;
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// if (prevPoint.X == double.NegativeInfinity || prevPoint.Y == double.NegativeInfinity || double.PositiveInfinity == prevPoint.X || double.PositiveInfinity == prevPoint.Y)
// {
// y = pt.Y;
// x = pt.X;
// }
// else
// {
// y = prevPoint.Y + subtractY;
// x = prevPoint.X + subtractX;
// }
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// drawingContext.DrawImage(img, new Rect(x - pointWidth / 2, y - pointWidth / 2, pointWidth, pointWidth)); //在当前点画笔刷图片
// prevPoint = new Point(x, y);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// if (double.IsNegativeInfinity(v.Length) || double.IsPositiveInfinity(v.Length))
// { break; }
2021-10-10 11:50:11 +08:00
// }
// }
2021-10-13 00:09:43 +08:00
// stylusPoints = null;
2021-10-10 11:50:11 +08:00
//}
2021-10-13 00:09:43 +08:00
protected override void OnDraw(DrawingContext drawingContext,
StylusPointCollection stylusPoints,
Geometry geometry, Brush fillBrush)
{
// Create a new Brush, if necessary.
//brush ??= new LinearGradientBrush(Colors.Red, Colors.Blue, 20d);
// Create a new Pen, if necessary.
//pen ??= new Pen(brush, 2d);
// Draw linear gradient ellipses between
// all the StylusPoints that have come in.
for (int i = 0; i < stylusPoints.Count; i++)
{
Point pt = (Point)stylusPoints[i];
Vector v = Point.Subtract(prevPoint, pt);
// Only draw if we are at least 4 units away
// from the end of the last ellipse. Otherwise,
// we're just redrawing and wasting cycles.
if (v.Length > 4)
{
// Set the thickness of the stroke based
// on how hard the user pressed.
double radius = stylusPoints[i].PressureFactor * 10d;
drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
prevPoint = pt;
}
}
}
2021-10-10 11:50:11 +08:00
}
public class Global
{
public static double StrokeWidth = 2.5;
}
public class CustomRenderingInkCanvas : InkCanvas
{
CustomDynamicRenderer customRenderer = new CustomDynamicRenderer();
public CustomRenderingInkCanvas() : base()
{
// Use the custom dynamic renderer on the
// custom InkCanvas.
this.DynamicRenderer = customRenderer;
}
2021-10-13 00:09:43 +08:00
protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
{
// Remove the original stroke and add a custom stroke.
this.Strokes.Remove(e.Stroke);
CustomStroke customStroke = new CustomStroke(e.Stroke.StylusPoints);
this.Strokes.Add(customStroke);
// Pass the custom stroke to base class' OnStrokeCollected method.
InkCanvasStrokeCollectedEventArgs args =
new InkCanvasStrokeCollectedEventArgs(customStroke);
base.OnStrokeCollected(args);
}
2021-10-10 11:50:11 +08:00
}// A class for rendering custom strokes
class CustomStroke : Stroke
{
Brush brush;
Pen pen;
public CustomStroke(StylusPointCollection stylusPoints)
: base(stylusPoints)
{
// Create the Brush and Pen used for drawing.
brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20d);
pen = new Pen(brush, 2d);
}
//protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
//{
// ImageSource img = new BitmapImage(new Uri("pack://application:,,,/Resources/maobi.png"));
// //前一个点的绘制。
// Point prevPoint = new Point(double.NegativeInfinity,
// double.NegativeInfinity);
// var w = Global.StrokeWidth + 15; //输出时笔刷的实际大小
// Point pt = new Point(0, 0);
// Vector v = new Vector(); //前一个点与当前点的距离
// var subtractY = 0d; //当前点处前一点的Y偏移
// var subtractX = 0d; //当前点处前一点的X偏移
// var pointWidth = Global.StrokeWidth;
// double x = 0, y = 0;
// for (int i = 0; i < stylusPoints.Count; i++)
// {
// pt = (Point)stylusPoints[i];
// v = Point.Subtract(prevPoint, pt);
// Debug.WriteLine("X " + pt.X + "\t" + pt.Y);
// subtractY = (pt.Y - prevPoint.Y) / v.Length; //设置stylusPoints两个点之间需要填充的XY偏移
// subtractX = (pt.X - prevPoint.X) / v.Length;
// if (w - v.Length < Global.StrokeWidth) //控制笔刷大小
// {
// pointWidth = Global.StrokeWidth;
// }
// else
// {
// pointWidth = w - v.Length; //在两个点距离越大的时候,笔刷所展示的大小越小
// }
// for (double j = 0; j < v.Length; j = j + 1d) //填充stylusPoints两个点之间
// {
// x = 0; y = 0;
// if (prevPoint.X == double.NegativeInfinity || prevPoint.Y == double.NegativeInfinity || double.PositiveInfinity == prevPoint.X || double.PositiveInfinity == prevPoint.Y)
// {
// y = pt.Y;
// x = pt.X;
// }
// else
// {
// y = prevPoint.Y + subtractY;
// x = prevPoint.X + subtractX;
// }
// drawingContext.DrawImage(img, new Rect(x - pointWidth / 2, y - pointWidth / 2, pointWidth, pointWidth)); //在当前点画笔刷图片
// prevPoint = new Point(x, y);
// if (double.IsNegativeInfinity(v.Length) || double.IsPositiveInfinity(v.Length))
// { break; }
// }
// }
// stylusPoints = null;
//}
2021-10-13 00:09:43 +08:00
protected override void DrawCore(DrawingContext drawingContext,
DrawingAttributes drawingAttributes)
{
// Allocate memory to store the previous point to draw from.
Point prevPoint = new Point(double.NegativeInfinity,
double.NegativeInfinity);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// Draw linear gradient ellipses between
// all the StylusPoints in the Stroke.
for (int i = 0; i < this.StylusPoints.Count; i++)
{
Point pt = (Point)this.StylusPoints[i];
Vector v = Point.Subtract(prevPoint, pt);
2021-10-10 11:50:11 +08:00
2021-10-13 00:09:43 +08:00
// Only draw if we are at least 4 units away
// from the end of the last ellipse. Otherwise,
// we're just redrawing and wasting cycles.
if (v.Length > 4)
{
// Set the thickness of the stroke
// based on how hard the user pressed.
double radius = this.StylusPoints[i].PressureFactor * 10d;
drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
prevPoint = pt;
}
}
}
2021-10-10 11:50:11 +08:00
}
#endregion
2020-11-22 11:05:12 +08:00
}