Browse Source

compdfkit(win) - HomePage自定义按钮

liuaoran 1 năm trước cách đây
mục cha
commit
e58cdf01e3

+ 60 - 0
Demo/Examples/Compdfkit_Tools/Common/BaseControl/HomePageButton.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace Compdfkit_Tools.Common
+{
+    public class HomePageButton : Button
+    {
+        public static readonly DependencyProperty IsToggledProperty =
+    DependencyProperty.Register("IsToggled", typeof(bool), typeof(HomePageButton), new PropertyMetadata(false));
+
+        public bool IsToggled
+        {
+            get { return (bool)GetValue(IsToggledProperty); }
+            set { SetValue(IsToggledProperty, value); }
+        }
+
+        public static readonly DependencyProperty CanChangeProperty =
+            DependencyProperty.Register("CanChange", typeof(bool), typeof(HomePageButton), new PropertyMetadata(true));
+        public bool CanChange
+        {
+            get { return (bool)GetValue(CanChangeProperty); }
+            set { SetValue(CanChangeProperty, value); }
+        }
+
+        public static readonly RoutedEvent ToggledEvent = 
+            EventManager.RegisterRoutedEvent("Toggled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HomePageButton));
+        public event RoutedEventHandler Toggled
+        {
+            add { AddHandler(ToggledEvent, value); }
+            remove { RemoveHandler(ToggledEvent, value); }
+        }
+
+        public HomePageButton()
+        {
+            Click += HomePageButton_Click; ;
+        }
+
+        private void HomePageButton_Click(object sender, RoutedEventArgs e)
+        {
+            if (CanChange)
+            {
+                IsToggled = !IsToggled;
+                RaiseToggledEvent();
+            }
+        }
+
+        private void RaiseToggledEvent()
+        {
+            RoutedEventArgs newEventArgs = new RoutedEventArgs(HomePageButton.ToggledEvent);
+            RaiseEvent(newEventArgs);
+        }
+    }
+}

+ 41 - 0
Demo/Examples/Compdfkit_Tools/Common/Convert/BoolToColorConverter.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Data;
+using System.Windows;
+using System.Drawing;
+using System.Globalization;
+using System.Windows.Media;
+
+namespace Compdfkit_Tools.Common
+{
+    /// <summary>
+    /// Value converter between bool and Brushes
+    /// </summary>
+    [ValueConversion(typeof(bool), typeof(SolidColorBrush))]
+    public class BoolToColorConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if (value is bool boolValue)
+            {
+                if (boolValue)
+                {
+                    return new SolidColorBrush(Colors.Black);
+                }
+                else
+                {
+                    return new SolidColorBrush(Colors.White);
+                }
+            }
+            return new SolidColorBrush(Colors.Black);; 
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            throw new NotImplementedException(); 
+        }
+    }
+}

+ 2 - 0
Demo/Examples/Compdfkit_Tools/Compdfkit_Tools.csproj

@@ -122,9 +122,11 @@
     <Compile Include="Common\BaseControl\CustomProgressBarControl.xaml.cs">
       <DependentUpon>CustomProgressBarControl.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Common\BaseControl\HomePageButton.cs" />
     <Compile Include="Common\BaseControl\PageNumberControl.xaml.cs">
       <DependentUpon>PageNumberControl.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Common\Convert\BoolToColorConverter.cs" />
     <Compile Include="Common\Convert\HeightToTopMarginConverter.cs" />
     <Compile Include="Common\Convert\HomePageHeightConverter.cs" />
     <Compile Include="Common\Convert\AndMultiBoolValueConverter.cs" />

+ 3 - 2
Demo/Examples/Compdfkit_Tools/Security/Watermark/FileGridListWithPageRangeControl.xaml

@@ -4,6 +4,7 @@
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:Compdfkit_Tools.PDFControl"
+             Loaded="UserControl_Loaded"
              mc:Ignorable="d" 
              d:DesignHeight="450" d:DesignWidth="500">
     <UserControl.Resources>
@@ -48,10 +49,10 @@
                                IsReadOnly="True" AutoGenerateColumns="False" FontSize="14" SelectionMode="Extended"
                                ScrollViewer.CanContentScroll="False" HorizontalScrollBarVisibility="Auto">
                         <DataGrid.Columns>
