110,531
社区成员
发帖
与我相关
我的任务
分享
int X = 50;
int Y = 50;
int MoveY = 5;
Y += MoveY;
if (Y >= this.Height)
Y = 50;
有的是从设备传递过来的参数,如果我使用你的控件,按照你的DrawRender函数,那么我需要
Graphics gs = this.你重构的PIC控件.CreateGraphics();
//gs.Clear(Color.Black);
Pen p = new Pen(Color.Red, 10);
gs.FillRectangle(new SolidBrush(Color.Black), 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
gs.DrawRectangle(p, 10f, Y0, 1.5f, 10f);
gs.DrawRectangle(p, 40f, Y1, 1.5f, 10f);
gs.DrawRectangle(p, 80f, Y2, 1.5f, 10f);
gs.DrawRectangle(p, 120f, Y3, 1.5f, 10f);
gs.DrawRectangle(p, 160f, Y4, 1.5f, 10f);
gs.DrawRectangle(p, 200f, Y5, 1.5f, 10f);
gs.DrawRectangle(p, 240f, Y6, 1.5f, 10f);
gs.DrawRectangle(p, 280f, Y7, 1.5f, 20f);
gs.DrawRectangle(p, 320f, Y8, 1.5f, 40f);
gs.DrawRectangle(p, 360f, Y9, 1.5f, 10f);
gs.DrawRectangle(p, 400f, Y10, 1.5f, 10f);
gs.DrawRectangle(p, 440f, Y11, 1.5f, 30f);
gs.DrawRectangle(p, 480f, Y12, 1.5f, 10f);
gs.DrawRectangle(p, 520f, Y13, 1.5f, 10f);
gs.DrawRectangle(p, 550f, Y14, 1.5f, 20f);
gs.DrawRectangle(p, 580f, Y15, 1.5f, 10f);
this.pictureBoxEx21.DrawRender(gs);
this.pictureBoxEx21.Render();
想让你看看这样实现对不对 ,因为我修改了一下控件 发现红色快的坐标不是移动 而是连起来了,
就差那么一丢丢,我就完成了。你这个DEMO确实没有闪烁问题
Graphics gs = this.pictureBox1.CreateGraphics();
//gs.Clear(Color.Black);
Pen p = new Pen(Color.Red, 10);
gs.FillRectangle(new SolidBrush(Color.Black), 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
gs.DrawRectangle(p, 10f, Y0, 1.5f, 10f);
gs.DrawRectangle(p, 40f, Y1, 1.5f, 10f);
gs.DrawRectangle(p, 80f, Y2, 1.5f, 10f);
gs.DrawRectangle(p, 120f, Y3, 1.5f, 10f);
gs.DrawRectangle(p, 160f, Y4, 1.5f, 10f);
gs.DrawRectangle(p, 200f, Y5, 1.5f, 10f);
gs.DrawRectangle(p, 240f, Y6, 1.5f, 10f);
gs.DrawRectangle(p, 280f, Y7, 1.5f, 20f);
gs.DrawRectangle(p, 320f, Y8, 1.5f, 40f);
gs.DrawRectangle(p, 360f, Y9, 1.5f, 10f);
gs.DrawRectangle(p, 400f, Y10, 1.5f, 10f);
gs.DrawRectangle(p, 440f, Y11, 1.5f, 30f);
gs.DrawRectangle(p, 480f, Y12, 1.5f, 10f);
gs.DrawRectangle(p, 520f, Y13, 1.5f, 10f);
gs.DrawRectangle(p, 550f, Y14, 1.5f, 20f);
gs.DrawRectangle(p, 580f, Y15, 1.5f, 10f);
[/quote]
可以的呀,看你自己的需求,你想一个控件或多个都成。。然后BABY归我了。
Graphics gs = this.pictureBox1.CreateGraphics();
//gs.Clear(Color.Black);
Pen p = new Pen(Color.Red, 10);
gs.FillRectangle(new SolidBrush(Color.Black), 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
gs.DrawRectangle(p, 10f, Y0, 1.5f, 10f);
gs.DrawRectangle(p, 40f, Y1, 1.5f, 10f);
gs.DrawRectangle(p, 80f, Y2, 1.5f, 10f);
gs.DrawRectangle(p, 120f, Y3, 1.5f, 10f);
gs.DrawRectangle(p, 160f, Y4, 1.5f, 10f);
gs.DrawRectangle(p, 200f, Y5, 1.5f, 10f);
gs.DrawRectangle(p, 240f, Y6, 1.5f, 10f);
gs.DrawRectangle(p, 280f, Y7, 1.5f, 20f);
gs.DrawRectangle(p, 320f, Y8, 1.5f, 40f);
gs.DrawRectangle(p, 360f, Y9, 1.5f, 10f);
gs.DrawRectangle(p, 400f, Y10, 1.5f, 10f);
gs.DrawRectangle(p, 440f, Y11, 1.5f, 30f);
gs.DrawRectangle(p, 480f, Y12, 1.5f, 10f);
gs.DrawRectangle(p, 520f, Y13, 1.5f, 10f);
gs.DrawRectangle(p, 550f, Y14, 1.5f, 20f);
gs.DrawRectangle(p, 580f, Y15, 1.5f, 10f);
public class PictureBoxEx2 : PictureBox
{
public PictureBoxEx2()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
int X = 50;
int Y = 50;
int MoveY = 5;
public void Render()
{
Y += MoveY;
if (Y >= this.Height)
Y = 50;
this.Invalidate();
}
private void DrawRender(Graphics gs)
{
gs.Clear(Color.Black);
Pen p = new Pen(Color.Red, 10);
gs.DrawRectangle(p, X, Y, 5, 30);
gs.DrawRectangle(p, X + 30, Y, 10, 60);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
DrawRender(pe.Graphics);
}
}
然后在Time_TIck里调用这个
private void timer1_Tick(object sender, EventArgs e)
{
this.pictureBoxEx21.Render();
}
Graphics gs = pictureBox1.CreateGraphics();
//gs.Clear(Color.Black);
Pen p = new Pen(Color.Red, 10);
gs.FillRectangle(new SolidBrush(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height); <-用这样子填充背景就不闪了
gs.DrawRectangle(p, X, Y, 5, 30);
gs.DrawRectangle(p, X + 30, Y, 10, 60);
gs.Dispose();
Y += Move;
if (Y >= this.Height)
Y = 50;
还有我改了PictureBox
窗体控件变成这个
public class PictureBoxEx : PictureBox
{
public PictureBoxEx()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
}