<%-- ? .NET Framework ???????? Microsoft Word ?????
*
   ?? (??)

? .NET Framework ???????? Microsoft Word ?????

 

???Omar AL Zabir
OmarAlZabir@gmail.com

2005 ? 8 ?

?????????? OfficeAutomation.msi?

??????????????-??-????(Model-View-Controller) ????????? .NET Framework ???????? Microsoft Word ??????(?????????????????? 26 ?)

??

Microsoft Word ??????????
??????????????
???-??-????????
Smart Editor ???
???????????????????
???????????
??????
Framework ??
????????????
???????
?????
??

??? Microsoft Office ???????????????????????? (Automation)???????????????????????????????????????????????????????????????????????????????????????????? Office ????????????????????????????????????????????????????????? Microsoft Word ??????????? (Object-oriented?OO) ???????????-??-??? (MVC)???????????????????????????????

????????????????????? Microsoft Office ?????????????????????????????? Office ????????? .NET Framework ? Microsoft Visual Basic for Applications (VBA) ??????????????????????? .NET ???????? Microsoft Word ??????????????-??-?????????? .NET Framework ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? UI ???????????????????????????? UI???????????????????? UI ???????

Microsoft Word ??????????

?????? Microsoft Word ????????? Microsoft Word ??????????????????????????

Microsoft Word ??????????????? Application ????? Word ?????????????????????????? (Singleton) ???????

[? 1] Microsoft Word ???? (????)

?? Application ????????????? ???????????????????????????????????????????????????????????????? Application.Documents.Open()?? Word ????? [????] ??????? Application.Quit()???? Word?????????Microsoft Word ??????????????? Word ???????????????????????? public ??????????????????????? Document ?????????????? Name ???????? UI ??????????????????????????????????

????????????????????????????????????? Visual Studio IDE????????????? Visual Studio ????????????????????????????????? DEL ?????????? [????] ???????????????????????????????????????????? (??)????????????????????????????????????????????????????? IDE ??????????????????????????????????????????????????????????????????????????? Visual Studio ??????????????????????????????????????????????????

??????????????

??????????????????????????????????????????????????????

  • ??????? UI?
  • ?????????????????
  • ?????????????

??????? UI

????????????????????? UI ?????????????????????????????????????????????? (?? Windows ??????)??????????????

[? 2] Windows ?????????????????

  • ?????????
  • ???????????
  • ?????????????????????
  • ???????????????????????
  • ???????????????????
  • ???????????????????????

?????????????????????????????? UI ???????????????????????????????????????????????????????????????????????????????????????????????????????

??????????????????????????????????????????????????????????????????????????????

?????????????

??????????????????????????????????????????????????????????? Application.Files ??????????????????????????????????????? UI??? UI?????????????????????????????????????????????????????????????????????????????????? Application.Files.Add( new File( ... ) ) ??????

[? 3] Visual Studio .NET ????

???????? UI ????????????????? Visual Studio ????? (????????)??????????????????????????? Application.Files ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Application.Files ??????????????????????????????????????????????????????????????????????????????????????????????? File ????????????????????????????????????????????????????????????????????

????????????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??????????????????????????????????????????????????????????????????????????????????????????????? (Dirty)?????????????????????????????????????????????? DEL ????????????????????????????????????????????

AskForSave( file );
DeleteTreeNode( file );
CloseOpenDocument( file );
RemoveMenuItem( windowMenu, file );
UpdateRecentFiles( fileMenu, file );
KillFile( file );
MakeProjectDirty( project );

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????

  1. ??????????????????????????????????
  2. ????????????????????????????????????
  3. ???????????????????
  4. ???????????????????????
  5. ??????????????????
  6. ?????????????? Application.Files ????????????????????????
  7. ???????????????

????????????????????? (? Application ????)???????????????????????????????????????????????????????????

???-??-????????

???? MVC ?????????????????????????? MVC ?????????????????-??-????????????

  • ???????????????????????????
  • ??????????????????????????????????
  • ????????? UI ??????????????????????????????????????????????????????????
  • ??????????????????????????????? UI ?????????????? (??) ??????????Car.Started ????? True???????????????????????? Car.Accelerator=1000 ????????
  • ???????????????????????????????????????????Car.Started = false??

[? 4] MVC ??????

MVC ?????????????????????????????????????? MVC ?????????http://java.sun.com/blueprints/patterns/MVC.html ?? MVC ?????????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???-??-???????????????????????????????????????????????

???????????Application.Files ?????????????????????????????????????? MVC ?????????????????????????????????????????? UI??????????????????????????????????????????????????????????????????????????????????????????? UI ????

[? 5] ?????????? MVC