-                            <DataGridTextColumn Header="File" Width="*" Binding="{Binding FileName}" />
+                            <DataGridTextColumn Header="File" Width="*" Binding="{Binding Name}" />
                             <DataGridTextColumn Header="Size" Width="*" Binding="{Binding Size}" />
                             <DataGridTextColumn Header="Page Range" Width="*" Binding="{Binding PageRange}" />
-                            <DataGridTextColumn Header="Location" Width="*" Binding="{Binding Location}" />
+                            <DataGridTextColumn Header="Location" Width="*" Binding="{Binding Path}" />
                         </DataGrid.Columns>
                         <DataGrid.Resources>
                             <Style TargetType="DataGrid">

+ 55 - 17
Demo/Examples/Compdfkit_Tools/Security/Watermark/FileGridListWithPageRangeControl.xaml.cs

@@ -1,5 +1,6 @@
 using ComPDFKit.PDFDocument;
 using Compdfkit_Tools.Common;
+using Compdfkit_Tools.Helper;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
@@ -22,6 +23,9 @@ using UserControl = System.Windows.Controls.UserControl;
 
 namespace Compdfkit_Tools.PDFControl
 {
+
+
+
     /// <summary>
     /// Interaction logic for FileGridListWithPageRangeControl.xaml
     /// </summary>
@@ -39,7 +43,14 @@ namespace Compdfkit_Tools.PDFControl
                 SetValue(IsEnsureProperty, value);
             }
         }
-
+        public class FileInfo
+        {
+            public CPDFDocument Document;
+            public string Name { get; set; }
+            public string Path { get; set; }
+            public string PageRange { get; set; }
+            public string Size { get; set; }
+        }
 
         private int _fileNumText;
         public int FileNumText
@@ -52,8 +63,9 @@ namespace Compdfkit_Tools.PDFControl
             }
         }
 
-        private ObservableCollection<CPDFDocument> _fileInfoDataList;
-        public ObservableCollection<CPDFDocument> FileInfoDataList
+
+        private ObservableCollection<FileInfo> _fileInfoDataList;
+        public ObservableCollection<FileInfo> FileInfoDataList
         {
             get { return _fileInfoDataList; }
             set
@@ -68,8 +80,8 @@ namespace Compdfkit_Tools.PDFControl
 
         public FileGridListWithPageRangeControl()
         {
+            this.DataContext = this;
             InitializeComponent();
-
         }
 
         public event PropertyChangedEventHandler PropertyChanged;
@@ -91,24 +103,44 @@ namespace Compdfkit_Tools.PDFControl
 
         private void AddFilesBtn_Click(object sender, RoutedEventArgs e)
         {
-            var dialog = new OpenFileDialog();
-            dialog.Multiselect = true;
-            dialog.Filter = "PDF Files (*.pdf)|*.pdf";
-            dialog.ShowDialog();
-
-            foreach (string filePath in dialog.FileNames)
+            var openFileDialog = new OpenFileDialog();
+            openFileDialog.Multiselect = true;
+            openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
+            if (openFileDialog.ShowDialog() == DialogResult.OK)
             {
-                CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
-                if (document.IsLocked)
+                foreach (string filePath in openFileDialog.FileNames)
                 {
-                    PasswordWindow passwordWindow = new PasswordWindow();
-                    passwordWindow.InitWithDocument(document);
-                    passwordWindow.Owner = Window.GetWindow(this);
-                    passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
-                    passwordWindow.PasswordDialog.SetShowText("Password error");
+                    CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
+                    if (document.IsLocked)
+                    {
+                        PasswordWindow passwordWindow = new PasswordWindow();
+                        passwordWindow.InitWithDocument(document);
+                        passwordWindow.Owner = Window.GetWindow(this);
+                        passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
+                        passwordWindow.ShowDialog();
+                        if (document.IsLocked)
+                        {
+                            document.Release();
+                            continue;
+                        }
+
+                    }
+
+                    FileInfoDataList.Add(new FileInfo
+                    {
+                        Document = document,
+                        Name = document.FileName,
+                        Size = CommonHelper.GetFileSize(filePath),
+                        Path = document.FilePath,
+                        PageRange = "1-"+document.PageCount.ToString(),
+                    });
                 }
             }
+        }
 
+        private void PasswordWindow_DialogClosed(object sender, PasswordEventArgs e)
+        {
+            throw new NotImplementedException();
         }
 
         private void RemoveBtn_Click(object sender, RoutedEventArgs e)
@@ -120,5 +152,11 @@ namespace Compdfkit_Tools.PDFControl
         {
 
         }
+
+        private void UserControl_Loaded(object sender, RoutedEventArgs e)
+        {
+            FileInfoDataList = new ObservableCollection<FileInfo>();
+            FileDataGrid.ItemsSource = FileInfoDataList;
+        }
     }
 }

