CustomPanel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. class CustomPanel: Panel
  14. {
  15. public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xA0, 0xFF, 0xFF, 0xFF));
  16. public bool DrawCover { get; set; } = false;
  17. private WriteableBitmap BackgroundImage = null;
  18. private int BackgroundWidth = 0;
  19. private int BackgroundHeight = 0;
  20. private Point OffsetPos = new Point(0, 0);
  21. private double ScaleRate = 1;
  22. private CustomDraw HoverChild = null;
  23. public TextBlock ParentBlock;
  24. private int RawWidth = 0;
  25. private int RawHeight = 0;
  26. private List<CustomDraw> VisualControlList = new List<CustomDraw>();
  27. protected override int VisualChildrenCount => VisualControlList.Count;
  28. protected override Visual GetVisualChild(int index)
  29. {
  30. return VisualControlList[index];
  31. }
  32. protected override void OnRender(DrawingContext dc)
  33. {
  34. Rect drawRect = new Rect(0, 0, ActualWidth, ActualHeight);
  35. dc.DrawRectangle(Brushes.LightGray, null, drawRect);
  36. DrawBackground(dc);
  37. }
  38. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  39. {
  40. double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
  41. scale = Math.Min(scale, 1);
  42. int drawWidth = (int)(scale * BackgroundWidth);
  43. int drawHeight = (int)(scale * BackgroundHeight);
  44. if (drawWidth < 10 || drawHeight < 10)
  45. {
  46. return;
  47. }
  48. int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
  49. int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
  50. foreach (CustomDraw drawVisual in VisualControlList)
  51. {
  52. Rect paintRect = drawVisual.RawRect;
  53. drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
  54. (int)(paintRect.Top * scale + offsetY),
  55. (int)(paintRect.Width * scale),
  56. (int)(paintRect.Height * scale));
  57. drawVisual.Draw();
  58. }
  59. }
  60. private void DrawBackground(DrawingContext dc)
  61. {
  62. if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
  63. {
  64. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  65. scale = Math.Min(scale, 1);
  66. int drawWidth = (int)(scale * BackgroundWidth);
  67. int drawHeight = (int)(scale * BackgroundHeight);
  68. if (drawWidth < 10 || drawHeight < 10)
  69. {
  70. return;
  71. }
  72. ScaleRate = scale;
  73. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  74. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  75. Rect drawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
  76. dc.DrawImage(BackgroundImage, drawRect);
  77. if (DrawCover)
  78. {
  79. dc.DrawRectangle(CoverBrush, null, drawRect);
  80. }
  81. }
  82. }
  83. private void CleanChild()
  84. {
  85. foreach (DrawingVisual child in VisualControlList)
  86. {
  87. RemoveLogicalChild(child);
  88. RemoveVisualChild(child);
  89. }
  90. VisualControlList.Clear();
  91. }
  92. public void SetBackgroundImage(WriteableBitmap bgImage)
  93. {
  94. DrawCover = false;
  95. BackgroundImage = bgImage;
  96. BackgroundWidth = 0;
  97. BackgroundHeight = 0;
  98. RawWidth = 0;
  99. RawHeight = 0;
  100. OffsetPos = new Point(0, 0);
  101. CleanChild();
  102. if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
  103. {
  104. RawWidth = BackgroundImage.PixelWidth;
  105. RawHeight = BackgroundImage.PixelHeight;
  106. BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
  107. BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
  108. InvalidateVisual();
  109. }
  110. }
  111. public int GetBackgroundImageWidth()
  112. {
  113. if (BackgroundImage != null)
  114. {
  115. return RawWidth;
  116. }
  117. return 0;
  118. }
  119. public int GetBackgroundImageHeight()
  120. {
  121. if (BackgroundImage != null)
  122. {
  123. return RawHeight;
  124. }
  125. return 0;
  126. }
  127. public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
  128. {
  129. CleanChild();
  130. foreach (KeyValuePair<Rect, string> textRect in textsRect)
  131. {
  132. CustomDraw drawVisual = new CustomDraw();
  133. drawVisual.PaintText = textRect.Value;
  134. Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
  135. drawVisual.RawRect = paintRect;
  136. drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
  137. (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
  138. (int)(paintRect.Width * ScaleRate),
  139. (int)(paintRect.Height * ScaleRate));
  140. drawVisual.Draw();
  141. VisualControlList.Add(drawVisual);
  142. AddLogicalChild(drawVisual);
  143. AddVisualChild(drawVisual);
  144. }
  145. }
  146. public void RePaint()
  147. {
  148. InvalidateVisual();
  149. }
  150. }
  151. }