Browse Source

compdfkit(win) - drag

liuaoran 1 year ago
parent
commit
49de02d564

+ 2 - 2
Demo/Examples/Compdfkit_Tools/Security/Watermark/PreviewControl.xaml

@@ -61,11 +61,11 @@
         <Grid Margin="0,0,0,10">
             <TextBlock Text="Preview" FontWeight="DemiBold" FontFamily="Segoe UI" FontSize="14"></TextBlock>
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
-                <Button Height="20" Width="20" Style="{StaticResource TransparentButtonStyle}" Margin="0,0,10,0">
+                <Button x:Name="ZoomInBtn" Height="20" Width="20" Style="{StaticResource TransparentButtonStyle}" Margin="0,0,10,0" Click="ScaleBtn_Click">
                     <Path HorizontalAlignment="Center" VerticalAlignment="Center" Data="M0,6.5 C0,2.91015 2.91015,0 6.5,0 C10.0899,0 13,2.91015 13,6.5 C13,8.12212 12.4058,9.60545 11.4232,10.7442 L15.5,14.821 L14.821,15.5 L10.7442,11.4232 C9.60545,12.4058 8.12212,13 6.5,13 C2.91015,13 0,10.0899 0,6.5 Z M12,6.5 C12,3.46243 9.53757,1 6.5,1 C3.46243,1 1,3.46243 1,6.5 C1,9.53757 3.46243,12 6.5,12 C9.53757,12 12,9.53757 12,6.5 Z M10,6 V7 H3 V6 H10 Z" 
               Fill="Black" />
                 </Button>
-                <Button  Height="20" Width="20"  Style="{StaticResource TransparentButtonStyle}">
+                <Button x:Name="ZoomOutBtn" Height="20" Width="20"  Style="{StaticResource TransparentButtonStyle}"  Click="ScaleBtn_Click">
                     <Path HorizontalAlignment="Center" VerticalAlignment="Center" Data="M6.5 0C2.91015 0 0 2.91015 0 6.5C0 10.0899 2.91015 13 6.5 13C8.12212 13 9.60545 12.4058 10.7442 11.4232L14.821 15.5L15.5 14.821L11.4232 10.7442C12.4058 9.60545 13 8.12212 13 6.5C13 2.91015 10.0899 0 6.5 0ZM6.5 1C9.53757 1 12 3.46243 12 6.5C12 9.53757 9.53757 12 6.5 12C3.46243 12 1 9.53757 1 6.5C1 3.46243 3.46243 1 6.5 1ZM10 6H7V3H6V6H3V7H6V10H7V7H10V6Z" 
               Fill="Black" />
                 </Button>

+ 48 - 13
Demo/Examples/Compdfkit_Tools/Security/Watermark/PreviewControl.xaml.cs

@@ -27,6 +27,10 @@ namespace Compdfkit_Tools.PDFControl
     /// </summary>
     public partial class PreviewControl : UserControl, INotifyPropertyChanged
     {
+        private Point lastMousePosition;
+        private double startVerticalOffset;
+        private double startHorizontalOffset;
+
         private List<int> _pageIndexList = new List<int>();
         public List<int> PageRangeList
         {
@@ -70,13 +74,13 @@ namespace Compdfkit_Tools.PDFControl
             }
         }
 
-        private double _scale = 1.0;
+        private double _scale = 0.3;
         public double Scale
         {
             get => _scale;
             set
             {
-                UpdateProper(ref _scale, Math.Min((Math.Max(value, 0.1)), 10));
+                UpdateProper(ref _scale, Math.Min((Math.Max(value, 0.1)), 1));
             }
         }
 
@@ -118,17 +122,33 @@ namespace Compdfkit_Tools.PDFControl
 
         private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
-
+            lastMousePosition = e.GetPosition(Image);
+            startVerticalOffset = ImageSv.VerticalOffset;
+            startHorizontalOffset = ImageSv.HorizontalOffset;
+            Image.CaptureMouse();
+            this.Cursor = Cursors.Hand;
         }
 
         private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
         {
-
+            Image.ReleaseMouseCapture();
+            this.Cursor = Cursors.Arrow;
         }
 
         private void Image_MouseMove(object sender, MouseEventArgs e)
         {
+            if (ImageSv.Visibility == Visibility.Visible && Image.IsMouseCaptured)
+            {
+                Point currentMousePosition = e.GetPosition(Image);
+                double deltaVerticalOffset = lastMousePosition.Y - currentMousePosition.Y;
+                double deltaHorizontalOffset = lastMousePosition.X - currentMousePosition.X;
+
+                double newVerticalOffset = startVerticalOffset + deltaVerticalOffset/3;
+                double newHorizontalOffset = startHorizontalOffset + deltaHorizontalOffset/3;
 
+                ImageSv.ScrollToVerticalOffset(newVerticalOffset);
+                ImageSv.ScrollToHorizontalOffset(newHorizontalOffset);
+            }
         }
 
         public event PropertyChangedEventHandler PropertyChanged;
@@ -174,10 +194,10 @@ namespace Compdfkit_Tools.PDFControl
         private WriteableBitmap LoadImage(CPDFPage pdfPage)
         {
             Size pageSize = pdfPage.PageSize;
-            double ratio = CalculateThumbnailSize(pageSize);
-            Rect pageRect = new Rect(0, 0, (int)(pageSize.Width / 72.0 * 96 * ratio), (int)(pageSize.Height / 72.0 * 96 * ratio));
-            byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * (96 / 72.0) * (96 / 72.0) * 4)];
-            pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0 * ratio), pageRect, 0xFFFFFFFF, bmpData, 0, true);
+            double ratio = CalculateThumbnailSize(pageSize) * 3;
+            Rect pageRect = new Rect(0, 0, (int)(pageSize.Width * ratio), (int)(pageSize.Height * ratio));
+            byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * 4)];
+            pdfPage.RenderPageBitmapWithMatrix((float)ratio, pageRect, 0xFFFFFFFF, bmpData, 0, true);
 
             WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.Width, (int)pageRect.Height, 96, 96, PixelFormats.Bgra32, null);
             writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.Width, (int)pageRect.Height), bmpData, writeableBitmap.BackBufferStride, 0);
@@ -190,11 +210,11 @@ namespace Compdfkit_Tools.PDFControl
 
             if (size.Height / size.Width > aspectRatio)
             {
-                return thumbnailWidth / size.Width;
+                return (thumbnailWidth) / size.Width;
             }
             else
             {
-                return thumbnailHeight / size.Height;
+                return (thumbnailHeight) / size.Height;
 
             }
         }
@@ -202,9 +222,8 @@ namespace Compdfkit_Tools.PDFControl
         private async void UserControl_Loaded(object sender, RoutedEventArgs e)
         {
             aspectRatio = ImageGd.ActualHeight / ImageGd.ActualWidth;
-            thumbnailWidth = ImageGd.ActualWidth;
-            thumbnailHeight = ImageGd.ActualHeight;
-
+            thumbnailWidth = ImageGd.ActualWidth - 20;
+            thumbnailHeight = ImageGd.ActualHeight - 20;
             await LoadImageAsync(Document.PageAtIndex(0));
         }
 
@@ -243,5 +262,21 @@ namespace Compdfkit_Tools.PDFControl
                 }
             }
         }
+
+        private void ScaleBtn_Click(object sender, RoutedEventArgs e)
+        {
+            var button = sender as Button;
+            if (button != null)
+            {
+                if (button.Name == "ZoomInBtn")
+                {
+                    Scale -= 0.1;
+                }
+                else
+                {
+                    Scale += 0.1;
+                }
+            }
+        }
     }
 }