miteshpandey
September 6th, 2005, 11:51 AM
protected override void OnPaint(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(
new Pen(Color.Red, 1),
0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(
measureString,
stringFont,
Brushes.Black,
new PointF(0, 0));
}
The above is the code copied from MSDN exactly. It produces the following screenshot.
The problem is that you can see a small gap between the message and the right edge of the rectangle enclosing the message. The gap is about one character of the message.
If I use a smaller font the gap becomes about 3 characters of the message.
e.Graphics.DrawRectangle(
new Pen(Color.Red, 1),
0.0F, 0.0F, stringSize.Width-10, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(
measureString,
stringFont,
Brushes.Black,
new PointF(0, 0));
If I deduct about 10 from the stringSize.Width like shown in the above code, the gap dissappears and the result is perfect.
Since 10 is an arbitary value and when used with lager fonts the message is intersected by the right border of the rectagle, I am having problems
What is going on? Am I missing something. I have used the same font for both while measuring and drawing the message.
You might say why do you worry about this. This is a minor thing. But what I have is an ArrayList of messages. But when these messages are drawn there appears to have gaps which I don't like. I am using this to code a custom control of mine which supports scrolling. So I need this measurement to be very precise.
Please help.Any suggestions would be appreciated much
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(
new Pen(Color.Red, 1),
0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(
measureString,
stringFont,
Brushes.Black,
new PointF(0, 0));
}
The above is the code copied from MSDN exactly. It produces the following screenshot.
The problem is that you can see a small gap between the message and the right edge of the rectangle enclosing the message. The gap is about one character of the message.
If I use a smaller font the gap becomes about 3 characters of the message.
e.Graphics.DrawRectangle(
new Pen(Color.Red, 1),
0.0F, 0.0F, stringSize.Width-10, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(
measureString,
stringFont,
Brushes.Black,
new PointF(0, 0));
If I deduct about 10 from the stringSize.Width like shown in the above code, the gap dissappears and the result is perfect.
Since 10 is an arbitary value and when used with lager fonts the message is intersected by the right border of the rectagle, I am having problems
What is going on? Am I missing something. I have used the same font for both while measuring and drawing the message.
You might say why do you worry about this. This is a minor thing. But what I have is an ArrayList of messages. But when these messages are drawn there appears to have gaps which I don't like. I am using this to code a custom control of mine which supports scrolling. So I need this measurement to be very precise.
Please help.Any suggestions would be appreciated much