2013년 3월 5일 화요일

vsto 상단 버튼 만들기




http://blog.naver.com/nettalk72/40043967801
http://msdn.microsoft.com/en-us/library/office/bb226712(v=office.12).aspx



보통 VSTO 를 가지고 서는 Word 와 Excel 을 주로 다루고 있다.
이번에 설명하고 하는 것은 PowerPoint 2007 이다. (전체 Full 소스 참조)

물론, PowerPoint 에 대한 자료는 극히 적다. 인터넷을 찾아봐도, 이렇다할 자료가 있지 않음을 확인했다.
돌고 돌다가, http://msdn2.microsoft.com/ko-kr/office/aa905465.aspx 의 PowerPoint 개발 Portal 에서 시작해야 할것 같다.

이번에 설명할 것은 간단하게 ?  템플릿을 적용하고, 새로운 슬라이드및 이미지 슬라이드를 추가 시키는 방법을 설명할 것이다.
이렇게 만들어진 최종 결과는 아래 화면과 비슷하다.

 

그럼 우선 VS.NET2005에서 Powerpoint addin 을 만든다.
그리고 새로운 아이템으로 Ribbon 컨트롤을 추가한다. 이는 위 화면상에 있는 3개의 HappyFace 버튼들을 만들기 위한 것이다.


Add를 선택해서 추가시킨다.

그러면 Ribbon1.cs 와 함께 Ribbon1.xml 이 추가된다.
옆화면처럼 기존 데이타를 변경시킨다.

(Ribbon1.xml 에 것은 추후 Ribbon 만을 상세히 다루는 글을 게재할 예정이다. 이는 기본적인 XML을 수정하는 것과 VS.NET 2008 Beta2 부터 추가된 디자인 툴을 통해서 이뤄진다 )

 
 

이렇게 지정하는 것만으로도 HappyFace 이미지를 가진 버튼 3개가 나타날 환경이 갖쳐졌다.
이제 이를 나타나도록 지시해야 하는데,
Addin 이 호출될때 불려지는 RequestService 메서드에서 생성된 Ribbon을 리턴하므로써 전달 된다.

이를 위해서 Ribbon1.cs 의 첫번쨰 라인에 설명이 나온대로 Uncomment 를 해야 한다.

 // TODO:
 // This is an override of the RequestService method in the ThisAddIn class.
 // To hook up your custom ribbon uncomment this code.
 ...
 //

이 과정으로 Ribbon 에 버튼이 나타날 것이다. 그러나, Button 을 클릭시 해당 하는 코드(메서드)들을 만들어 줘야 한다.
이는  button 에 대해서는 메서드 이름은 XML 에서 정의하고 파라미터는 사전에 button 에 대해서 정의가 되어 있다.
(Ribbon 에 사용되는 모든 컨트롤 들은 자신만의 고유한 파라미터가 미리 정의되어 있다. 이에 대한것은 다음에 살펴볼것이다.)

아래는 Button1 의 클릭시 실행되는 메서드 이다. 

    public void OnButton1(Office.IRibbonControl control)
    {
      // 현재 활성중인 Presentation 을 얻어낸다.
      PowerPoint.Presentation presentation =  Globals.ThisAddIn.Application.ActivePresentation;

      // 기본 Layout 으로 Title 을 선택한다.
      PowerPoint.CustomLayout customLayout =
                       presentation.SlideMaster.CustomLayouts[PowerPoint.PpSlideLayout.ppLayoutTitle];

      // 첫위치에 삽입한다.
      PowerPoint.Slide slide = presentation.Slides.AddSlide(1, customLayout);

      // Shape 의 Text 에 값을 입력한다.
      slide.Shapes.Title.TextFrame.TextRange.Text = "VSTO 2005 SE";
      slide.Shapes[2].TextFrame.TextRange.Text = "Nettalk's VSTO Story";
     
      // 템플릿을 지정한다.
      presentation.ApplyTemplate(@"C:\Program Files\Microsoft Office\Document Themes 12\paper.thmx");
   }

다음은 Button2 에 대한 메서드이다.

    public void OnButton2(Office.IRibbonControl control)
    {
        // 현재 활성중인 Presentation 을 얻어낸다.
        PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
  
        // 기본 Layout 으로 Text 을 선택한다.
        PowerPoint.CustomLayout customLayout =
               presentation.SlideMaster.CustomLayouts[PowerPoint.PpSlideLayout.ppLayoutText];

        // 마지막 위치에 삽입한다.
        presentation.Slides.AddSlide(presentation.Slides.Count + 1, customLayout);
    }

다음은 Button3 에 대한 메서드 이다.

  public void OnButton3(Office.IRibbonControl control)
    {
      OpenFileDialog dialog = new OpenFileDialog();
      dialog.Filter =
        "All pictures (*.jpg,*.jpeg,*.bmp,*.gif,*.tif)|" +
        "*.jpf;*.jpeg;*.bmp;*.gif;*.tif|" +
        "All files (*.*)|*.*";
      dialog.CheckFileExists = true;
      if (dialog.ShowDialog() == DialogResult.OK)
      {
        AddPicture(dialog.FileName);
      }
    }

    public void AddPicture(String pictureFileName)
    {
        PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
        if (presentation != null)
        {
          // 새로운 Layout (PictureWithCaption)를 추가한다.
          PowerPoint.Slide slide =  presentation.Slides.Add(presentation.Slides.Count + 1,
                                                          PowerPoint.PpSlideLayout.ppLayoutPictureWithCaption);
          // Shapes[2] 는 선택된 부분이 Image 이다.
          PowerPoint.Shape shape = slide.Shapes[2];

          // AddPicture 를 통해서 이미지를 삽입한다.
          slide.Shapes.AddPicture(pictureFileName, Microsoft.Office.Core.MsoTriState.msoFalse,
                                         Microsoft.Office.Core.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);

          // 파일 이름을 추가(표시) 한다.
          slide.Shapes[1].TextFrame.TextRange.Text = pictureFileName;
          slide.Select();
        }
    }

댓글 없음: