DrawLine()位于标签上方

时间:2018-04-23 22:21:21

标签: c# winforms graphics labels

我试图在标签上使用DrawLine(),但看起来标签总是位于绘制线的顶部。我尝试了很多东西,比如Bring to Front,Send to Bottom,在我画线后使用label1.show()。这些都没有奏效。附图说明了我遇到的问题。关于如何将线放在最上面的任何输入将不胜感激。

DrawLine() on top of a label

1 个答案:

答案 0 :(得分:1)

试试这段代码,首先加载所有标签make to invisible,然后在paint form事件中,在绘制线之前绘制文本。

private void Form1_Paint ( object sender , PaintEventArgs e ) {     

            foreach (var ctl in this.Controls ) {
                if(ctl.GetType () == typeof ( Label ) ) {
                    e.Graphics.DrawString ( ( ( Label ) ctl ).Text , ( ( Label ) ctl ).Font , new SolidBrush ( ( ( Label ) ctl ).ForeColor ) , ( ( Label ) ctl ).Location  );
                }
            }


            using ( var pen = new Pen ( Color.Blue , 10 ) ) {
                e.Graphics.DrawLine ( pen , 0 , 0 , 200 , 100 );
            }
        }

        private void Form1_Load ( object sender , EventArgs e ) {
            foreach ( var ctl in this.Controls ) {
                if ( ctl.GetType ( ) == typeof ( Label ) ) {
                    ( ( Label ) ctl ).Visible = false;
            }
        }

结果: enter image description here