CustomPanel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using PDF_Office.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. namespace PDF_Office.CustomControl.ScanViewControl
  12. {
  13. public class CustomPanel : Panel
  14. {
  15. public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xCC, 0xFF, 0xFF, 0xFF));
  16. public bool DrawCover { get; set; } = false;
  17. public Rect DrawRect { get; set; }
  18. private WriteableBitmap BackgroundImage = null;
  19. private int BackgroundWidth = 0;
  20. private int BackgroundHeight = 0;
  21. private Point OffsetPos = new Point(0, 0);
  22. private double ScaleRate = 1;
  23. private CustomDraw HoverChild = null;
  24. public TextBlock ParentBlock;
  25. private int RawWidth = 0;
  26. private int RawHeight = 0;
  27. /// <summary>
  28. /// 展示用的背景图
  29. /// </summary>
  30. public WriteableBitmap BGImage
  31. {
  32. get { return (WriteableBitmap)GetValue(BGImageProperty); }
  33. set
  34. {
  35. SetValue(BGImageProperty, value);
  36. }
  37. }
  38. // Using a DependencyProperty as the backing store for BGImage. This enables animation, styling, binding, etc...
  39. public static readonly DependencyProperty BGImageProperty =
  40. DependencyProperty.Register("BGImage", typeof(WriteableBitmap), typeof(CustomPanel), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnImageChanged)));
  41. private static void OnImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  42. {
  43. if ((e.NewValue as WriteableBitmap) != null)
  44. {
  45. (d as CustomPanel).SetBackgroundImage(e.NewValue as WriteableBitmap);
  46. }
  47. }
  48. /// <summary>
  49. /// 当前页的OCR结果
  50. /// </summary>
  51. public List<KeyValuePair<Rect, string>> OCRTextRectList
  52. {
  53. get { return (List<KeyValuePair<Rect, string>>)GetValue(OCRTextRectListProperty); }
  54. set { SetValue(OCRTextRectListProperty, value); }
  55. }
  56. // Using a DependencyProperty as the backing store for OCRTextRectList. This enables animation, styling, binding, etc...
  57. public static readonly DependencyProperty OCRTextRectListProperty =
  58. DependencyProperty.Register("OCRTextRectList", typeof(List<KeyValuePair<Rect, string>>), typeof(CustomPanel), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnOCRChanged)));
  59. private static void OnOCRChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  60. {
  61. if ((e.NewValue as List<KeyValuePair<Rect, string>>) != null)
  62. {
  63. (d as CustomPanel).SetTextsRects(e.NewValue as List<KeyValuePair<Rect, string>>);
  64. }
  65. }
  66. private List<CustomDraw> VisualControlList = new List<CustomDraw>();
  67. private DrawRect RectObject { get; set; } = new DrawRect();
  68. public CustomPanel()
  69. {
  70. AddLogicalChild(RectObject);
  71. AddVisualChild(RectObject);
  72. }
  73. protected override int VisualChildrenCount
  74. {
  75. get
  76. {
  77. return VisualControlList.Count + 1;
  78. }
  79. }
  80. protected override Visual GetVisualChild(int index)
  81. {
  82. if (index == VisualControlList.Count)
  83. {
  84. return RectObject;
  85. }
  86. return VisualControlList[index];
  87. }
  88. protected override void OnRender(DrawingContext dc)
  89. {
  90. DrawRect = new Rect(0, 0, ActualWidth, ActualHeight);
  91. dc.DrawRectangle(Brushes.LightGray, null, DrawRect);
  92. DrawBackground(dc);
  93. }
  94. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  95. {
  96. double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
  97. int drawWidth = (int)(scale * BackgroundWidth);
  98. int drawHeight = (int)(scale * BackgroundHeight);
  99. if (drawWidth < 10 || drawHeight < 10)
  100. {
  101. return;
  102. }
  103. int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
  104. int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
  105. foreach (CustomDraw drawVisual in VisualControlList)
  106. {
  107. Rect paintRect = drawVisual.RawRect;
  108. drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
  109. (int)(paintRect.Top * scale + offsetY),
  110. (int)(paintRect.Width * scale),
  111. (int)(paintRect.Height * scale));
  112. drawVisual.Draw();
  113. }
  114. }
  115. private void DrawBackground(DrawingContext dc)
  116. {
  117. if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
  118. {
  119. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  120. int drawWidth = (int)(scale * BackgroundWidth);
  121. int drawHeight = (int)(scale * BackgroundHeight);
  122. if (drawWidth < 10 || drawHeight < 10)
  123. {
  124. return;
  125. }
  126. ScaleRate = scale;
  127. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  128. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  129. DrawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
  130. dc.DrawImage(BackgroundImage, DrawRect);
  131. if (DrawCover)
  132. {
  133. dc.DrawRectangle(CoverBrush, null, DrawRect);
  134. }
  135. }
  136. }
  137. private void CleanChild()
  138. {
  139. foreach (DrawingVisual child in VisualControlList)
  140. {
  141. RemoveLogicalChild(child);
  142. RemoveVisualChild(child);
  143. }
  144. VisualControlList.Clear();
  145. }
  146. public void SetBackgroundImage(WriteableBitmap bgImage)
  147. {
  148. DrawCover = false;
  149. BackgroundImage = bgImage;
  150. BackgroundWidth = 0;
  151. BackgroundHeight = 0;
  152. RawWidth = 0;
  153. RawHeight = 0;
  154. OffsetPos = new Point(0, 0);
  155. CleanChild();
  156. if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
  157. {
  158. RawWidth = BackgroundImage.PixelWidth;
  159. RawHeight = BackgroundImage.PixelHeight;
  160. BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
  161. BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
  162. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  163. int drawWidth = (int)(scale * BackgroundWidth);
  164. int drawHeight = (int)(scale * BackgroundHeight);
  165. if (drawWidth < 10 || drawHeight < 10)
  166. {
  167. return;
  168. }
  169. ScaleRate = scale;
  170. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  171. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  172. InvalidateVisual();
  173. }
  174. }
  175. /// <summary>
  176. /// 设置绘制内容
  177. /// </summary>
  178. public void DisplayContent(CustomDraw custom)
  179. {
  180. if (custom != null)
  181. {
  182. if (HoverChild == null)
  183. {
  184. HoverChild = custom;
  185. HoverChild.DrawBounds();
  186. if (ParentBlock != null)
  187. {
  188. ParentBlock.Text = HoverChild.PaintText;
  189. }
  190. }
  191. else
  192. {
  193. if (HoverChild != custom)
  194. {
  195. HoverChild?.Draw();
  196. HoverChild = custom;
  197. HoverChild.DrawBounds();
  198. }
  199. }
  200. }
  201. else
  202. {
  203. if (HoverChild != null)
  204. {
  205. HoverChild.Draw();
  206. HoverChild = null;
  207. }
  208. }
  209. }
  210. public int GetBackgroundImageWidth()
  211. {
  212. if (BackgroundImage != null)
  213. {
  214. return RawWidth;
  215. }
  216. return 0;
  217. }
  218. public int GetBackgroundImageHeight()
  219. {
  220. if (BackgroundImage != null)
  221. {
  222. return RawHeight;
  223. }
  224. return 0;
  225. }
  226. /// <summary>
  227. /// 设置文字与文字矩形
  228. /// </summary>
  229. public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
  230. {
  231. CleanChild();
  232. foreach (KeyValuePair<Rect, string> textRect in textsRect)
  233. {
  234. CustomDraw drawVisual = new CustomDraw();
  235. drawVisual.PaintText = textRect.Value;
  236. Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
  237. drawVisual.RawRect = paintRect;
  238. drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
  239. (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
  240. (int)(paintRect.Width * ScaleRate),
  241. (int)(paintRect.Height * ScaleRate));
  242. drawVisual.Draw();
  243. VisualControlList.Add(drawVisual);
  244. AddLogicalChild(drawVisual);
  245. AddVisualChild(drawVisual);
  246. }
  247. }
  248. public void RePaint()
  249. {
  250. InvalidateVisual();
  251. }
  252. public void SetRect(Rect newRect)
  253. {
  254. RectObject.SetRect(newRect);
  255. }
  256. public bool RectMouseMove(Point clickPoint)
  257. {
  258. return RectObject.MouseMove(clickPoint);
  259. }
  260. public bool RectMouseLeftButtonUp(Point clickPoint)
  261. {
  262. return RectObject.MouseLeftButtonUp(clickPoint);
  263. }
  264. public bool RectMouseLeftButtonDown(Point clickPoint)
  265. {
  266. return RectObject.MouseLeftButtonDown(clickPoint,this);
  267. }
  268. public void RectSetMaxRect(Rect rect)
  269. {
  270. RectObject.SetMaxRect(rect);
  271. }
  272. public void RectDraw()
  273. {
  274. RectObject.Draw();
  275. }
  276. }
  277. }