C#如何使用背景透明度绘制用户控件

时间:2013-06-26 12:21:15

标签: c# wpf background user-controls gdi+

我想知道即使在运行时引发或移动用户控件,如何使用background Transparency绘制User Control。 我的代码是

private void UserControl1_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    g.Clear(Color.White);
    g.SmoothingMode = SmoothingMode.HighQuality;//可以反锯齿
    var rectBound = new Rectangle(0, 0, Width-1, Height-1);

    var b = new SolidBrush(Color.FromArgb(0, 122, 204));
    var rect = new Rectangle(2, 2, Width - 4, Height - 4);
    if(!_isSelected)//FillRectangle
        g.FillEllipse(b, rectBound);
    else
        g.FillEllipse(b, rect);

    var pen = new Pen(Color.Yellow);
    pen.DashStyle = DashStyle.DashDot;
    g.DrawLine(pen,10,10,100,10);

    pen.DashStyle = DashStyle.Dash;
    g.DrawLine(pen, 10, 15, 100, 15);

    pen.DashStyle = DashStyle.DashDotDot;
    g.DrawLine(pen, 10, 20, 100, 20);

    pen.DashStyle = DashStyle.Dot;
    g.DrawLine(pen, 10, 25, 100, 25);

    pen.DashStyle = DashStyle.Solid;
    g.DrawLine(pen, 10, 30, 100, 30);

    if (_isSelected)
    {
        pen = new Pen(Color.Black) { DashStyle = DashStyle.Dot, Width = 1 };
        g.DrawRectangle(pen, rectBound);
    }
}

和演示图片enter image description here

会有很多用户控件在运行时创建,其中一些可能重叠,那么应该清楚用户控件的background.how这样做吗?

0 个答案:

没有答案