CrazyIter_Bin 1 年之前
父節點
當前提交
81cec113e0

+ 61 - 1
HTEXTeach/MainRibbon.cs

@@ -5,7 +5,9 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-
+using Microsoft.Office.Interop.PowerPoint;
+using System.Windows;
+using PowerPoint = Microsoft.Office.Interop.PowerPoint;
 namespace HTEXTeach
 {
     public partial class MainRibbon
@@ -15,9 +17,67 @@ namespace HTEXTeach
            
          
         }
+        public bool IsEditingMode()
+        {
+            // 获取 PowerPoint 应用程序对象
+            PowerPoint.Application pptApp = Globals.ThisAddIn.Application;
+
+            // 检查是否有活动的演示文稿
+            if (pptApp.ActivePresentation != null)
+            {
+                // 检查是否有编辑视图窗口
+                if (pptApp.ActiveWindow.ViewType == PowerPoint.PpViewType.ppViewNormal)
+                {
+                    // 处于编辑模式
+                    return true;
+                }
+            }
+
+            // 不处于编辑模式
+            return false;
+        }
+        public bool IsSlideShowInProgress()
+        {
+            // 获取 PowerPoint 应用程序对象
+            PowerPoint.Application pptApp = Globals.ThisAddIn.Application;
 
+            // 检查是否存在幻灯片播放窗口
+            if (pptApp.SlideShowWindows.Count > 0)
+            {
+                // 存在幻灯片播放窗口,处于全屏播放模式
+                return true;
+            }
+            else
+            {
+                // 不存在幻灯片播放窗口,不处于全屏播放模式
+                return false;
+            }
+        }
         private void button1_Click(object sender, RibbonControlEventArgs e)
         {
+            if (IsEditingMode())
+            {
+                MessageBox.Show("编辑模式");
+
+            }
+            else { MessageBox.Show("不是编辑模式"); }
+            if (IsSlideShowInProgress())
+            {
+                MessageBox.Show("全屏");
+
+            }
+            else { MessageBox.Show("不是全屏"); }
+
+            Microsoft.Office.Interop.PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
+            // 在演示文稿中添加新的幻灯片
+            Microsoft.Office.Interop.PowerPoint.Slide newSlide = presentation.Slides.Add(presentation.Slides.Count + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);
+
+            //newSlide.Shapes.AddPicture2()
+
+            // 可以在这里对新幻灯片进行进一步的设置
+
+            // 最后,显示新幻灯片
+            newSlide.Select();
             Microsoft.Office.Interop.PowerPoint.Application pptApp = Globals.ThisAddIn.Application;
             Microsoft.Office.Interop.PowerPoint.Slide activeSlide = pptApp.ActiveWindow.View.Slide;
             MainWindow mainWindow = new MainWindow();

+ 126 - 15
HTEXTeach/ThisAddIn.cs

@@ -7,35 +7,146 @@ using PowerPoint = Microsoft.Office.Interop.PowerPoint;
 using Office = Microsoft.Office.Core;
 using  System.Windows;
 using HTEXTeachLib;
+using Microsoft.Office.Interop.PowerPoint;
+using System.Windows.Forms;
+using Microsoft.Office.Core;
 
 namespace HTEXTeach
 {
     public partial class ThisAddIn
     {
+
+        public static MainWindow mainWindow = null;
         
         private void ThisAddIn_Startup(object sender, System.EventArgs e)
         {
-           
-            //var hwnd = new IntPtr(Application.HWND);
-            //var windowWrapper = new WindowWrapper(hwnd);
-            //System.Windows.Interop.ComponentDispatcher.ThreadPreprocessMessage += (_, msg) =>
-            //{
-            //    wpfWindow.Dispatcher.Invoke(() =>
-            //    {
-            //        System.Windows.Interop.HwndSource.FromHwnd(windowWrapper.Handle)?.CompositionTarget?.TransformFromDevice?.Transform(msg.Message);
-            //        System.Windows.Interop.HwndSource.FromHwnd(windowWrapper.Handle)?.Dispatcher?.Invoke(() =>
-            //        {
-            //            System.Windows.Interop.ComponentDispatcher.RaiseThreadMessage(ref msg.Message);
-            //        });
-            //    });
-            //};
-            //mainWindow.Show();
+            // 注册 PowerPoint 应用程序的事件
+            Globals.ThisAddIn.Application.SlideShowBegin += Application_SlideShowBegin;
+            //Globals.ThisAddIn.Application.SlideShowEnd += Application_SlideShowEnd;
+            Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;
+            //Globals.ThisAddIn.Application.ProtectedViewWindowBeforeClose+=Application_ProtectedViewWindowBeforeClose;
+            Globals.ThisAddIn.Application.PresentationBeforeClose+=Application_PresentationBeforeClose;
+           // Globals.ThisAddIn.Application.PresentationClose+=Application_PresentationClose;
+            Globals.ThisAddIn.Application.PresentationCloseFinal+=Application_PresentationCloseFinal;
+        }
+
+        /// <summary>
+        /// 最终会执行的
+        /// </summary>
+        /// <param name="Pres"></param>
+        private void Application_PresentationCloseFinal(Presentation Pres)
+        {
+            System.Windows.MessageBox.Show("Application_PresentationCloseFinal");
+        }
+
+        //private void Application_PresentationClose(Presentation Pres)
+        //{
+        //    MessageBox.Show("Application_PresentationClose");
+        //}
+
+        private void Application_PresentationBeforeClose(Presentation Pres, ref bool Cancel)
+        {
+           var  path=  GetCurrentPresentationPath();
+            System.Windows.MessageBox.Show($"path:{path},Application_PresentationBeforeClose");
         }
 
+        //private void Application_ProtectedViewWindowBeforeClose(ProtectedViewWindow ProtViewWindow, PpProtectedViewCloseReason ProtectedViewCloseReason, ref bool Cancel)
+        //{
+        //    MessageBox.Show("Application_ProtectedViewWindowBeforeClose");
+        //}
+
         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
+        {   
+            // 解除注册的事件
+            Globals.ThisAddIn.Application.SlideShowBegin -= Application_SlideShowBegin;
+            //Globals.ThisAddIn.Application.SlideShowEnd -= Application_SlideShowEnd;
+            Globals.ThisAddIn.Application.WindowActivate -= Application_WindowActivate;
+        }
+
+        private void Application_SlideShowBegin(PowerPoint.SlideShowWindow Wn)
+        {
+            // 在全屏播放模式开始时触发
+            // 可以在这里执行相应的操作
+            IsEditingMode();
+            Console.WriteLine("Slide Show Mode Started");
+        }
+
+        private void Application_SlideShowEnd(PowerPoint.Presentation Pres)
         {
+            // 在全屏播放模式结束时触发
+            // 可以在这里执行相应的操作
+            IsEditingMode();
+            Console.WriteLine("Slide Show Mode Ended");
         }
 
+        private void Application_WindowActivate(PowerPoint.Presentation Pres, PowerPoint.DocumentWindow Wn)
+        {
+            // 在切换到编辑模式时触发
+            // 可以在这里执行相应的操作
+
+            IsEditingMode();
+            Console.WriteLine("Switched to Editing Mode");
+          
+        }
+        public string GetCurrentPresentationPath()
+        {
+            PowerPoint.Presentation currentPresentation = Globals.ThisAddIn.Application.ActivePresentation;
+
+            if (currentPresentation != null)
+            {
+                // 获取当前演示文稿的完整路径
+                string presentationPath = currentPresentation.FullName+$"{currentPresentation.Saved}";
+
+                // 检查路径是否为空,如果为空表示演示文稿是新建的
+                if (currentPresentation.Saved==MsoTriState.msoFalse)
+                {
+                    // 使用System.Windows.Forms.SaveFileDialog来选择保存路径
+                    using (SaveFileDialog saveFileDialog = new SaveFileDialog())
+                    {
+                        saveFileDialog.Title = "Save Presentation As";
+                        saveFileDialog.Filter = "PowerPoint Presentation (*.pptx)|*.pptx";
+                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
+                        {
+                            presentationPath = saveFileDialog.FileName;
+
+                            // 在这里可以保存演示文稿到指定路径
+                            currentPresentation.SaveAs(presentationPath);
+                        }
+                        else
+                        {
+                            // 用户取消保存操作
+                            // 这里可以根据需求进行处理
+                        }
+                    }
+                }
+
+                return presentationPath;
+            }
+
+            // 如果没有打开的演示文稿,则返回空字符串或者可以抛出异常,具体取决于你的需求
+            return string.Empty;
+        }
+        public bool IsEditingMode()
+        {
+            // 获取 PowerPoint 应用程序对象
+            PowerPoint.Application pptApp = Globals.ThisAddIn.Application;
+
+            // 检查是否有活动的演示文稿
+            if (pptApp.ActivePresentation != null)
+            {
+                var s = pptApp.ActiveWindow.WindowState==PpWindowState.ppWindowMaximized;
+                // 检查是否有编辑视图窗口
+                if (s)
+                {
+                    // 处于编辑模式
+                    return true;
+                }
+            }
+            // 不处于编辑模式
+            return false;
+        }
+
+
         #region VSTO 生成的代码
 
         /// <summary>

+ 82 - 0
HTEXTeachLib/Controls/logo.xaml

@@ -0,0 +1,82 @@
+<Viewbox x:Class="HTEXTeachLib.Controls.logo"
+         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+         Width="45"
+         Margin="0,0,0,0"
+         HorizontalAlignment="Center"
+         VerticalAlignment="Center">
+    <Grid Background="Transparent">
+        <Grid.Effect>
+            <DropShadowEffect BlurRadius="1" Direction="310" ShadowDepth="2" />
+        </Grid.Effect>
+        <!--<Image Source="../Icon/logo.png" SnapsToDevicePixels="True"></Image>-->
+        <Path Width="50"
+              Height="18"
+              Margin="12,56,0,0"
+              HorizontalAlignment="Left"
+              VerticalAlignment="Top"
+              Data="M30.0504,8.2068672E-07 L34.709944,4.6595454 34.584206,4.782318 C30.023165,9.130882 23.847369,11.800751 17.047994,11.800751 10.687286,11.800751 4.8722948,9.4642684 0.41445786,5.6027482 L3.4369513E-07,5.2349866 4.6675589,0.56742611 4.7268556,0.62004353 C8.0289567,3.4804319 12.336359,5.2111566 17.047994,5.2111566 22.084569,5.2111566 26.65923,3.2334779 30.03778,0.012320504 z"
+              Stretch="Fill"
+              Stroke="Black"
+              StrokeThickness="0"
+              UseLayoutRounding="False">
+            <Path.Fill>
+                <LinearGradientBrush StartPoint="0.196,0.641" EndPoint="0.974,-0.546">
+                    <GradientStop Offset="0" Color="#FFAACD06" />
+                    <GradientStop Offset="1" Color="#FFE3EDB8" />
+                </LinearGradientBrush>
+            </Path.Fill>
+        </Path>
+        <Path Width="18"
+              Height="50"
+              Margin="56,12,0,0"
+              HorizontalAlignment="Left"
+              VerticalAlignment="Top"
+              Data="M4.6675618,3.4369513E-07 L5.0353234,0.41445786 C8.8968436,4.8722948 11.233326,10.687286 11.233326,17.047994 11.233326,23.408699 8.8968436,29.223693 5.0353234,33.681527 L4.887639,33.847967 0.21580592,29.176134 0.34446605,29.023971 C3.0303087,25.769485 4.6437319,21.597157 4.6437319,17.047994 4.6437319,12.336359 2.9130071,8.0289567 0.052618759,4.7268556 L1.3389534E-06,4.6675579 z"
+              Stretch="Fill"
+              Stroke="Black"
+              StrokeThickness="0"
+              UseLayoutRounding="False">
+            <Path.Fill>
+                <LinearGradientBrush StartPoint="0.307,0.861" EndPoint="-0.036,-0.292">
+                    <GradientStop Offset="0" Color="#FFFDD000" />
+                    <GradientStop Offset="1" Color="#FFFEF7DD" />
+                </LinearGradientBrush>
+            </Path.Fill>
+        </Path>
+        <Path Width="18"
+              Height="50"
+              Margin="0,12,0,0"
+              HorizontalAlignment="Left"
+              VerticalAlignment="Top"
+              Data="M7.1412043,1.7763568E-15 L11.800749,4.6595455 11.78843,4.6721619 C8.5672713,8.0507143 6.5895925,12.625377 6.5895925,17.661952 6.5895925,22.536056 8.4417378,26.97757 11.480612,30.321072 L11.57478,30.422233 6.9125664,35.084443 6.6028758,34.751769 C2.5003956,30.238033 0,24.241992 0,17.661952 0,10.862577 2.6698669,4.6867813 7.0184306,0.12573684 z"
+              Stretch="Fill"
+              Stroke="Black"
+              StrokeThickness="0"
+              UseLayoutRounding="False">
+            <Path.Fill>
+                <LinearGradientBrush StartPoint="0.48,0.223" EndPoint="2.768,1.31">
+                    <GradientStop Offset="0" Color="#FF00A0E9" />
+                    <GradientStop Offset="1" Color="#FFE6F4FD" />
+                </LinearGradientBrush>
+            </Path.Fill>
+        </Path>
+        <Path Width="50"
+              Height="18"
+              Margin="12,0,0,0"
+              HorizontalAlignment="Left"
+              VerticalAlignment="Top"
+              Data="M16.799974,0 C23.380013,0 29.376055,2.5003956 33.88979,6.6028758 L34.222465,6.9125659 29.560251,11.574779 29.459094,11.480612 C26.115592,8.4417378 21.674078,6.5895925 16.799974,6.5895925 12.250808,6.5895925 8.0784807,8.2030167 4.8239938,10.888856 L4.6718315,11.017517 3.6955384E-07,6.3456868 0.16643769,6.1980009 C4.6242747,2.3364808 10.439266,0 16.799974,0 z"
+              Stretch="Fill"
+              Stroke="Black"
+              StrokeThickness="0"
+              UseLayoutRounding="False">
+            <Path.Fill>
+                <LinearGradientBrush StartPoint="0.601,-0.073" EndPoint="-0.115,0.842">
+                    <GradientStop Offset="1" Color="#FFFCE4D6" />
+                    <GradientStop Color="#FFE60012" />
+                </LinearGradientBrush>
+            </Path.Fill>
+        </Path>
+    </Grid>
+</Viewbox>

+ 28 - 0
HTEXTeachLib/Controls/logo.xaml.cs

@@ -0,0 +1,28 @@
+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.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace HTEXTeachLib.Controls
+{
+    /// <summary>
+    /// LOGO.xaml 的互動邏輯
+    /// </summary>
+    public partial class logo : Viewbox
+    {
+        public logo()
+        {
+            InitializeComponent();
+        }
+    }
+}

+ 14 - 0
HTEXTeachLib/HTEXTeachLib.csproj

@@ -50,6 +50,9 @@
     <Reference Include="PresentationFramework" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Controls\logo.xaml.cs">
+      <DependentUpon>logo.xaml</DependentUpon>
+    </Compile>
     <Compile Include="MainControl.xaml.cs">
       <DependentUpon>MainControl.xaml</DependentUpon>
     </Compile>
@@ -69,6 +72,9 @@
       <DependentUpon>Settings.settings</DependentUpon>
       <DesignTimeSharedInput>True</DesignTimeSharedInput>
     </Compile>
+    <Compile Include="TopToolBar.xaml.cs">
+      <DependentUpon>TopToolBar.xaml</DependentUpon>
+    </Compile>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>
@@ -79,6 +85,10 @@
     </None>
   </ItemGroup>
   <ItemGroup>
+    <Page Include="Controls\logo.xaml">
+      <Generator>MSBuild:Compile</Generator>
+      <SubType>Designer</SubType>
+    </Page>
     <Page Include="MainControl.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
@@ -87,6 +97,10 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
+    <Page Include="TopToolBar.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 14 - 0
HTEXTeachLib/TopToolBar.xaml

@@ -0,0 +1,14 @@
+<Window x:Class="HTEXTeachLib.TopToolBar"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:HTEXTeachLib"
+        mc:Ignorable="d"
+        Title="TopToolBar" MaxWidth="300"  Height="41" Width="184"
+        WindowStyle="None" Background="Transparent"
+        AllowsTransparency="True" ShowInTaskbar="False">
+    <Canvas>
+        
+    </Canvas>
+</Window>

+ 27 - 0
HTEXTeachLib/TopToolBar.xaml.cs

@@ -0,0 +1,27 @@
+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.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace HTEXTeachLib
+{
+    /// <summary>
+    /// TopToolBar.xaml 的交互逻辑
+    /// </summary>
+    public partial class TopToolBar : Window
+    {
+        public TopToolBar()
+        {
+            InitializeComponent();
+        }
+    }
+}