StickyNotePopup.xaml.cs 13 KB

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