Windows Develop Bookmark and Share   
 index > Windows Forms General > C# Forms: Different text size of label and DrawString
 

C# Forms: Different text size of label and DrawString

Platform: VS2005/WinXP/.NET 2.0

I am trying to make one sentence of text scroll from right to left over the form (that works well enough, although not perfect).
Problem is, when I try to add shadow to that text, shadow (drawn with DrawString in OnPaint event) is a bit wider, and it accumulates over time.
Left edge of a shadow is not where it should be - it is more to the left then it should be.
(image can be seen here: "http://i274.photobucket.com/albums/jj262/dzenanz/ScreenShot1.png")

Also, as the whole thing moves to the left, it leaves a nasty trail behind (on the right).
(image can be seen here: "http://i274.photobucket.com/albums/jj262/dzenanz/ScreenShot2.png")

Code Snippet

private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(label1.Text, label1.Font,
new System.Drawing.SolidBrush(Color.FromArgb(64, label1.ForeColor)),
label1.Left + 1, label1.Top + 1);//try changing these to 5 or 10
}

private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Left + label1.Width < 0)
label1.Left = panel1.Width;
else
label1.Left -= 1;
}


As you probably guess, this is just simplified problem in a larger project... Any ideas how to solve abovementioned problems?
Dženan Zukić  Tuesday, February 12, 2008 3:18 PM


By default the Label control uses GDI to draw the text, while the e.Graphics.DrawString() method use GDI+ to draw string,which results in the size difference. You can set the Label.UseCompatibleTextRendering property to TRUE to make the e.Graphics.DrawString() methodto draw a same size of string with the text in the Label.

Zhi-Xin Ye  Friday, February 15, 2008 9:43 AM
Nobody wants to tackle a hard question?
Dženan Zukić  Wednesday, February 13, 2008 7:55 AM
Well, I completele abandoned approach Label for main text + DrawString for shadow, and went for completely custom control (both main text and shadow drawn with DrawString)
Dženan Zukić  Thursday, February 14, 2008 1:11 PM

You should call the Invalidate() method to force the panel to redraw itselft,

private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Left + label1.Width < 0)
label1.Left = panel1.Width;
else
label1.Left -= 1;

this.panel1.Invalidate();
}

Zhi-Xin Ye  Friday, February 15, 2008 8:22 AM
Thanks for trying to be helpfull, but problem is that text within a Label is shorter than text drawn with e.Graphics.DrawString(labelText, labelFont, labelLocation)!

As I noted in earlier post, I completely abandoned that approach, but I ran into new problem, see http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2849789&SiteID=1
Dženan Zukić  Friday, February 15, 2008 9:05 AM


By default the Label control uses GDI to draw the text, while the e.Graphics.DrawString() method use GDI+ to draw string,which results in the size difference. You can set the Label.UseCompatibleTextRendering property to TRUE to make the e.Graphics.DrawString() methodto draw a same size of string with the text in the Label.

Zhi-Xin Ye  Friday, February 15, 2008 9:43 AM
hello
how to write string from righ to left in graphics.drawstring in c#.net
for example for write farsi string
mohammad114  Thursday, August 13, 2009 8:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Remove created graphics in picturebox
• Cast or convert?
• How can i get information from Form1 to Form2 ?c#
• being able to detect all form closed
• populate textbox from a datatable
• a Strange isue using ListView control
• Drag & Drop, EnableAutoDragDrop
• special mask for text box
• Connection Error
• Creating bitmap from text string