?????????????????? Application ??????????????????? UI ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? UI ?????????????? UI ??????????????????????????????????????????? Microsoft ???????????????????????????????????????????????? UI???????????????????????? .NET Framework ???????????????????????????????????????????

Smart Editor ???

???????????????Smart Editor?(??????) ?????????????????????????????????????????????????????? Visual Studio IDE ????????????? Visual Studio ??????? (? Microsoft Word ??????) ?????????????????? Microsoft ????????????????????????????????????????????????????????????????

[? 6] Smart Editor ? UI

???????????????????

?????????????????????? Microsoft Word ????????? Application???????? Application ??? .NET Framework ? Application ???????System.Windows.Forms.Application ? Windows Form ???????? Application ??? Editor.ObjectModel.Application???????????????Application?????????????? Windows Form ??????????????????????

using Application = Editor.ObjectModel.Application;

??????????Application??? Editor.ObjectMode.Application??? Windows Form ??????????

[? 7] Smart Editor ????

????????????? UI ???????? UI ??????

[? 8] Smart Editor ??

[? 8] ??? UI ???????????????????????????????? UI ????????????????????????? Document Explorer??????????????????????????????

Application

??????????????????????????????????

private Application()
{
   this._Tabs = new TabCollection( this );
   this._ToolBars = new ToolBarCollection( this );
   this._Menus = new MenuCollection( this );
   this._Documents = new DocumentCollection( this );
}

?????????????????????????????

???????????? UI ????? UI ?????????? Toolbars ? Menus ???

[? 9] ?? UI ???????

Document ??

Application ????????? Documents ???????????? DocumentCollection ????????????? SelectableCollectionBase ??????????????????? Selected ????????????????????? Listbox ? Treeview ???? Items ??????????????????????????? Selected ???????????????????

?????????? SelectableCollectionBase ?????? DocumentCollection ????????????????????? Document ?????????

public class DocumentCollection : SelectableCollectionBase
{
   new public int Add( Document doc )
   {
      return base.Add( doc );
   }

   new Document this[ int index ]
   {
      get { return (Document)base[ index ]; }
   }

   new public Document Selected
   {
      get { return (Document) base.Selected; }
      set { base.Selected = value; }
   }

   public Document New( string name, string path, byte [] data, IDocumentEditor editor )
   {
      return new Document( name, path, data, editor, null );
   }

   public DocumentCollection( object parent ) : base( false, parent ) { }
}

?????????? true/false ??? base( IsMultiSelect, ParentObject )?????????????????????????????????????????????????????????

Document ??????????? ItemBase (????????????????) ????? (????)?????? base(parent)??????????????

public class Document : ItemBase
{
   private string _Name;
   private string _Path;
   private byte [] _Data;
   private IDocumentEditor _DocumentEditor;

   public IDocumentEditor DocumentEditor { ... }

   public byte[] Data  { ... }

   public string Name { ... }

   public string Path { ... }

   public Document( 
      string name, string path, byte [] data, 
      IDocumentEditor editor, object parent ) 
      : base( parent )
   {
      ...
   }

}

Menu ??

Menu ????? Menu ?????????????????????????????????? CollectionBase ??? SelectableCollectionBase?????????????? SelectableCollectionBase (?? CollectionBase)????????

?? UI ?????????????????????? Menu ????????[??] ?[??]?[??]?[??] ??????????? Application.Menus ?????????????

MenuItem ??

?????????? MenuItem ?????????????????????

MenuItem fileNew = Application.Menus[ "File" ].Items["New"];
fileNew.Click();

??????? -> ???????????????????????? Click ????????????????????????????????????

Tab ??

??????????????????????????????????????? Application ??? Tabs ??????????????

[? 10] ?????????

??????????????????????????????????????Tab ???????????

???????? Tab ??? Show ???????????????????? Close ????????

Toolbar ??

UI ????????? Applications.Toolbars ????????????? Toolbar ?????????? Application.Toolbars[ index ] ???????????? Toolbar ??? Add ???? Run Time ???????

??????????????????????????????????????????????????Toolbar ??? Items ?????????? ToolbarItem ????????????????? New ??????

// Get the first toolbar in the collection and the first toolbar item
ToolbarItem newButton = Application.Toolbars[ "Standard" ].Items[ "New" ];
newButton.Click();

??????? Click ?????????????

?????????

?????? Application.Quit()????????????? Application.Save()???????????? (?????)?Application.ActiveDocument ??????????????????????? Document Explorer ????????????????Application.ActiveTab ????????

???????????

??????????????????????????????????????????????????????????????????????????????????????????????????

???

