CustomPanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using PDF_Master.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_Master.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. public bool IsShowRect
  67. {
  68. get { return (bool)GetValue(IsShowRectProperty); }
  69. set { SetValue(IsShowRectProperty, value); }
  70. }
  71. // Using a DependencyProperty as the backing store for IsShowRect. This enables animation, styling, binding, etc...
  72. public static readonly DependencyProperty IsShowRectProperty =
  73. DependencyProperty.Register("IsShowRect", typeof(bool), typeof(CustomPanel), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnShowRectChanged)));
  74. private static void OnShowRectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  75. {
  76. if (Convert.ToBoolean(e.NewValue)==false)
  77. {
  78. (d as CustomPanel).SetRect(new Rect(0,0,0,0));
  79. (d as CustomPanel).RectDraw();
  80. }
  81. }
  82. private List<CustomDraw> VisualControlList = new List<CustomDraw>();
  83. private DrawRect RectObject { get; set; } = new DrawRect();
  84. public CustomPanel()
  85. {
  86. AddLogicalChild(RectObject);
  87. AddVisualChild(RectObject);
  88. }
  89. protected override int VisualChildrenCount
  90. {
  91. get
  92. {
  93. return VisualControlList.Count + 1;
  94. }
  95. }
  96. protected override Visual GetVisualChild(int index)
  97. {
  98. if (index == VisualControlList.Count)
  99. {
  100. return RectObject;
  101. }
  102. return VisualControlList[index];
  103. }
  104. protected override void OnRender(DrawingContext dc)
  105. {
  106. DrawRect = new Rect(0, 0, ActualWidth, ActualHeight);
  107. dc.DrawRectangle(Brushes.LightGray, null, DrawRect);
  108. DrawBackground(dc);
  109. }
  110. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  111. {
  112. double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
  113. int drawWidth = (int)(scale * BackgroundWidth);
  114. int drawHeight = (int)(scale * BackgroundHeight);
  115. if (drawWidth < 10 || drawHeight < 10)
  116. {
  117. return;
  118. }
  119. int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
  120. int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
  121. foreach (CustomDraw drawVisual in VisualControlList)
  122. {
  123. Rect paintRect = drawVisual.RawRect;
  124. drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
  125. (int)(paintRect.Top * scale + offsetY),
  126. (int)(paintRect.Width * scale),
  127. (int)(paintRect.Height * scale));
  128. drawVisual.Draw();
  129. }
  130. }
  131. private void DrawBackground(DrawingContext dc)
  132. {
  133. if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
  134. {
  135. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  136. int drawWidth = (int)(scale * BackgroundWidth);
  137. int drawHeight = (int)(scale * BackgroundHeight);
  138. if (drawWidth < 10 || drawHeight < 10)
  139. {
  140. return;
  141. }
  142. ScaleRate = scale;
  143. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  144. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  145. DrawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
  146. dc.DrawImage(BackgroundImage, DrawRect);
  147. if (DrawCover)
  148. {
  149. dc.DrawRectangle(CoverBrush, null, DrawRect);
  150. }
  151. }
  152. }
  153. private void CleanChild()
  154. {
  155. foreach (DrawingVisual child in VisualControlList)
  156. {
  157. RemoveLogicalChild(child);
  158. RemoveVisualChild(child);
  159. }
  160. VisualControlList.Clear();
  161. }
  162. public void SetBackgroundImage(WriteableBitmap bgImage)
  163. {
  164. DrawCover = false;
  165. BackgroundImage = bgImage;
  166. BackgroundWidth = 0;
  167. BackgroundHeight = 0;
  168. RawWidth = 0;
  169. RawHeight = 0;
  170. OffsetPos = new Point(0, 0);
  171. CleanChild();
  172. if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
  173. {
  174. RawWidth = BackgroundImage.PixelWidth;
  175. RawHeight = BackgroundImage.PixelHeight;
  176. BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
  177. BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
  178. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  179. int drawWidth = (int)(scale * BackgroundWidth);
  180. int drawHeight = (int)(scale * BackgroundHeight);
  181. if (drawWidth < 10 || drawHeight < 10)
  182. {
  183. return;
  184. }
  185. ScaleRate = scale;
  186. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  187. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  188. InvalidateVisual();
  189. }
  190. }
  191. /// <summary>
  192. /// 设置绘制内容
  193. /// </summary>
  194. public void DisplayContent(CustomDraw custom)
  195. {
  196. if (custom != null)
  197. {
  198. if (HoverChild == null)
  199. {
  200. HoverChild = custom;
  201. HoverChild.DrawBounds();
  202. if (ParentBlock != null)
  203. {
  204. ParentBlock.Text = HoverChild.PaintText;
  205. }
  206. }
  207. else
  208. {
  209. if (HoverChild != custom)
  210. {
  211. HoverChild?.Draw();
  212. HoverChild = custom;
  213. HoverChild.DrawBounds();
  214. }
  215. }
  216. }
  217. else
  218. {
  219. if (HoverChild != null)
  220. {
  221. HoverChild.Draw();
  222. HoverChild = null;
  223. }
  224. }
  225. }
  226. public int GetBackgroundImageWidth()
  227. {
  228. if (BackgroundImage != null)
  229. {
  230. return RawWidth;
  231. }
  232. return 0;
  233. }
  234. public int GetBackgroundImageHeight()
  235. {
  236. if (BackgroundImage != null)
  237. {
  238. return RawHeight;
  239. }
  240. return 0;
  241. }
  242. /// <summary>
  243. /// 设置文字与文字矩形
  244. /// </summary>
  245. public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
  246. {
  247. CleanChild();
  248. foreach (KeyValuePair<Rect, string> textRect in textsRect)
  249. {
  250. CustomDraw drawVisual = new CustomDraw();
  251. drawVisual.PaintText = textRect.Value;
  252. Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
  253. drawVisual.RawRect = paintRect;
  254. drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
  255. (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
  256. (int)(paintRect.Width * ScaleRate),
  257. (int)(paintRect.Height * ScaleRate));
  258. drawVisual.Draw();
  259. VisualControlList.Add(drawVisual);
  260. AddLogicalChild(drawVisual);
  261. AddVisualChild(drawVisual);
  262. }
  263. }
  264. public void RePaint()
  265. {
  266. InvalidateVisual();
  267. }
  268. public void SetRect(Rect newRect)
  269. {
  270. RectObject.SetRect(newRect);
  271. }
  272. public bool RectMouseMove(Point clickPoint)
  273. {
  274. return RectObject.MouseMove(clickPoint);
  275. }
  276. public bool RectMouseLeftButtonUp(Point clickPoint)
  277. {
  278. return RectObject.MouseLeftButtonUp(clickPoint);
  279. }
  280. public bool RectMouseLeftButtonDown(Point clickPoint)
  281. {
  282. return RectObject.MouseLeftButtonDown(clickPoint, this);
  283. }
  284. public void RectSetMaxRect(Rect rect)
  285. {
  286. RectObject.SetMaxRect(rect);
  287. }
  288. public void RectDraw()
  289. {
  290. RectObject.Draw();
  291. }
  292. public void ClearDraw()
  293. {
  294. RectObject.ClearDraw();
  295. }
  296. public Rect GetClientRect()
  297. {
  298. return RectObject.GetClientRect();
  299. }
  300. }
  301. }