This topic demonstrates how to dock an instant messaging (IM) conversation window in a Windows Form object using .NET managed code with Microsoft Lync 2010 SDK.

Prerequisites

For a list of prerequisites, see Walkthrough: Start an Instant Message Conversation .

Creating the Conversation Window Docking Application

To create this application, perform many of same tasks used to create an IM conversation as described in the topic Walkthrough: Start an Instant Message Conversation . Then, add the ConversationWindow . . :: . . Dock method, add event handlers to resize and set focus to the docked ConversationWindow , and use delegates to handle marshaling of event data from the Lync thread to your UI thread.

Tip Tip

You can also dock a ConversationWindow representing a conversation started by a remote user. In that case, you pass the Conversation instance into GetConversationWindow and an instance of ConversationWindow is returned. You can dock the returned ConversationWindow in your application. For information about accepting an incoming conversation, see Walkthrough: Respond to a Conversation Invitation

To create the IM conversation window docking Windows Forms application

  1. Sign in to Microsoft Lync 2010.

  2. In Microsoft Visual Studio development system, create a new Windows Forms application.

  3. Select .NET Framework 3.5 or 4.0 as the target framework. For more information, see the MSDN topic How to: Target a Specific .NET Framework.

  4. Add a reference to Microsoft.Lync.Model in your project reference folder. Browse to the C:\Program Files (x86)\Microsoft Lync\SDK\Assemblies\WPF\ folder and choose Microsoft.Lync.Model.dll.

  5. In Form1.cs add the following using statement.

      Copy imageCopy Code
    using Microsoft.Lync.Model;
    using Microsoft.Lync.Model.Conversation;
    using Microsoft.Lync.Model.Conversation.Extensibility;
    
  6. At the top of the Form class, define the following declarations.

      Copy imageCopy Code
    //These delegates are used call methods on the UI thread.
    private delegate void FocusWindowDelegate();
    private delegate void ResizeWindowDelegate(System.Drawing.Size
    resizeTo);
    private delegate void DockWindowDelegate(ConversationWindow
    _ConversationWindow);
    
    //This window will be docked to the form.
    ConversationWindow _ConversationWindow = null;
    
  7. In the Form Load event handler, add the following code.

      Copy imageCopy Code
    // Create the major API objects.
    Automation _Automation = LyncClient.GetAutomation();
    
    // Create a generic List object to contain a contact URI.
    List<string> inviteeList = new List<string>();
    inviteeList.Add("elise@contoso.com");
    
    
    // Create a generic Dictionary object to contain context objects.
    Dictionary< AutomationModalitySettings, object>
    _ModalitySettings = new Dictionary< AutomationModalitySettings,
    object >();
    _ModalitySettings.Add(AutomationModalitySettings.FirstInstantMessage,
    "hello");
    _ModalitySettings.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately,
    true);
    
    // Start the conversation.
    _Automation.BeginStartConversation(
    	AutomationModalities.InstantMessage
    	, inviteeList
    	, _ModalitySettings
    	, StartConversationCallback
    	, null);
    
    
  8. Create three #regiondivisions within the form class. Put callback methods, delegated methods, and event handlers in the three regions.

      Copy imageCopy Code
    		#region callback
    		#endregion
    
    		#region Delegated Methods
    		#endregion
    
    		#region Event Handlers
    		#endregion
    
  9. Add the following code into the callback region

    C#  Copy imageCopy Code
    // This callback method appears as a parameter in the
    StartConversaton method.
    private void StartConversationCallback(IAsyncResult asyncop)
    {
    	if (asyncop.IsCompleted == true)
    	{
    		ConversationWindow newConversationWindow =
    _Automation.EndStartConversation(asyncop);
    
    		//Marshal event to your UI thread to cause conversation
    window to dock in your application
    		This.Invoke(new DockWindowDelegate(DockWindow), new
    object[] { newConversationWindow });
    }
    }
    
    
  10. Add the following code to the delegated method region.

    C#  Copy imageCopy Code
    // This method will be passed to a delegate.
    private void SetWindowSize(System.Drawing.Size newSize)
    {
      this.Size = newSize;
    }
    
    // This method will be passed to a delegate.
    private void GetWindowFocus()
    {
      this.Focus();
    }
    
    // This method will be passed to the DockWindowDelegate delegate.
    private void DockWindow(ConversationWindow pConversationWindow)
    {
      //Dock the conversation window.
      _ConversationWindow = pConversationWindow;
    
      //Register for these events to receive necessary notification
    that ConversationWindow size changed.
      _ConversationWindow.NeedsSizeChange +=
    _ConversationWindow_NeedsSizeChange;
      _ConversationWindow.NeedsAttention +=
    _ConversationWindow_NeedsAttention;
      _ConversationWindow.Dock(this.Handle.ToInt32());
    }
    
    
  11. Add the following code to the event handler region

    C#  Copy imageCopy Code
    void _ConversationWindow_NeedsSizeChange(object source,
    ConversationWindowNeedsSizeChangeEventArgs data)
    {
    	System.Drawing.Size windowSize = new Size();
    	windowSize.Height = data.RecommendedWindowHeight;
    	windowSize.Width = data.RecommendedWindowWidth;
    
    	// Execute the delegate on the UI thread.
    	this.Invoke(new ResizeWindowDelegate(SetWindowSize), new
    object[] { windowSize });
    }
    	
    void _ConversationWindow_NeedsAttention(object source,
    ConversationWindowNeedsAttentionEventArgs data)
    {
    	// Execute the delegate on the UI thread.
    	this.Invoke(new FocusWindowDelegate(GetWindowFocus), new
    object[] { });
    }
    
  12. Press F5 to build and run the application.

See Also