Many of you already have a Window Forms application. You can add WPF to your existing application quite easily using the ElementHost class, which can be added as a control to your form.

 

(You can also go the other way: host a Windows Forms control in a WPF element with the WindowsFormsHost , which can be added to your XAML)

 

This will bring you the ability to animate or scale parts of the UI for your applications. Notice how the Winform button rescales when you MouseWheel on the form!

 

The sample below is a Windows Form, which hosts a WPF Canvas, which has a TextBlock, Ellipse, and a WindowFormHost which contains a Windows Form button.

 

(You can also run this code in Foxpro, Excel, VB6! Do you see how?)

 

Start Visual Studio 2008

Choose File->New Project->Visual Basic->Windows Forms Application

Add references to

 

PresentationCore

PresentationFrameWork

WindowsBase

WindowsFormsIntegration

 

Paste in the code, then hit F5 to run.

 

 

The textblock opacity and Ellipse color are animated too. Try MouseWheel to see the objects grow/shrink.

 

 

See also

Animation Overview

WPF Animation: Use dynamic XAML to animate movies using XAMLReader or animate in code using BeginStoryBoard

 

 

 

 

<Sample Code>

Imports System.Windows.Forms.Integration

Imports System.Windows.Controls

Imports System.Windows.Media

Imports System.Windows.Input

 

Public Class Form1

    Sub New()

 

        ' This call is required by the Windows Form Designer.

        InitializeComponent()

 

        ' Add any initialization after the InitializeComponent() call.

 

        Me.Height = 600

        Me.Width = 800

        Dim elemHost As New System.Windows.Forms.Integration.ElementHost

        Me.Controls.Add(elemHost)

        elemHost.Top = 30   ' move down a little bit

        'see http://blogs.msdn.com/calvin_hsia/archive/2007/11/29/6600915.aspx

        Dim xaml = _

        <Canvas Name="MyPanel"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Background="LightGreen"

            >

            <TextBlock Name="MyTextBlock" Canvas.Top="10">WPF Embedded Hi there

                <TextBlock.Triggers>

                    <EventTrigger RoutedEvent="TextBlock.Loaded">

                        <BeginStoryboard>

                            <Storyboard>

                                <DoubleAnimation

                                    Storyboard.TargetName="MyTextBlock"

                                    Storyboard.TargetProperty="(Opacity)"

                                    From="1.0" To=".10" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"/>

                            </Storyboard>

                        </BeginStoryboard>

                    </EventTrigger>

                </TextBlock.Triggers>

            </TextBlock>

            <Ellipse Height="150" HorizontalAlignment="Left" Canvas.Top="30">

                <Ellipse.Fill>

                    <SolidColorBrush x:Name="ebrush" Color="Black"/>

                </Ellipse.Fill>

                <Ellipse.Triggers>

                    <EventTrigger RoutedEvent="Ellipse.Loaded">

                        <BeginStoryboard>

                            <Storyboard

                                TargetProperty="(Ellipse.Width)">

                                <DoubleAnimation From="20" To="200" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever"/>

                                <ColorAnimation

                                    Storyboard.TargetName="ebrush"

                                    Storyboard.TargetProperty="Color"

                                    From="Red" To="Blue" Duration="0:0:3" AutoReverse="True" RepeatBehavior="Forever"/>

                            </Storyboard>

                        </BeginStoryboard>

                    </EventTrigger>

                </Ellipse.Triggers>

            </Ellipse>

        </Canvas>

        elemHost.Child = CType(System.Windows.Markup.XamlReader.Load(xaml.CreateReader), System.Windows.UIElement)

 

        Dim MyCanvas = CType(elemHost.Child, Canvas)

        elemHost.Height = 400

        elemHost.Width = 600

        Dim btn As New Windows.Forms.Button ' This is a WinForms button: not a wpf System.Windows.Controls.Button

        btn.Visible = True

        btn.Text = "Winform Btn to launch WPF form"

        btn.AutoSize = True

        btn.BackColor = System.Drawing.Color.Bisque

        Dim WinFormHost = New WindowsFormsHost

        WinFormHost.Child = btn

        AddHandler btn.Click, AddressOf btn_Click

        Canvas.SetTop(WinFormHost, 80)

        MyCanvas.Children.Add(WinFormHost)

 

        MyCanvas.RenderTransform = New ScaleTransform(1, 1)

        AddHandler MyCanvas.MouseWheel, AddressOf MyCanvas_MouseWheel

 

 

    End Sub

    Sub btn_Click()

        Dim oWPFForm = New WPFForm

        oWPFForm.ShowDialog()

    End Sub

    Sub MyCanvas_MouseWheel(ByVal o As Canvas, ByVal e As MouseWheelEventArgs)

        Dim tr = CType(o.RenderTransform, ScaleTransform)

        If e.Delta > 0 Then

            tr.ScaleX *= 1.1

            tr.ScaleY *= 1.1

        Else

            tr.ScaleX /= 1.1

            tr.ScaleY /= 1.1

 

        End If

 

    End Sub

End Class

 

Class WPFForm : Inherits Windows.Window

    Sub New()

        Dim xaml = _

        <StackPanel

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Orientation="Vertical"

            >

            <TextBlock>WPFForm Hi there</TextBlock>

        </StackPanel>

        Me.Content = System.Windows.Markup.XamlReader.Load(xaml.CreateReader)

    End Sub

 

 

End Class

</Sample Code>