+ 1 - 1
Demo/Examples/Compdfkit_Tools/Security/Watermark/WatermarkListDialog.xaml

@@ -15,7 +15,7 @@
         <local:FileGridListWithPageRangeControl x:Name="FileGridListWithPageRangeControl"></local:FileGridListWithPageRangeControl>
         <Grid Grid.Row="1">
             <StackPanel Orientation="Horizontal" Margin="0,24,24,12" HorizontalAlignment="Right">
-                <Button x:Name="ApplyBtn" Content="Apply" Height="32" Width="112" IsEnabled="{Binding ElementName=FileGridListWithPageRangeControl, Path=IsEnsure}" Margin="0,0,8,0"></Button>
+                <Button x:Name="NextBtn" Content="Next" Height="32" Width="112" IsEnabled="{Binding ElementName=FileGridListWithPageRangeControl, Path=IsEnsure}" Margin="0,0,8,0"></Button>
                 <Button x:Name="CancelBtn" Height="32" Content="Cancel" Width="112" ></Button>
             </StackPanel>
         </Grid>

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 113 - 93
Demo/Examples/PDFViewer/MainWindow.xaml


+ 19 - 17
Demo/Examples/PDFViewer/MainWindow.xaml.cs

@@ -19,6 +19,7 @@ using System.Runtime.CompilerServices;
 using Dragablz;
 using Compdfkit_Tools.Helper;
 using System.Windows.Controls.Primitives;
+using Compdfkit_Tools.Common;
 
 namespace PDFViewer
 {
@@ -39,7 +40,6 @@ namespace PDFViewer
         {
             InitializeComponent();
             Loaded += MainWindow_Loaded; ;
-            CreateHomePage();
         }
 
         private void MainWindow_Loaded(object sender, RoutedEventArgs e)
@@ -56,13 +56,13 @@ namespace PDFViewer
                 LoadDefaultDocument();
                 App.DefaultPDFLoaded = true;
             }
+            TabControl.SelectionChanged += TabControl_SelectionChanged;
         }
 
         private void LoadDefaultDocument()
         {
             string defaultFilePath = "PDF32000_2008.pdf";
             TabControlLoadDocument(defaultFilePath);
-            TabControl.SelectionChanged += TabControl_SelectionChanged;
         }
 
         private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -70,24 +70,18 @@ namespace PDFViewer
             var tabablz = sender as Dragablz.TabablzControl;
             if(tabablz.SelectedIndex != -1)
             {
-                HomePageTog.IsChecked = false;
+                HomePageButton.IsToggled = false;
+            }
+            if (TabControl.Items.Count > 0)
+            {
+                HomePageButton.CanChange = true;
             }
             else
             {
-                HomePageTog.IsChecked = true;
+                HomePageButton.CanChange = false;
             }
         }
 
-        private void CreateHomePage()
-        {
-            TabItemExt tabItem = new TabItemExt();
-            tabItem.Content = homePageControl;
-            tabItem.IsSelected = true; 
-            tabItem.Tag = "Home Page";
-            tabItem.FileName = "Home Page";
-            TabControl.Items.Add(tabItem);
-        }
-
         private void TabControlLoadDocument(string filePath)
         {
             if (App.OpenedFilePathList.Contains(filePath))
@@ -428,10 +422,18 @@ namespace PDFViewer
 
         }
 
-        private void Button_Click(object sender, RoutedEventArgs e)
+
+        private void HomePageButton_Toggled(object sender, RoutedEventArgs e)
         {
-            TabControl.SelectedIndex = -1;
-            ToggleButton toggleButton = (ToggleButton)sender;
+            var homePageButton = sender as HomePageButton;
+            if (homePageButton.IsToggled)
+            {
+                TabControl.SelectedIndex = -1;
+            }
+            else
+            {
+                TabControl.SelectedIndex = 0;
+            }
         }
     }
 }