Delphi & PowerPoint(Delphi如何控制Powerpoint並顯示時間)

 

這一篇文章主要是筆者在上課的時候要求學生上台報告時必須要學會如何控制時間製作的程式所改編而成,當時主要是要求學生報告的時間必須控制在15~20分鐘裡面,同時為了精準計時,因此當投影片啟動時,自動啟動倒數計時,當時間剩下5分鐘時,自動顯示時間及投影片張數在投影片上(如圖1)

1 程式執行畫面

整個程式的構思如下:

1.      程式畫面如圖2

2 程式設計畫面

2.      在程式啟動後,使用Delphi6以後可以製作透明Form的屬性及將程式的Style屬性設定為bsNone,然後將Panel隱藏,只有顯示Label1

3.      使用TPowerPointApplicationTpowerPointPresentation來控制及連結投影片的顯示。因此按下啟動的按鈕後,程式控制如下:

procedure TForm1.Button1Click(Sender: TObject);

begin

   self.TransparentColor:=True;

   self.BorderStyle:=bsNone;

   panel1.Visible:=False;

   try

      PowerPointApplication1.Connect;

      PowerPointPresentation1.ConnectTo(PowerPointApplication1.Presentations.Item(1));

   except

      showmessage('無法連上Powerpoint');

   end;

end;

4.      當使用者Click Label1時回覆設定畫面

procedure TForm1.Label1Click(Sender: TObject);

begin

   self.TransparentColor:=False;

   self.BorderStyle:=bsSizeable;

   Panel1.Visible:=True;

   Timer1.Enabled:=False;

   try

      PowerPointPresentation1.Disconnect;

      PowerPointApplication1.Disconnect;

   except

   end;

end;

5.      開始啟動投影片後,啟動Timer1計時,其可以在PowerPointApplication1onSlideShow Event中控制

procedure TForm1.PowerPointApplication1SlideShowBegin(Sender: TObject;

  var Wn: OleVariant);

begin

   case Combobox1.ItemIndex of

      0:tt:=0;

      1:tt:=0;

      2:tt:=60*strtoint(Edit1.text);

   end;

   Timer1.Enabled:=True;

   self.FormStyle:=fsNormal;

end;

6.      Time1OnTimer Event中,檢查及判斷是否符合設定的功能,假如有需要將FormFormStyle設定為StayonTop,程式如下:

procedure TForm1.Timer1Timer(Sender: TObject);

begin

   case Combobox1.ItemIndex of

      0:begin

           if (strtoint(Edit2.text)=0) or (tt=strtoint(Edit2.text)*60) then

              self.FormStyle:=fsStayOnTop;

           Label1.Caption:=TimeToStr(Now)+

               '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

           tt:=tt+1;

        end;

      1:begin

           if (tt=strtoint(Edit1.text)*60) then

              Timer1.Enabled:=false

           else

           begin

              if (strtoint(Edit2.text)=0) or (tt=strtoint(Edit2.text)*60) then

                 self.FormStyle:=fsStayOnTop;

              Label1.Caption:=formatfloat('00',tt div 60)+':'+formatfloat('00',tt mod 60)+

                  '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

              tt:=tt+1;

           end;

        end;

      2:begin

           if tt=0 then

           begin

              Timer1.Enabled:=false;

           end else

           begin

              if (tt=strtoint(Edit1.text)*60) then

                 self.FormStyle:=fsStayOnTop;

              Label1.Caption:=formatfloat('00',tt div 60)+':'+formatfloat('00',tt mod 60)+

              '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

              tt:=tt-1;

           end;

        end;

   end;

 

end;

7.      在以上的程式中,最難取的的是目前撥放投影片的頁數(變數pp),因為經由PowerPointPresentation1的物件中,並無法取得目前所在的投影片位置,其只可以提供PowerPointPresentation1.Slides.Count,筆者經由『Delphi如何控制WordExcel的自我學習方式』那篇文章中,所介紹的學習方式,再比較PowerPointPresentation1Event的涵義,開始Delphi所付的PowerPoint source(unit MSPpt2000),經過比對後,了解SlideShowNextSlide EventWn參數的內容,取得目前投影片的slidenumber,程式如下:

 

procedure TForm1.PowerPointApplication1SlideShowNextSlide(Sender: TObject;

  var Wn: OleVariant);

begin

   pp:=Wn.view.slide.slidenumber;

   case Combobox1.ItemIndex of

      0:begin

           Label1.Caption:=TimeToStr(Now)+

               '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

        end;

      1:begin

           Label1.Caption:=formatfloat('00',tt div 60)+':'+formatfloat('00',tt mod 60)+

                  '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

        end;

      2:begin

           Label1.Caption:=formatfloat('00',tt div 60)+':'+formatfloat('00',tt mod 60)+

              '('+inttostr(pp)+'/'+inttostr(PowerPointPresentation1.Slides.Count)+')';

        end;

   end;

end;

 

若要了解整個程式的內容,請參考Source