StickyNotePopup.xaml.cs 13 KB

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