StickyNotePopup.xaml.cs 15 KB

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