pictureBox1_Paint() doesn't draw
Hi, I am making a scrolling text with transparent background.
However, thepictureBox1_Paint() seem to draw nothing!
Any idea?
public partial class Form1 : Form
{
private String drawString = "Sample Text";
private Font drawFont = new Font("Arial", 31);
private SolidBrush drawBrush = new SolidBrush(Color.White);
private int textX = 0;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
timer1.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX, 0));
}
private void timer1_Tick(object sender, EventArgs e)
{
textX++;
Invalidate();
}
}
[1192 byte] By [
ddlam] at [2007-12-23]
I tested the code above, and it worked fine. Of course, when textX is large enough, the string will be out of the client size.
To debug, try to add MessageBox.Show("pictureBox1_Paint called"); in the pictureBox1_paint function, and have a check.
Thanks gqlu, the "pictureBox1_Paint called" is printed out. The sample text is printed too.
but I suppose it to draw every minisecond
as I have written
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX++, 0));
However, it draw once only.
Hi,
Make sure that the timer is enabled. To demonstrate that the timer is on, add a label in the form, and add the statement "this.label1.Text = this.textX.ToString();" in the pictureBox1_Paint function. Then you can clearly see whether the timer is on.
Thanks gqlu. The timer is also enabled.
I see the problem is that the "sample text" is not drawing in the From1.pictureBox1.
Instead, the text is drawn on the From1 which is covered by the pictureBox1.
Can I draw the text at the toppest component/layer?
or can I make it draw on the pictureBox1?
You also need to add the handler to PictureBox's Paint event, not form's:
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
Andrej