?????????????? Application ????? UI ???????? Application.OnFileSaveDialog?Application.ShowStatus ???????????????????? UI ????????????????????? Application ?????? UI ?????????????????????????????????????????????????????????????????????? UI ?????????? UI ????????????????????????????????????????? Application ??????????????????

????

??????????????????????????????????????????? Menu?Toolbar ? ToolbarItems ???????? UI ??????????

?????????????? Toolbar ???????????

standardToolBar = Application.ToolBars.New( "Standard" );
Application.ToolBars.Add( standardToolBar );

???????????????? ToolbarItems?

ToolBarItem itemNew = standardToolBar.Items.New( "New", string.Empty, btnNew );
standardToolBar.Items.Add( itemNew );
itemNew.OnClick += new ToolBarItemClickHandler(itemNew_OnClick);

?????????????? Menu ? MenuItem ????????File ???????????

Menu fileMenu = Application.Menu.New( "File", string.Empty, this.mnubarStandard, null );
Application.Menu.Add( fileMenu );

??????????????????????

  1. ????????? Menu?MenuItem?Toolbar ? ToolbarItems ????????????????????????????????????????????? UI ??????Toolbar ?????? OnChange ????????????????????????????????????????????????? UI ??
    private void toolbar_OnChange(ItemBase item, StringArgs s)
    {
       if( item is ToolBarItem )
       {
          ToolBarItem toolBarItem = item as ToolBarItem;
          
          ButtonItem button = toolBarItem.Tag as ButtonItem;
          
          button.Enabled = toolBarItem.Enabled;
          button.Visible = toolBarItem.Visible;
          button.Checked = toolBarItem.Selected;
       }
    }
    
  2. ??????????????? Menu?MenuItem?Toolbar ? ToolbarItems ??????????????????????????????UI ??????????????? Toolbar??????? MenuItem ???? Menu ??? Items ??????????????? UI ????

?????????????????????????????????????? UI???? UI ????????????

????

?????????????????????? Application.Documents ???????????????????? Applications.Tabs ???????? Application.Tabs ?????? Tab ???Tab ?????????????????????? UI ??

Application.Tabs.OnItemCollectionAdd += new CollectionAddHandler( Tabs_OnAdd );
...
...
private void Tabs_OnAdd( CollectionBase collection, ItemBase item )
{
   if( item is Tab )
   {
      CreateNewTab( (Tab) item );
   }
}

??????? Document ??? Show ????????????? doc.Show()???????????????????? Tabs ??????Document ?????????

Document Explorer ????

?????????????????????? Document Explorer ?????????????? Application.Documents ???????????????

  1. ?? Application.Documents ???????????????????????????????????????????????????? DocumentCollection ????? Document?????????????
    private void Documents_OnItemCollectionAdd(CollectionBase collection, ItemBase item)
    {
       if( item is Document )
       {
          Document doc = item as Document;
    
          DocumentNode node = new DocumentNode( doc );
    
          // There's a tree view control named "treSolution" on the UI
          treSolution.Nodes[0].Nodes.Add( node );
       }
    }
    
  2. ???????????????????????????????? Application.Documents ??????????????? DEL ??????? Treeview Keypress ????????????????
    private void treSolution_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
       DocumentNode node = treSolution.SelectedNode as DocumentNode;
       if( null != node )
       {            
          if( e.KeyCode == Keys.Delete )
          {
             Application.Documents.Remove( node.Document );
          }
          else if( e.KeyCode == Keys.Enter )
          {
             node.Document.Show();
          }
       }
       
    }
    

?????? Document ?????????????????????????????????????????????????????????????

??????

??????????????? [????] ???????????

[? 11] ????? [????] ????????

????????? Click ????? Button ?? (??????? [????] ??) ? Click ????????????????? Click ??????????????? Application.Open()?????????? OnOpen ??????????????? .NET Framework [????] ?????????????????????????????????? Document ????? Application.Documents ???????????Document Explorer ??????????????????????????????????? Show() ??????????? OnShow ?????????????? Document Tabs ????????????????????????????? Tab ???Application.Tabs ??????? Tab ???Document Tabs ?????????????????????????Tabs ????????? Tab ?????????????????? Tab ???????????????????????

Framework ??

????????????????? Framework ?????? ItemBase?CollectionBase ? SelectableCollectionBase?????????????????????????????????????????????????????????????????????????DocumentCollection ????? CollectionBase?CollectionBase ??????????????????????????????????????????????????????????????????????????????????????? (Observer)?

[? 12] ?????????

????????????????????????????????????????????????????????????????????????????

[? 13] ???????????????

???????????????????????????????????????

??????????????? [??] ???? [??] ?????????? MenuItem ???? MenuItemCollection ???????? Menu ???????????? MenuCollection ???????????????????????????????????????????????? Application.Menus?????????????????????????

