Graphics coordinates problem
VS 2008
On startup of this sample Windows forms application, the paint override uses DrawString to put a message on the screen. So far, so good. The problem is that there are three paint events on startup, and the 2nd or 3rd one causes the string to be drawn at a different location than where it is supposed to be.
I first noticed this problem in a more complicated application that I'm developing and created this simple example to eliminate any other code as the source of the problem. The problem also occurs with DrawRectangle, DrawLine, and DrawEllipse.
The 2nd and 3rd paint events occur 3 to 4 seconds after program startup. After that, the screen is redrawn correctly for the rest of program execution.
Is this possibly a bug in C# or am I missing something obvious? Here's the code. Thanks in advance.
#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Junk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs eventArgs)
{
using (Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic))
{
eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, 200, 200);
} //myFont is automatically disposed here, even if an exception was thrown
}
}
}
#
Re: Graphics coordinates problem
Using your exact code compiled with Visual Studio 2010SP1/W7-Enterprise. The Paint Event was only called once in my case?