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
Tip |
---|
You can also dock a
|
To create the IM conversation window docking Windows Forms application
-
Sign in to Microsoft Lync 2010.
-
In Microsoft Visual Studio development system, create a new Windows Forms application.
-
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.
-
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.
-
In Form1.cs add the following using statement.
Copy Code using Microsoft.Lync.Model; using Microsoft.Lync.Model.Conversation; using Microsoft.Lync.Model.Conversation.Extensibility;
-
At the top of the Form class, define the following declarations.
Copy 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;
-
In the Form Load event handler, add the following code.
Copy 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);
-
Create three #regiondivisions within the form class. Put callback methods, delegated methods, and event handlers in the three regions.
Copy Code #region callback #endregion #region Delegated Methods #endregion #region Event Handlers #endregion
-
Add the following code into the callback region
C# Copy 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 }); } }
-
Add the following code to the delegated method region.
C# Copy 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()); }
-
Add the following code to the event handler region
C# Copy 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[] { }); }
-
Press F5 to build and run the application.