一、WPF制作控件然后Winform调用WPF的控件:
1.建立一个WinForm的Application;
2.为inForm程序添加Reference,点击.NET 找到WindowsFormsIntegration这个Reference,加入你的Reference;
3.建立你的WPF UserControl;
4.生成;
二、WPF的UserControl的动画闪烁代码:
<UserControl x:Class="WPFInWinForms.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Grid>
<Grid x:Name="RootElement" RenderTransformOrigin="0.5,0.5" Margin="216,160,263,0" Height="54" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Ellipse Canvas.Left="-5" Canvas.Top="-5" RenderTransformOrigin="0.5,0.5" x:Name="ellipse" IsHitTestVisible="False" Margin="0,0,-1,1" Grid.RowSpan="2">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimation BeginTime="0" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" From="1" To="10" Duration="00:00:01" />
<DoubleAnimation BeginTime="0" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" From="1" To="10" Duration="00:00:01" />
<DoubleAnimation BeginTime="0" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)" From="1" To="0" Duration="00:00:01" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
<Ellipse.RenderTransform>
<ScaleTransform />
</Ellipse.RenderTransform>
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="#00FF0000" />
<GradientStop Color="#FFFF0000" Offset="0.25" />
<GradientStop Color="#00FF0000" Offset="0.5" />
<GradientStop Color="#FFFF0000" Offset="0.75" />
<GradientStop Color="#00FF0000" Offset="1" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Grid>
</UserControl>
在完成上面的第一项后,点生成,左侧的工具箱就有一个UserControl的自写控件,直接拖动即可,这里winform会直接调用elementHost,效果如图:
我这里写的第一个是WPF的动画闪烁的效果,但是在winform中不能显示出来,不知道是WPF哪个属性有问题,还在研究中,第二个控件就可以直接使用。第一个闪烁效果在WPF窗体中是可以使用的,效果如图:
其实在winform可以直接显示GIF图,实现闪烁的效果,用PictureBox控件即可,效果如图: