现在的需求需要高度定制,根据动态的配置生成动态的界面,所以网上一般的方式都是无法解决问题的。比如直接wpf控件拉进去使用的方式。
网上的资料太少,都是基于xml布局
做了一半项目发现winform没法实现阴影,结果发现wpf可以设置局部阴影,设置改变阴影颜色。
操作步骤,创建wpf用户控件,然后动态的添加到winform然后,插入一个画版 画板除了可以直接添加按钮外还可以添加一个矩形等等。
<UserControl Name="user_control" x:Class="cn.ijiami.keyboard.wpf.WpfPannelControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:cn.ijiami.keyboard.wpf"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200">
<Canvas Name="wpf_canvas" HorizontalAlignment="Center" Height="200" Margin="0,0,0,0" VerticalAlignment="Center" Width="200" >
</Canvas>
</UserControl>
DropShadowEffect dropShadowEffect = new DropShadowEffect();
dropShadowEffect.Color = Color.FromRgb(255,255,0);
Rectangle rectangle = new Rectangle { Width = 200, Height = 200, Fill = Brushes.Red,Effect= dropShadowEffect };
rectangle.Width = 50;
Thickness marginThickness= new Thickness();
marginThickness.Top = 20;
marginThickness.Bottom = 30;
rectangle.Margin = marginThickness;
rectangle.Height = 50;
currentCanvas.Children.Add(rectangle);
user_control.Background = new SolidColorBrush(Color.FromRgb(0,255,255));
/// <summary>
///
///
/// </summary>
/// <param name="left"></param>
/// <param name="top"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="rectSolidColor"></param>
/// <param name="shadowColor"></param>
/// <param name="shadowX"></param>
/// <param name="shadowY"></param>
/// <param name="shadowBlurRadius"></param>
/// <param name="shadowAlpha"> 透明度</param>
/// <returns></returns>
public static System.Windows.Shapes.Rectangle createDropShadowRectangle(double left, double top,double width, double height, string rectSolidColor, string shadowColor,double shadowX, double shadowY, double shadowBlurRadius, double shadowAlpha)
{
System.Windows.Media.Effects.DropShadowEffect dropShadowEffect = new System.Windows.Media.Effects.DropShadowEffect();
dropShadowEffect.BlurRadius = shadowBlurRadius;// 一个值,该值指示阴影的半径模糊效果。 默认值为 5。 MORE BIG MORE BLUR
dropShadowEffect.RenderingBias = RenderingBias.Quality;
dropShadowEffect.ShadowDepth = Math.Max(shadowX,shadowY);// 纹理下方投影的距离。 默认值为 5。
dropShadowEffect.Opacity = shadowAlpha;
dropShadowEffect.Color = AppUtil.parseColorWpf(shadowColor);// Color.FromRgb(255, 255, 0);
SolidColorBrush fillColor = new SolidColorBrush(AppUtil.parseColorWpf(rectSolidColor));//Brushes.Red
System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle { Fill = fillColor, Effect = dropShadowEffect };
System.Windows.Thickness marginThickness = new System.Windows.Thickness();
marginThickness.Top = top;
marginThickness.Left = left;
rectangle.Margin = marginThickness;
rectangle.Height = height;
rectangle.Width = width;
return rectangle;
//canvas.Children.Add(rectangle);
}
实现的效果如下
![](https://i-blog.csdnimg.cn/blog_migrate/b2b8afc11a55f374f2c7196992e7de67.png)
image.png
这里除了红色部分,其他地方全是通过 winform实现的。