StickyNotePopup.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using ComPDFKitViewer;
  2. using PDF_Office.CustomControl.CompositeControl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace PDF_Office.Views.PropertyPanel.AnnotPanel
  20. {
  21. /// <summary>
  22. /// StickyNotePopup.xaml 的交互逻辑
  23. /// </summary>
  24. public class GripThumb : Thumb
  25. {
  26. private Pen DrawPen;
  27. private Brush FillBrush;
  28. public GripThumb()
  29. {
  30. DefaultStyleKey = typeof(GripThumb);
  31. DrawPen = new Pen(new SolidColorBrush(Colors.Blue), 2);
  32. FillBrush = new SolidColorBrush(Color.FromArgb(0x0A, 0xFF, 0xFF, 0xFF));
  33. }
  34. protected override void OnRender(DrawingContext DrawContext)
  35. {
  36. DrawContext.DrawRectangle(FillBrush, null, new Rect(0, 0, Width, Height));
  37. DrawContext.DrawLine(DrawPen, new Point(Width, 0), new Point(0, Height));
  38. DrawContext.DrawLine(DrawPen, new Point(Width, Height / 3), new Point(Width / 3, Height));
  39. DrawContext.DrawLine(DrawPen, new Point(Width, Height * 2 / 3), new Point(Width * 2 / 3, Height));
  40. }
  41. }
  42. /// <summary>
  43. /// StickyNotePopup.xaml 的交互逻辑
  44. /// </summary>
  45. public partial class StickyNotePopup : StickyPopupExt
  46. {
  47. private ObservableCollection<ColorItem> colors = new ObservableCollection<ColorItem>();
  48. private Point PressPoint;
  49. public Point OffsetParent;
  50. private byte saveOpacity = 1;
  51. public StickyNotePopup()
  52. {
  53. InitializeComponent();
  54. AddHandler(MouseUpEvent, new MouseButtonEventHandler(StickyPopupControl_MouseUp), true);
  55. ContentText.AddHandler(MouseDownEvent, new MouseButtonEventHandler(StickyPopupControl_MouseDown), true);
  56. Loaded += StickyPopupControl_Loaded;
  57. ContentText.GotFocus += ContentText_GotFocus;
  58. ContentText.LostFocus += ContentText_LostFocus;
  59. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x10)));
  60. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0x10, 0x10)));
  61. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0xFF, 0x10)));
  62. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0x70, 0xFF)));
  63. ListColor.ItemsSource = colors;
  64. }
  65. private void ContentText_LostFocus(object sender, RoutedEventArgs e)
  66. {
  67. e.Handled = true;
  68. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  69. bkColor.A = saveOpacity;
  70. border.BorderBrush = new SolidColorBrush(bkColor);
  71. CloseText_MouseUp(this, null);
  72. }
  73. private void ContentText_GotFocus(object sender, RoutedEventArgs e)
  74. {
  75. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  76. saveOpacity = bkColor.A;
  77. bkColor.A = 255;
  78. border.BorderBrush = new SolidColorBrush(bkColor);
  79. this.Background = new SolidColorBrush(this.StickyColor);
  80. }
  81. private void StickyPopupControl_Loaded(object sender, RoutedEventArgs e)
  82. {
  83. ContentText.Focus();
  84. ContentText.CaretIndex = ContentText.Text.Length;
  85. }
  86. private void StickyPopupControl_MouseDown(object sender, MouseButtonEventArgs e)
  87. {
  88. PressPoint = new Point(0, 0);
  89. }
  90. private void StickyPopupControl_MouseUp(object sender, MouseButtonEventArgs e)
  91. {
  92. CanMove = true;
  93. GripControl.ReleaseMouseCapture();
  94. }
  95. public string StickyText
  96. {
  97. get
  98. {
  99. return ContentText.Text;
  100. }
  101. set
  102. {
  103. ContentText.Text = value;
  104. }
  105. }
  106. public string StickyAuthor
  107. {
  108. get
  109. {
  110. return AuthorText.Text;
  111. }
  112. set
  113. {
  114. AuthorText.Text = value;
  115. }
  116. }
  117. public string StickyDate
  118. {
  119. get
  120. {
  121. return DateText.Text;
  122. }
  123. set
  124. {
  125. DateText.Text = value;
  126. }
  127. }
  128. public bool CanMove { get; set; } = true;
  129. private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
  130. {
  131. e.Handled = true;
  132. if (e.LeftButton == MouseButtonState.Pressed)
  133. {
  134. Point currentPos = e.GetPosition(ContainerElement);
  135. double newWidth = GridUi.ActualWidth + currentPos.X - PressPoint.X;
  136. double newHeight = GridUi.ActualHeight + currentPos.Y - PressPoint.Y;
  137. if (Left + newWidth >= ContainerElement.ActualWidth)
  138. {
  139. newWidth = ContainerElement.ActualWidth - Left - 5;
  140. }
  141. if (Top + newHeight >= ContainerElement.ActualHeight)
  142. {
  143. newHeight = ContainerElement.ActualHeight - Top - 5;
  144. }
  145. GridUi.Width = newWidth;
  146. GridUi.Height = newHeight;
  147. PressPoint = currentPos;
  148. CanMove = false;
  149. if (GripControl.IsMouseCaptured == false)
  150. {
  151. GripControl.CaptureMouse();
  152. }
  153. }
  154. }
  155. private void CloseText_MouseUp(object sender, MouseButtonEventArgs e)
  156. {
  157. if (e != null)
  158. {
  159. e.Handled = true;
  160. }
  161. PlaceChange = null;
  162. RemoveFromLayer();
  163. if (Closed != null)
  164. {
  165. Closed.Invoke(sender, EventArgs.Empty);
  166. }
  167. }
  168. protected override void OnMouseDown(MouseButtonEventArgs e)
  169. {
  170. e.Handled = true;
  171. PressPoint = e.GetPosition(ContainerElement);
  172. }
  173. protected override void OnMouseEnter(MouseEventArgs e)
  174. {
  175. e.Handled = true;
  176. if (PlaceChange != null)
  177. {
  178. PlaceChange.Invoke(this, new EventArgs());
  179. }
  180. }
  181. protected override void OnMouseLeave(MouseEventArgs e)
  182. {
  183. e.Handled = true;
  184. if (PlaceChange != null)
  185. {
  186. PlaceChange.Invoke(null, new EventArgs());
  187. }
  188. PressPoint = new Point(0, 0);
  189. }
  190. protected override void OnMouseUp(MouseButtonEventArgs e)
  191. {
  192. CanMove = true;
  193. if (PlaceChange != null)
  194. {
  195. PlaceChange.Invoke(this, new EventArgs());
  196. }
  197. PressPoint = new Point(0, 0);
  198. GripControl.ReleaseMouseCapture();
  199. }
  200. protected override void OnMouseMove(MouseEventArgs e)
  201. {
  202. e.Handled = true;
  203. if (e.LeftButton == MouseButtonState.Pressed && e.Source.GetType() != typeof(TextBox) && e.Source.GetType() != typeof(GripThumb))
  204. {
  205. if (CanMove == false || (PressPoint.X == 0 && PressPoint.Y == 0))
  206. {
  207. return;
  208. }
  209. Point currentPoint = e.GetPosition(ContainerElement);
  210. double newLeft = Left + currentPoint.X - PressPoint.X;
  211. double newTop = Top + currentPoint.Y - PressPoint.Y;
  212. if (newLeft < 0)
  213. {
  214. newLeft = 0;
  215. }
  216. if (newLeft + GridUi.ActualWidth > ContainerElement.ActualWidth)
  217. {
  218. newLeft = ContainerElement.ActualWidth - GridUi.ActualWidth;
  219. }
  220. if (newTop < 0)
  221. {
  222. newTop = 0;
  223. }
  224. if (newTop + GridUi.ActualHeight > ContainerElement.ActualHeight)
  225. {
  226. newTop = ContainerElement.ActualHeight - GridUi.ActualHeight;
  227. }
  228. Left = newLeft;
  229. Top = newTop;
  230. PressPoint = currentPoint;
  231. if (PlaceChange != null)
  232. {
  233. PlaceChange.Invoke(null, new EventArgs());
  234. }
  235. RefreshPosition();
  236. }
  237. }
  238. public override void SetAuthor(string author)
  239. {
  240. base.SetAuthor(author);
  241. StickyAuthor = author;
  242. }
  243. public override void SetDateText(string dateText)
  244. {
  245. base.SetDateText(dateText);
  246. StickyDate = dateText;
  247. }
  248. public override void SetStickyColor(Color newColor)
  249. {
  250. base.SetStickyColor(newColor);
  251. border.BorderBrush = new SolidColorBrush(newColor);
  252. }
  253. public override void SetStickyNote(string note)
  254. {
  255. base.SetStickyNote(note);
  256. StickyText = note;
  257. }
  258. public override void SetReadOnly(bool readOnly)
  259. {
  260. base.SetReadOnly(readOnly);
  261. ContentText.IsReadOnly = readOnly;
  262. }
  263. public override string GetText()
  264. {
  265. return StickyText;
  266. }
  267. private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
  268. {
  269. var color = ListColor.SelectedItem as ColorItem;
  270. if (color != null)
  271. {
  272. this.SetStickyColor((color.Color as SolidColorBrush).Color);
  273. border.BorderBrush = new SolidColorBrush((color.Color as SolidColorBrush).Color);
  274. this.Background = new SolidColorBrush((color.Color as SolidColorBrush).Color);
  275. }
  276. }
  277. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  278. {
  279. ContentText.Text = "";
  280. this.SetStickyNote("");
  281. Closed.Invoke(sender, EventArgs.Empty);
  282. }
  283. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  284. {
  285. var frame = e.OriginalSource as Control;
  286. if(frame != null)
  287. {
  288. var color = frame.Background as SolidColorBrush;
  289. if(color != null && color.Color == Colors.Transparent)
  290. Closed.Invoke(sender, EventArgs.Empty);
  291. }
  292. }
  293. }
  294. }