|
@@ -0,0 +1,163 @@
|
|
|
+using PDF_Office.Helper;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Controls;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+
|
|
|
+namespace PDF_Office.CustomControl.ScanViewControl
|
|
|
+{
|
|
|
+ class CustomPanel: Panel
|
|
|
+ {
|
|
|
+ public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xA0, 0xFF, 0xFF, 0xFF));
|
|
|
+ public bool DrawCover { get; set; } = false;
|
|
|
+
|
|
|
+ private WriteableBitmap BackgroundImage = null;
|
|
|
+ private int BackgroundWidth = 0;
|
|
|
+ private int BackgroundHeight = 0;
|
|
|
+ private Point OffsetPos = new Point(0, 0);
|
|
|
+ private double ScaleRate = 1;
|
|
|
+ private CustomDraw HoverChild = null;
|
|
|
+ public TextBlock ParentBlock;
|
|
|
+ private int RawWidth = 0;
|
|
|
+ private int RawHeight = 0;
|
|
|
+
|
|
|
+ private List<CustomDraw> VisualControlList = new List<CustomDraw>();
|
|
|
+ protected override int VisualChildrenCount => VisualControlList.Count;
|
|
|
+ protected override Visual GetVisualChild(int index)
|
|
|
+ {
|
|
|
+ return VisualControlList[index];
|
|
|
+ }
|
|
|
+ protected override void OnRender(DrawingContext dc)
|
|
|
+ {
|
|
|
+ Rect drawRect = new Rect(0, 0, ActualWidth, ActualHeight);
|
|
|
+ dc.DrawRectangle(Brushes.LightGray, null, drawRect);
|
|
|
+ DrawBackground(dc);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
|
|
+ {
|
|
|
+ double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
|
|
|
+ scale = Math.Min(scale, 1);
|
|
|
+ int drawWidth = (int)(scale * BackgroundWidth);
|
|
|
+ int drawHeight = (int)(scale * BackgroundHeight);
|
|
|
+ if (drawWidth < 10 || drawHeight < 10)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
|
|
|
+ int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
|
|
|
+ foreach (CustomDraw drawVisual in VisualControlList)
|
|
|
+ {
|
|
|
+ Rect paintRect = drawVisual.RawRect;
|
|
|
+ drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
|
|
|
+ (int)(paintRect.Top * scale + offsetY),
|
|
|
+ (int)(paintRect.Width * scale),
|
|
|
+ (int)(paintRect.Height * scale));
|
|
|
+ drawVisual.Draw();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DrawBackground(DrawingContext dc)
|
|
|
+ {
|
|
|
+ if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
|
|
|
+ {
|
|
|
+ double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
|
|
|
+ scale = Math.Min(scale, 1);
|
|
|
+ int drawWidth = (int)(scale * BackgroundWidth);
|
|
|
+ int drawHeight = (int)(scale * BackgroundHeight);
|
|
|
+ if (drawWidth < 10 || drawHeight < 10)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ScaleRate = scale;
|
|
|
+ OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
|
|
|
+ OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
|
|
|
+ Rect drawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
|
|
|
+ dc.DrawImage(BackgroundImage, drawRect);
|
|
|
+ if (DrawCover)
|
|
|
+ {
|
|
|
+ dc.DrawRectangle(CoverBrush, null, drawRect);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CleanChild()
|
|
|
+ {
|
|
|
+ foreach (DrawingVisual child in VisualControlList)
|
|
|
+ {
|
|
|
+ RemoveLogicalChild(child);
|
|
|
+ RemoveVisualChild(child);
|
|
|
+ }
|
|
|
+ VisualControlList.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetBackgroundImage(WriteableBitmap bgImage)
|
|
|
+ {
|
|
|
+ DrawCover = false;
|
|
|
+ BackgroundImage = bgImage;
|
|
|
+ BackgroundWidth = 0;
|
|
|
+ BackgroundHeight = 0;
|
|
|
+ RawWidth = 0;
|
|
|
+ RawHeight = 0;
|
|
|
+ OffsetPos = new Point(0, 0);
|
|
|
+ CleanChild();
|
|
|
+ if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
|
|
|
+ {
|
|
|
+ RawWidth = BackgroundImage.PixelWidth;
|
|
|
+ RawHeight = BackgroundImage.PixelHeight;
|
|
|
+ BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
|
|
|
+ BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
|
|
|
+ InvalidateVisual();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int GetBackgroundImageWidth()
|
|
|
+ {
|
|
|
+ if (BackgroundImage != null)
|
|
|
+ {
|
|
|
+ return RawWidth;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int GetBackgroundImageHeight()
|
|
|
+ {
|
|
|
+ if (BackgroundImage != null)
|
|
|
+ {
|
|
|
+ return RawHeight;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
|
|
|
+ {
|
|
|
+ CleanChild();
|
|
|
+ foreach (KeyValuePair<Rect, string> textRect in textsRect)
|
|
|
+ {
|
|
|
+ CustomDraw drawVisual = new CustomDraw();
|
|
|
+ drawVisual.PaintText = textRect.Value;
|
|
|
+ Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
|
|
|
+ drawVisual.RawRect = paintRect;
|
|
|
+ drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
|
|
|
+ (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
|
|
|
+ (int)(paintRect.Width * ScaleRate),
|
|
|
+ (int)(paintRect.Height * ScaleRate));
|
|
|
+ drawVisual.Draw();
|
|
|
+ VisualControlList.Add(drawVisual);
|
|
|
+ AddLogicalChild(drawVisual);
|
|
|
+ AddVisualChild(drawVisual);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void RePaint()
|
|
|
+ {
|
|
|
+ InvalidateVisual();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|