StickyNotePopup.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.CustomControl.CompositeControl;
  5. using PDF_Master.ViewModels.PropertyPanel.AnnotPanel;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Markup;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace PDF_Master.Views.PropertyPanel.AnnotPanel
  25. {
  26. /// <summary>
  27. /// StickyNotePopup.xaml 的交互逻辑
  28. /// </summary>
  29. public class GripThumb : Thumb
  30. {
  31. private Pen DrawPen;
  32. private Brush FillBrush;
  33. public GripThumb()
  34. {
  35. DefaultStyleKey = typeof(GripThumb);
  36. DrawPen = new Pen(new SolidColorBrush(Colors.Blue), 2);
  37. FillBrush = new SolidColorBrush(Color.FromArgb(0x0A, 0xFF, 0xFF, 0xFF));
  38. }
  39. protected override void OnRender(DrawingContext DrawContext)
  40. {
  41. DrawContext.DrawRectangle(FillBrush, null, new Rect(0, 0, Width, Height));
  42. DrawContext.DrawLine(DrawPen, new Point(Width, 0), new Point(0, Height));
  43. DrawContext.DrawLine(DrawPen, new Point(Width, Height / 3), new Point(Width / 3, Height));
  44. DrawContext.DrawLine(DrawPen, new Point(Width, Height * 2 / 3), new Point(Width * 2 / 3, Height));
  45. }
  46. }
  47. /// <summary>
  48. /// StickyNotePopup.xaml 的交互逻辑
  49. /// </summary>
  50. public partial class StickyNotePopup : StickyPopupExt
  51. {
  52. private ObservableCollection<ColorItem> colors = new ObservableCollection<ColorItem>();
  53. private Point PressPoint;
  54. public Point OffsetParent;
  55. private byte saveOpacity = 1;
  56. public StickyNotePopup()
  57. {
  58. InitializeComponent();
  59. AddHandler(MouseUpEvent, new MouseButtonEventHandler(StickyPopupControl_MouseUp), true);
  60. ContentText.AddHandler(MouseDownEvent, new MouseButtonEventHandler(StickyPopupControl_MouseDown), true);
  61. Loaded += StickyPopupControl_Loaded;
  62. ContentText.GotFocus += ContentText_GotFocus;
  63. ContentText.LostFocus += ContentText_LostFocus;
  64. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x10)));
  65. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0x10, 0x10)));
  66. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0xFF, 0x10)));
  67. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0x70, 0xFF)));
  68. ListColor.ItemsSource = colors;
  69. }
  70. private void ContentText_LostFocus(object sender, RoutedEventArgs e)
  71. {
  72. e.Handled = true;
  73. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  74. bkColor.A = saveOpacity;
  75. border.BorderBrush = new SolidColorBrush(bkColor);
  76. this.Background = new SolidColorBrush(Colors.Transparent);
  77. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  78. //为了点击某控件之后就直接关闭窗口
  79. var ui = Mouse.DirectlyOver as FrameworkElement;
  80. if (ui != null)
  81. {
  82. var colorItem = ui.DataContext as ColorItem;
  83. if (colorItem != null || ui.DataContext == null)
  84. {
  85. e.Handled = false;
  86. return;
  87. }
  88. }
  89. (DataContext as StickyNotePopupViewModel).ContentTextLostFocus.Execute(sender);
  90. CloseText_MouseUp(this, null);
  91. }
  92. private void ContentText_GotFocus(object sender, RoutedEventArgs e)
  93. {
  94. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  95. saveOpacity = bkColor.A;
  96. bkColor.A = 255;
  97. border.BorderBrush = new SolidColorBrush(bkColor);
  98. this.Background = new SolidColorBrush(Colors.Transparent);
  99. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  100. }
  101. private void StickyPopupControl_Loaded(object sender, RoutedEventArgs e)
  102. {
  103. ContentText.Focus();
  104. ContentText.CaretIndex = ContentText.Text.Length;
  105. LoadedColor();
  106. }
  107. public void LoadedColor()
  108. {
  109. if (colors != null && colors.Count > 0)
  110. {
  111. BtnDelete.DataContext = colors[0];//为了避免点击删除按钮之后,先执行取消焦点函数后,就没法执行点击事件
  112. if (GetCurrentAnnot != null)
  113. {
  114. foreach (var item in colors)
  115. {
  116. var colorItem = (item.Color as SolidColorBrush).Color;
  117. if (colorItem.A == GetCurrentAnnot.Color.A &&
  118. colorItem.R == GetCurrentAnnot.Color.R &&
  119. colorItem.G == GetCurrentAnnot.Color.G &&
  120. colorItem.B == GetCurrentAnnot.Color.B
  121. )
  122. {
  123. ListColor.SelectedItem = item;
  124. return;
  125. }
  126. }
  127. }
  128. ListColor.SelectedItem = null;
  129. }
  130. }
  131. private void StickyPopupControl_MouseDown(object sender, MouseButtonEventArgs e)
  132. {
  133. PressPoint = new Point(0, 0);
  134. }
  135. private void StickyPopupControl_MouseUp(object sender, MouseButtonEventArgs e)
  136. {
  137. CanMove = true;
  138. GripControl.ReleaseMouseCapture();
  139. }
  140. public string StickyText
  141. {
  142. get
  143. {
  144. return ContentText.Text;
  145. }
  146. set
  147. {
  148. ContentText.Text = value;
  149. }
  150. }
  151. public string StickyAuthor
  152. {
  153. get
  154. {
  155. return AuthorText.Text;
  156. }
  157. set
  158. {
  159. AuthorText.Text = value;
  160. }
  161. }
  162. public string StickyDate
  163. {
  164. get
  165. {
  166. return DateText.Text;
  167. }
  168. set
  169. {
  170. DateText.Text = value;
  171. }
  172. }
  173. public StickyAnnotArgs GetCurrentAnnot { get; set; }
  174. public CPDFViewer GetPDFViewer { get; set; }
  175. public bool CanMove { get; set; } = true;
  176. private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
  177. {
  178. e.Handled = true;
  179. if (e.LeftButton == MouseButtonState.Pressed)
  180. {
  181. Point currentPos = e.GetPosition(ContainerElement);
  182. double newWidth = GridUi.ActualWidth + currentPos.X - PressPoint.X;
  183. double newHeight = GridUi.ActualHeight + currentPos.Y - PressPoint.Y;
  184. if (Left + newWidth >= ContainerElement.ActualWidth)
  185. {
  186. newWidth = ContainerElement.ActualWidth - Left - 5;
  187. }
  188. if (Top + newHeight >= ContainerElement.ActualHeight)
  189. {
  190. newHeight = ContainerElement.ActualHeight - Top - 5;
  191. }
  192. GridUi.Width = newWidth;
  193. GridUi.Height = newHeight;
  194. PressPoint = currentPos;
  195. CanMove = false;
  196. if (GripControl.IsMouseCaptured == false)
  197. {
  198. GripControl.CaptureMouse();
  199. }
  200. }
  201. }
  202. private void CloseText_MouseUp(object sender, MouseButtonEventArgs e)
  203. {
  204. if (e != null)
  205. {
  206. e.Handled = true;
  207. }
  208. PlaceChange = null;
  209. RemoveFromLayer();
  210. if (Closed != null)
  211. {
  212. Closed.Invoke(sender, EventArgs.Empty);
  213. }
  214. }
  215. protected override void OnMouseDown(MouseButtonEventArgs e)
  216. {
  217. e.Handled = true;
  218. PressPoint = e.GetPosition(ContainerElement);
  219. }
  220. protected override void OnMouseEnter(MouseEventArgs e)
  221. {
  222. e.Handled = true;
  223. if (PlaceChange != null)
  224. {
  225. PlaceChange.Invoke(this, new EventArgs());
  226. }
  227. }
  228. protected override void OnMouseLeave(MouseEventArgs e)
  229. {
  230. e.Handled = true;
  231. if (PlaceChange != null)
  232. {
  233. PlaceChange.Invoke(null, new EventArgs());
  234. }
  235. PressPoint = new Point(0, 0);
  236. }
  237. protected override void OnMouseUp(MouseButtonEventArgs e)
  238. {
  239. CanMove = true;
  240. if (PlaceChange != null)
  241. {
  242. PlaceChange.Invoke(this, new EventArgs());
  243. }
  244. PressPoint = new Point(0, 0);
  245. GripControl.ReleaseMouseCapture();
  246. }
  247. protected override void OnMouseMove(MouseEventArgs e)
  248. {
  249. e.Handled = true;
  250. if (e.LeftButton == MouseButtonState.Pressed && e.Source.GetType() != typeof(TextBox) && e.Source.GetType() != typeof(GripThumb))
  251. {
  252. if (CanMove == false || (PressPoint.X == 0 && PressPoint.Y == 0) || ContainerElement == null)
  253. {
  254. return;
  255. }
  256. Point currentPoint = e.GetPosition(ContainerElement);
  257. double newLeft = Left + currentPoint.X - PressPoint.X;
  258. double newTop = Top + currentPoint.Y - PressPoint.Y;
  259. if (newLeft < 0)
  260. {
  261. newLeft = 0;
  262. }
  263. if (newLeft + GridUi.ActualWidth > ContainerElement.ActualWidth)
  264. {
  265. newLeft = ContainerElement.ActualWidth - GridUi.ActualWidth;
  266. }
  267. if (newTop < 0)
  268. {
  269. newTop = 0;
  270. }
  271. if (newTop + GridUi.ActualHeight > ContainerElement.ActualHeight)
  272. {
  273. newTop = ContainerElement.ActualHeight - GridUi.ActualHeight;
  274. }
  275. Left = newLeft;
  276. Top = newTop;
  277. PressPoint = currentPoint;
  278. if (PlaceChange != null)
  279. {
  280. PlaceChange.Invoke(null, new EventArgs());
  281. }
  282. RefreshPosition();
  283. }
  284. }
  285. public override void SetAuthor(string author)
  286. {
  287. base.SetAuthor(author);
  288. StickyAuthor = author;
  289. }
  290. public override void SetDateText(string dateText)
  291. {
  292. base.SetDateText(dateText);
  293. if (Regex.IsMatch(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  294. {
  295. string dateStr = Regex.Match(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  296. dateText = dateStr.Substring(4, 2)+ "/" + dateStr.Substring(6, 2) +"/" + dateStr.Substring(0, 4) + " " + dateStr.Substring(8, 2) + ":" +
  297. dateStr.Substring(10, 2) + ":" + dateStr.Substring(12, 2);
  298. }
  299. StickyDate = dateText;
  300. }
  301. public override void SetStickyColor(Color newColor)
  302. {
  303. base.SetStickyColor(newColor);
  304. border.BorderBrush = new SolidColorBrush(newColor);
  305. this.Background = new SolidColorBrush(Colors.Transparent);
  306. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  307. }
  308. public override void SetStickyNote(string note)
  309. {
  310. base.SetStickyNote(note);
  311. StickyText = note;
  312. }
  313. public override void SetReadOnly(bool readOnly)
  314. {
  315. base.SetReadOnly(readOnly);
  316. ContentText.IsReadOnly = readOnly;
  317. }
  318. public override string GetText()
  319. {
  320. return StickyText;
  321. }
  322. private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
  323. {
  324. var colorItem = ListColor.SelectedItem as ColorItem;
  325. if (colorItem != null)
  326. {
  327. var color = (colorItem.Color as SolidColorBrush).Color;
  328. this.SetStickyColor(color);
  329. var annot = GetCurrentAnnot as StickyAnnotArgs;
  330. if (annot != null)
  331. {
  332. annot.Color = color;
  333. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  334. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, color);
  335. AnnotEvent?.UpdateAnnot();
  336. }
  337. border.BorderBrush = new SolidColorBrush(color);
  338. this.Background = new SolidColorBrush(Colors.Transparent);
  339. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  340. }
  341. }
  342. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  343. {
  344. if (GetPDFViewer != null)
  345. {
  346. if (GetCurrentAnnot != null)
  347. GetPDFViewer.RemovePageAnnot(GetCurrentAnnot.PageIndex, GetCurrentAnnot.AnnotIndex);
  348. // Closed.Invoke(sender, EventArgs.Empty);
  349. CloseText_MouseUp(this, null);
  350. }
  351. }
  352. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  353. {
  354. var frame = e.OriginalSource as Control;
  355. if (frame != null)
  356. {
  357. var color = frame.Background as SolidColorBrush;
  358. if (color != null && color.Color == Colors.Transparent)
  359. Closed.Invoke(sender, EventArgs.Empty);
  360. }
  361. }
  362. private void StickyPopupExt_LostFocus(object sender, RoutedEventArgs e)
  363. {
  364. }
  365. }
  366. }