ItemBase

????????? Document?Toolbar?ToolbarItem ????????????????????????????????????

?????????????????? ListenCollection????????????????????????? Menu ????????? (???MenuItemCollection) ??????????????????????????????????????????????????????????? ListenCollection ????????????????????????????????????????????????????????

CollectionBase

????????? ItemBase ????????????????????? DocumentCollection?ToolbarCollection ? MenuCollection ????????????????????????????????????????????? ItemBase ????????????????????????????????????????????????????????????????? OnChange ??????? OnItemChange(item) ????????????????????????????????????????????????????

SelectableCollectionBase

??????? CollectionBase????? Selected ????? SelectedItems ???ItemBase ?? Selected ??????? True ??????????????????? Selected ?????????????????? Collection ??? SelectedItems ????

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????????

???????????????????????????

  1. ? ItemBase?CollectionBase ? SelectableCollectionBase ????????
  2. ?????? Application ???????????????????????????????????
  3. ??? Person?Contact?File?Relationship ?????????? ItemBase ?????????
  4. ?? UI ??????? Window?Toolbar?Menu?Menuitem?DockingPane ?????????????? ItemBase????????????????????? UI?
  5. ?????????????PersonCollection?ContactCollection ? WindowCollection?????????? CollectionBase ? SelectableCollectionBase?
  6. ???????? Application ???????????????????? Persons?Contacts ? Windows???????????? ActivePerson?CurrentContact ? ActiveWindow ???
  7. ? UI ????????????

    (1) ???????????????? UI ?????????????? Button ????? OnClick ????????????? UI ????????????

    (2) ??? UI ???????????????????????????????????????????????? Click()?

???????

?????????????????????????????????????????????????????????????????????????????????????????????????????????????? NewFileHandler????? TextFileLoader???????????????????????????????????????????????????????????????????????????????? New ? Open ?????????????????? Click ????????? Application.New() ? Application.Open()??????????????????? Application.OnNew ? Application.OnOpen/OnSave ??????????????????????????????????????????????? NewFileHandler ??????

public class NewFileHandler
{
   public NewFileHandler()
   {
      Application.OnNew += new EventHandler(Application_OnNew);
   }

   private void Application_OnNew( object source, EventArgs e )
   {
      // A new file needs to be opened
      
      // Create a blank document with some dummy text
      byte [] data = System.Text.Encoding.UTF8.GetBytes(string.Empty);

      string newName = this.MakeNewDocumentName();
      Document doc = new Document( newName, string.Empty, data, 
new TextDocumentEditor(), null );
      Application.Documents.Add( doc );

      // Show the editor
      doc.Show();

      Application.ShowStatus( this, "New file created." );
   }
}
// When the main form loads, it is initialized this way
new NewFileHandler();

????TextFileLoader ??????? Application.OnOpen ? Application.OnSave ????????????????????????????????? Application ??? OnOpen ? OnSave ??????? BitmapFileLoader?MusicFileLoader ? WordFileLoader ??????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????

?????? Windows ???????????????????????????????????????????Visual Studio IDE ?? [????]?????????????????????

[? 14] Visual Studio ????

???? VBA ?????? IDE?????????????????????????????????????????????????????????? Application ??????????????? UI ??????????????? C# ?????????????????????????????????????????????

???????????????? System.CodeDom.Compiler ???????????? C# ???????????????????????????????

using( CSharpCodeProvider provider = new CSharpCodeProvider() )
{
   ICodeCompiler compiler = provider.CreateCompiler();
   ...
   string fullSource = this.MakeCode( code );
   CompilerResults results = compiler.CompileAssemblyFromSource(options, fullSource);
   ...
   try
   {
      Assembly assembly = results.CompiledAssembly;
      Type type = assembly.GetType( NAMESPACE_NAME + "." + CLASS_NAME );

      object obj = Activator.CreateInstance( type );
      MethodInfo method = type.GetMethod(METHOD_NAME);
      method.Invoke( obj, null );
   }
   ...
}

????????????????????????????????????????????????????????????????????????????????

Smart Editor ??????????

[? 15] Smart Editor ????

????????????????????????????????????????????????? 10 ??????????? 10 ????????????????? C# ?????????????????????????

???????????????????????? Microsoft Word VBA ???????????????????????????????????????????????

??

????????????????????????????? UI ?????????????????????????????????????????????

?


????

Omar AL Zabir ? American International University Bangladesh (http://www.aiub.edu/) ????????????????????????????? .NET Framework (Beta 1 ?)???????? Web ??????????????????????? Orion Technologies ??????????????????????????????????????? Microsoft ????????? http://www.oazabir.com/?

?? ??
--%>