none
用按钮在pictureBox1_paint中画一条线,如何实现。谢谢。 RRS feed

  • 问题

  • Button1画的线,当被移动到窗口外时,线条不能重画而被删除,如何使Button1画

    的线条在picturBox1_paint事件中呢。

    Pen p = new Pen(Color.White);//一支白色的笔,用来画直线 private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gp = e.Graphics; for (int i = 0; i <= 156; i = i + 31)//画6根横线 { gp.DrawLine(p, 0, i, 155, i); } for (int i = 0; i <= 156; i = i + 31)//画6根竖线 { gp.DrawLine(p, i, 0, i, 155); } } private void button1_Click(object sender, EventArgs e) { Graphics g = this.pictureBox1.CreateGraphics();//实例化Graphics类 g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿 g.DrawLine(p, new Point(0, 40), new Point(180, 80)); }



    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年6月12日 1:00

答案

  • 之所以你PictureBox中花的线不会删除是因为你每次移动窗体时都会触发PictureBox_Paint事件的,然而Button1的Click事件只会在点击按钮时触发,所以在移动窗体时因为click不会触发所以就不会重绘了,你只需要在Paint事件中触发click事件就可以达到你的效果了,这样在每次触发Paint事件就会触发Click事件,从而达到重绘的效果,具体代码如下:

    Pen p = new Pen(Color.Red);//一支白色的笔,用来画直线 private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gp = e.Graphics; for (int i = 0; i <= 156; i = i + 31)//画6根横线 { gp.DrawLine(p, 0, i, 155, i); } for (int i = 0; i <= 156; i = i + 31)//画6根竖线 { gp.DrawLine(p, i, 0, i, 155); }

    button1.PerformClick();// 加这行代码在你的代码就可以了 } private void button1_Click(object sender, EventArgs e) { Graphics g = this.pictureBox1.CreateGraphics();//实例化Graphics类 g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿 g.DrawLine(p, new Point(0, 40), new Point(180, 80)); }



    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

    2013年6月12日 2:37