MSDN Magazine - December 2007 - (Page 62) translate transforms to every child, and calculate how wide they need to be. In this sample, I scale the child button that is directly under the mouse to be larger than the others. The two buttons on either side of this are smaller, but still larger than all the other buttons. The remaining buttons are equal in size across the space left over. For further details, you can examine the code supplied in the download that accompanies this article on the MSDN® Magazine Web site (msdn.microsoft.com/msdnmag/code07.aspx). The net result is that with each mouse movement, I adjust the size of the buttons to provide the familiar fish-eye effect. Connecting the WPF UserControl to VSTO The basic VSTO solution is a Word 2007 add-in called WordImageSelecter. Once the initial add-in project is created, I can add the WPF UserControl project to the solution. This allows me to take advantage of the enhanced design-time support in Visual Studio 2008 for integrating WPF controls within Windows Forms projects. The next step is to create a simple Windows Forms UserControl within the add-in project. This will eventually host the custom WPF UserControl. In Visual Studio 2008, any WPF UserControl in any project in your solution will show up in the ToolBox, so you can drag and drop it directly onto the Windows Forms UserControl design surface, as shown in Figure 5. This action generates all the code needed to host the WPF control in the Windows Forms control, specifically via an ElementHost object. The ElementHost is designed for integrating Windows Forms and WPF. In this example, the Windows Forms UserControl hosts the ElementHost, which in turn hosts the WPF UserControl. This also adds the required references to the custom WPFFishEye.dll, as well as to the standard PresentationCore, PresentationFramework, UIAutomationProvider, WindowsBase, and WindowsFormsIntegration DLLs. The Windows Forms control is very simple—its only purpose is to host the WPF control, so I don’t need to build in any significant functionality. Note that I expose the hosted WPF FishEyeControl as a property so that I can sink the FishEyeEvent exposed by the FishEyeControl (as shown in Figure 6). Now, I must connect the Windows Forms control to a custom task pane in Word. To do this, I need to implement the ThisAddIn_ Startup method to create the custom task pane, specifying the Windows Forms control as the control to host, and sink the custom FishEyeClickEvent: private WFControl wfControl; private void ThisAddIn_Startup(object sender, System.EventArgs e) { Microsoft.Office.Tools.CustomTaskPane taskPane= this.CustomTaskPanes.Add(new WFControl(), “ImageSelecter”); wfControl = (WFControl)taskPane.Control; wfControl.FishEye.FishEyeClickEvent += new FishEyeEvent(FishEye_FishEyeClickEvent); taskPane.Visible = true; } In the implementation of the FishEyeClickEvent handler, for now I can simply display a message box. If you’re following along, you could test the add-in at this point because the custom task pane and WPF control pieces are complete: private void FishEye_FishEyeClickEvent(object source, FishEyeEventArgs e) { System.Windows.Forms.MessageBox.Show(e.ButtonName); } Figure 5 WPF Control Windows Forms Design Surface Figure 6 Initialize the Control private System.Windows.Forms.Integration.ElementHost elementHost; private WPFFishEye.FishEyeControl fishEye; public WPFFishEye.FishEyeControl FishEye { get { return fishEye; } } private void InitializeComponent() { this.elementHost = new System.Windows.Forms.Integration.ElementHost(); this.fishEye = new WPFFishEye.FishEyeControl(); this.elementHost.Dock = System.Windows.Forms.DockStyle.Fill; this.elementHost.Child = this.fishEyeControl; this.Controls.Add(this.elementHost); } You can use this same general technique for placing WPF controls anywhere in VSTO solutions you are allowed to put Windows Forms controls—in arbitrary Windows Forms dialogs, in app-level custom task panes, in the document actions pane, in Outlook custom form regions, and on the surface of a Word or Excel document. Note that for a Word or Excel document, you can use the regular design-time support for building a Windows Forms control that incorporates your WPF control, but then you have to stop short of actually putting the control on the document surface at design time, because the special VSTO Excel and Word designers don’t support it. That said, programmatically adding a hosted WPF control to a VSTO document solution is really no effort at all. The VSTO runtime infrastructure already supports any arbitrary Windows Forms control, including controls that host WPF controls. The runtime is already in place and has been since Visual Studio 2005. So, the only programmatic task you have left is to instantiate the outer Windows Forms control (that’s hosting your WPF control, via ElementHost) and add it to the solution. private void Sheet1_Startup(object sender, System.EventArgs e) { string controlName = “MyWfControl”; WFControl wfControl = new WFControl(); wfControl.Tag = Controls.AddControl(wfControl, 50,50, 208,250, controlName); wfControl.Name = controlName; } 62 msdnmagazine Office Services http://msdn.microsoft.com/msdnmag/code07.aspx
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.