110,536
社区成员
发帖
与我相关
我的任务
分享
class CustomButton : UIElement
{
VisualCollection childs;
DrawingVisual dvrectangle = new DrawingVisual();
DrawingContext dcrectangle;
int i = 0;
Brush bl = Brushes.Blue;
Brush w = new RadialGradientBrush(Colors.White, Colors.Red);
Brush bb = new RadialGradientBrush(Colors.Yellow, Colors.Blue);
Pen p = new Pen(Brushes.Yellow, 3);
MatrixTransform mt = new MatrixTransform();
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("zh-cn");//FormattedText字体参数
FlowDirection fd = FlowDirection.LeftToRight;//FormattedText字体参数
Typeface tf = new Typeface("宋体");//FormattedText字体参数
double d = 25;//FormattedText字体参数
FormattedText ft;
public CustomButton(string s)
{
bl.Freeze();
w.Freeze();
p.Freeze();
ft = new FormattedText(s, ci, fd, tf, d, w);
double fk = ft.Width;
double fh = ft.Height;
childs = new VisualCollection(this);
dcrectangle = dvrectangle.RenderOpen();
dcrectangle.DrawEllipse(bb, p, new Point(40, 40), 40, 40);
dcrectangle.DrawText(ft, new Point(40 - fk / 2, 40 - fh / 2));
dcrectangle.Close();
dvrectangle.Transform = mt;
childs.Add(dvrectangle);
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
public void CompositionTarget_Rendering(object sender, EventArgs e)
{
double n11 = (Math.Sin(i * 0.2) * 0.2 + 1);
double m11 = Math.Cos(i * 0.01) * n11;
double m12 = Math.Sin(i * 0.01) * n11;
mt.Matrix = new Matrix(m11, m12, -m12, m11, 40 * (1 - m11 + m12), 40 * (1 - m12 - m11));
i++;
}
protected override int VisualChildrenCount
{
get { return childs.Count; }
}
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= childs.Count)
{
throw new ArgumentOutOfRangeException();
}
return childs[index];
}
}