简体   繁体   English

交出控件

[英]Drawing over Controls

I have a form where I place a panel. 我有一个放置面板的表格。 In this panel I add some pictureboxes. 在此面板中,我添加了一些图片框。 My problem is that I want to draw lines inside this panel that can overlay the other controls (pictureboxes). 我的问题是我想在此面板内绘制可以覆盖其他控件(图片框)的线条。 The lines must be at the panel bounds 线必须在面板边界处

I tried to draw on form above controls overriding WS_CLIPCHILDREN value with the code below. 我试图利用下面的代码覆盖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;
        }
    }

The problem is occured when the panel scrollbars appear. 当面板滚动条出现时,发生此问题。 The code where i draw is on Form1_Paint event and when I scroll the panel, all of the lines spread in the whole form. 我绘制的代码在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);
        }
    }

The from & to arrays are two List variables where i save the position where the line should start and end. 从&到数组是两个List变量,我在其中保存行应开始和结束的位置。 See the pictures below: 请看下面的图片:

手动滚动之前

滚动后

I don't want those lines in the red circle to show any idea? 我不希望红色圆圈中的那些线显示任何想法?

Try to use the Graphics object that comes with arguments of the Paint event rather than creating own graphics. 尝试使用Paint事件的参数附带的Graphics对象,而不要创建自己的图形。

so that you'd have: 这样您就可以:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM