绘制控件

时间:2015-05-02 07:28:51

标签: c# .net winforms

我有一个表格放置面板。在这个面板中,我添加了一些图片框。我的问题是我想在这个面板中绘制线条,可以覆盖其他控件(图片框)。这些行必须位于面板边界

我尝试使用下面的代码在上面的控件上绘制覆盖WS_CLIPCHILDREN值的控件。

const int WS_CLIPCHILDREN = 0x02000000;


    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style &= ~WS_CLIPCHILDREN;
            //cp.Style &= ~0x04000000; //WS_CLIPSIBLINGS
            //cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT  
            //cp.Style &= 0x7DFFFFFF;

            return cp;
        }
    }

出现面板滚动条时出现问题。我绘制的代码是在Form1_Paint事件上,当我滚动面板时,所有的行都以整个形式传播。

   private void panel3_Paint(object sender, PaintEventArgs e)
    {
        for (int i = 0; i < from.Count; i++)
        {
            AdjustableArrowCap bigarrow = new AdjustableArrowCap(5, 5);
            Pen pen = new Pen(Color.Black, 3);
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.CustomEndCap = bigarrow;
            Graphics g;
            g = this.CreateGraphics();
            g.DrawLine(pen, from[i].X - panel3.HorizontalScroll.Value, from[i].Y - panel3.VerticalScroll.Value, to[i].X - panel3.HorizontalScroll.Value, to[i].Y - panel3.VerticalScroll.Value);
        }
    }

来自&amp;到数组是两个List变量,我保存行开始和结束的位置。 见下图:

Before scroll manually

After scrolling

我不希望红色圆圈中的那些线条显示出来 任何想法?

1 个答案:

答案 0 :(得分:3)

尝试使用Paint事件参数附带的Graphics对象,而不是创建自己的图形。

以便您拥有:

private void panel3_Paint(object sender, PaintEventArgs e)
{
    for (int i = 0; i < from.Count; i++)
    {
        AdjustableArrowCap bigarrow = new AdjustableArrowCap(5, 5);
        Pen pen = new Pen(Color.Black, 3);
        pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
        pen.CustomEndCap = bigarrow;

        var g = e.Graphics;
        g.DrawLine(pen, from[i].X - panel3.HorizontalScroll.Value, from[i].Y - panel3.VerticalScroll.Value, to[i].X - panel3.HorizontalScroll.Value, to[i].Y - panel3.VerticalScroll.Value);
    }
}