Re: Microsoft Application Blocks User Interface Process
From: hayrob (straiton_at_nospam.nospam)
Date: 03/25/05
- Previous message: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- In reply to: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Next in thread: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Reply: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 25 Mar 2005 10:09:03 -0000
Peter
I attach the "basic" classes I have developed for extending the UIP. These
have to be written in C#, Since I'm happier working in VB.Net, and Word and
C# is a pig, I have written some VB.net classes which inherited from them.
These do much more interessting things and interact with the WinForm views.
I've sorted my problem on activating Word documents, but I still feel that
my implementation is a bit naïve and really I wondered if anyone else has
tried to do what I'm trying to do and can give me some tips on the best way
to do it.
Thanks for your interest.
Robin Hay
//===============================================================================
// Microsoft User Interface Process Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/uip.asp
//
// WordView.cs
//
// This file contains the implementations of the WordView class
//
// For more information see the User Interface Process Application Block
Implementation Overview.
//
//===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//==============================================================================
using System;
using System.Windows.Forms;
using System.Xml;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace Microsoft.ApplicationBlocks.UIProcess
{
#region WordView class definition
/// <summary>
/// Represents a view used in Windows applications.
/// </summary>
public class WordView : IView
{
#region Declares variables
private const string WORD_APP = "WordApp";
private ControllerBase _controller;
private Guid _taskId;
private string _navigationGraph;
private string _viewName;
private Navigator _navigator;
#endregion
/// <summary>
///
/// </summary>
public delegate void ActivatedEventHandler(object sender,EventArgs e);
/// <summary>
///
/// </summary>
public delegate void ClosedEventHandler(object sender,EventArgs e);
/// <summary>
///
/// </summary>
public event ActivatedEventHandler Activated;
/// <summary>
///
/// </summary>
public virtual event ClosedEventHandler Closed;
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
public WordView()
{
}
#endregion
#region IView implementation
/// <summary>
/// Gets the task identifier related to this view.
/// </summary>
public Guid TaskId { get { return _taskId; } }
/// <summary>
/// Gets the view name.
/// </summary>
public string ViewName { get { return _viewName; } }
/// <summary>
/// Gets the view controller.
/// </summary>
public ControllerBase Controller
{
get
{
return _controller;
}
}
/// <summary>
/// Gets the <see cref="Microsoft.ApplicationBlocks.UIProcess.Navigator"/>
associated with this WordView.
/// </summary>
public Navigator Navigator
{
get { return _navigator; }
}
/// <summary>
/// True enables the WordView. False disables the WordView.
/// </summary>
/// <param name="enabled"></param>
public virtual void Enable(bool enabled)
{
}
/// <summary>
/// Initializes the WordView.
/// </summary>
/// <param name="args">The initialization arguments.</param>
/// <param name="settings">The settings for the view.</param>
public virtual void Initialize(TaskArgumentsHolder args, ViewSettings
settings)
{
Word.ApplicationClass wordApp = new Word.ApplicationClass();
_controller.State[WORD_APP] = wordApp;
AddWordAppEventHandlers();
}
protected virtual void AddWordAppEventHandlers()
{
Word.ApplicationClass wordApp =
(Word.ApplicationClass)_controller.State[WORD_APP];
wordApp.ApplicationEvents3_Event_Quit
+= new Microsoft.Office.Interop.Word.ApplicationEvents3_QuitEventHandler(
wordApp_ApplicationEvents3_Event_Quit);
}
protected virtual object GetWordDocument(Guid id)
{
Word.ApplicationClass wordApp =
(Word.ApplicationClass)this.Controller.State[WORD_APP];
// Create a new Document, by calling the Add function in the Documents
collection
object fileName = "normal.dot"; // template file name
object newTemplate = false;
object docType = 0;
object isVisible = true;
object doc = wordApp.Documents.Add(ref fileName, ref newTemplate, ref
docType, ref isVisible);
((Word.DocumentClass)doc).ActiveWindow.Caption = "Word Document";
return doc;
}
#endregion
#region Internal properties
/// <summary>
/// Gets the current task identifier.
/// </summary>
protected internal Guid InternalTaskId
{
set { _taskId = value; }
}
/// <summary>
/// Gets the current navigation graph.
/// </summary>
protected internal string InternalNavigationGraph
{
set { _navigationGraph = value; }
get { return _navigationGraph; }
}
/// <summary>
/// Gets the view name.
/// </summary>
public string InternalViewName
{
get { return _viewName; }
set {_viewName = value; }
}
internal Navigator InternalNavigator
{
set {_navigator = value; }
get { return _navigator; }
}
internal ControllerBase InternalController
{
set { _controller = value; }
get { return _controller; }
}
#endregion
/// <summary>
///
/// </summary>
public virtual void Activate()
{
Word.DocumentClass doc = (Word.DocumentClass) GetDocument();
doc.ActiveWindow.Activate();
}
protected virtual object GetDocument()
{
// Create a new Document, by calling the Add function in the Documents
collection
object fileName = "normal.dot"; // template file name
object newTemplate = false;
object docType = 0;
object isVisible = true;
Word.ApplicationClass wordApp =
(Word.ApplicationClass)_controller.State[WORD_APP];
wordApp.Visible=true;
Word.DocumentClass doc
= (Word.DocumentClass)wordApp.Documents.Add(ref fileName, ref newTemplate,
ref docType, ref isVisible);
return doc;
}
public virtual void Show()
{
Word.ApplicationClass wordApp =
(Word.ApplicationClass)_controller.State[WORD_APP];
wordApp.Visible = true;
}
private void wordApp_ApplicationEvents3_Event_Quit()
{
_controller.State[WORD_APP] = null;
_controller.CompleteTask();
Closed(this, new EventArgs());
}
}
#endregion
}
//===============================================================================
// Microsoft User Interface Process Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/uip.asp
//
// WinFormViewManager.cs
//
// This file contains the implementations of the WordViewManager class
//
// For more information see the User Interface Process Application Block
Implementation Overview.
//
//===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//==============================================================================
using System;
using System.Collections;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace Microsoft.ApplicationBlocks.UIProcess
{
#region WordViewManager class definition
/// <summary>
/// Provides methods to manipulate Windows Form views.
/// </summary>
public class WordViewManager : IViewManager
{
#region Declares variables
private const string CommaSeparator = ",";
private const string WORD_APP = "WordApp";
//Stores active views
private static WordView _activeView;
private static Guid _taskId;
//Stores active views
private static Hashtable _properties = new Hashtable();
#endregion
protected WordView ActiveView
{
get
{
return _activeView;
}
set
{
_activeView = value;
}
}
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public WordViewManager( )
{
}
#endregion
#region IViewManager Members
private object GetProperty( Guid taskId, string name )
{
Hashtable taskProperties = (Hashtable)_properties [taskId];
if( taskProperties != null )
return taskProperties [name];
else return null;
}
/// <summary>
/// Stores a property in the view manager.
/// Each task has its own properties.
/// </summary>
/// <remarks>Property storage is a view manager responsibility.</remarks>
/// <param name="taskId">The task identifier (a GUID associated with the
task).</param>
/// <param name="name">The property name.</param>
/// <param name="value">The property value.</param>
public void StoreProperty( Guid taskId, string name, object value )
{
if( _properties [taskId] == null )
_properties [taskId] = new Hashtable();
((Hashtable)_properties [taskId])[name] = value;
}
/// <summary>
/// Activates a specific view.
/// </summary>
/// <param name="previousView">The view currently displayed.</param>
/// <param name="view">The name of the view to be displayed next.</param>
/// <param name="navigator">The navigator.</param>
public void ActivateView( string previousView, string view, Navigator
navigator )
{
ActivateView(previousView, view, navigator, null);
}
/// <summary>
/// Activates a specific view with activation arguments.
/// </summary>
/// <param name="previousView">The view currently displayed.</param>
/// <param name="view">The name of the view to be displayed next.</param>
/// <param name="navigator">The navigator.</param>
/// <param name="args">The arguments for the next view.</param>
public void ActivateView( string previousView, string view, Navigator
navigator, TaskArgumentsHolder args )
{
if (_activeView == null)
{
_taskId = navigator.CurrentState.TaskId;
CreateNewView(view, navigator, _taskId, args);
}
else
{
_activeView.Activate();
}
}
private ViewSettings CreateNewView(string viewName, Navigator navigator,
Guid taskId, TaskArgumentsHolder args)
{
//Create a new instance
ViewSettings viewSettings =
UIPConfiguration.Config.GetViewSettingsFromName(viewName);
if( viewSettings == null )
throw new UIPException( Resource.ResourceManager.FormatMessage(
Resource.Exceptions.RES_ExceptionViewConfigNotFound, viewName ) );
WordView view = (WordView)GenericFactory.Create(viewSettings);
ActivateForm(view, viewSettings, navigator, taskId, null, args);
return viewSettings;
}
private WordView ActivateForm(WordView wordView, ViewSettings viewSettings,
Navigator navigator, Guid taskId, string previousView, TaskArgumentsHolder
args)
{
wordView.InternalTaskId = taskId;
wordView.InternalNavigationGraph = navigator.Name;
wordView.InternalViewName = viewSettings.Name;
wordView.InternalNavigator = navigator;
ControllerBase controller = navigator.GetController(wordView);
wordView.InternalController = controller;
wordView.Initialize(args, viewSettings);
AddWordViewEventHandlers(wordView);
wordView.Activate();
return wordView;
}
protected virtual void AddWordViewEventHandlers(WordView wordView)
{
wordView.Activated += new
WordView.ActivatedEventHandler(Document_Activated);
wordView.Closed += new WordView.ClosedEventHandler(WordView_Closed);
}
/// <summary>
/// Required for Web applications only.
/// <remark>Not implemented in WinFormViewManager.</remark>
/// </summary>
/// <param name="view"></param>
/// <param name="stateViewName"></param>
/// <returns></returns>
public bool IsRequestCurrentView( IView view, string stateViewName )
{
// Not implemented. In Windows Forms applications it is not necessary.
return true;
}
/// <summary>
/// The number of currently active views.
/// </summary>
/// <returns>The view count.</returns>
public int GetActiveViewCount()
{
if (_activeView == null)
{
return 0;
}
else
{
return 1;
}
}
/// <summary>
/// Gets the running tasks in the manager.
/// </summary>
/// <returns>An array with the task identifiers.</returns>
public Guid[] GetCurrentTasks()
{
ArrayList tasks = new ArrayList();
tasks.Add( _taskId );
return (Guid[]) tasks.ToArray( typeof(Guid) );
}
/// <summary>
/// Required for Web applications only.
/// <remark>Not implemented in WinFomViewManager.</remark>
/// </summary>
/// <param name="currentView">The current view.</param>
/// <returns></returns>
public string GetViewNameForCurrentRequest( IView currentView )
{
return null;
}
/// <summary>
/// For a given task, returns a hash table composed of WinFormViews keyed by
name.
/// </summary>
/// <param name="taskId">The task identifier (a GUID associated with the
task).</param>
/// <returns>A collection of IViews.</returns>
public static Hashtable GetCurrentViews(Guid taskId)
{
Hashtable views = new Hashtable();
views[_activeView] = _activeView.ViewName;
return views;
}
#endregion
#region WordView Event Handlers
/// <summary>
/// Updates the current view.
/// </summary>
protected virtual void Document_Activated( object source, EventArgs e)
{
}
/// <summary>
/// Removes the closed document from the collection of active documents.
/// </summary>
protected virtual void WordView_Closed( object source, EventArgs e)
{
//Remove the view
_activeView = null;
UIPManager.OnCompletion();
}
#endregion
}
#endregion
}
""Peter Huang" [MSFT]" <v-phuang@online.microsoft.com> wrote in message
news:Zd7QWsQMFHA.3984@TK2MSFTNGXA03.phx.gbl...
> Hi
>
> Thanks for your quickly reply!
> I have not used UIP with Word Application.
> From your description it seems that you have customized the UIP, so can
> you
> send me a reproduce sample to reproduct the issue, so that I can look into
> it to see what is the problem.
>
> Also based on my experience, for such automation out of process in windows
> application and if you have another thread which will access the
> automation
> instance in the winform main thread, we usually need to make a invoke
> marshal. So that the call will be run at the winform main thread which you
> call invoke at.
>
> Control.Invoke Method
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
> frlrfsystemwindowsformscontrolclassinvoketopic.asp
>
> Best regards,
>
> Peter Huang
> Microsoft Online Partner Support
>
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
- Previous message: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- In reply to: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Next in thread: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Reply: Peter Huang: "Re: Microsoft Application Blocks User Interface Process"
- Messages sorted by: [ date ] [ thread ]