Reputation: 4025
class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
// Handlers
Paint += new PaintEventHandler(MainWindow_Paint);
MouseClick += new MouseEventHandler(MainWindow_MouseClick);
}
void MainWindow_MouseClick(object sender, MouseEventArgs e)
{
var p = new Point(e.X, e.Y);
m_ListOfFigures.ToArray()[0].addPoint(ref p);
Refresh();
}
void MainWindow_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
}
I have no idea why my method, registered for wm_paint, not called?
Can you point to my mistake?
Upvotes: 1
Views: 1346
Reputation: 109792
What you did should have worked, but Microsoft say you should override OnPaint()
rather than subscribing to the Paint
event, to paint a user control.
As it says in the documentation for Control.Paint
:
When creating a new custom control or an inherited control with a different visual appearance, you must provide code to render the control by overriding the OnPaint method.
See here for more details: http://msdn.microsoft.com/en-us/library/cksxshce.aspx
But do note that doesn't really apply to Form classes (such as the one you're using), which are not User Controls or Custom Controls! Nevertheless, it's still more usual to override OnPaint() to paint the control itself.)
You should try adding the following to your form:
protected override void OnPaint(PaintEventArgs e)
{
// Call the OnPaint method of the base class.
base.OnPaint(e);
// Now draw stuff using e.Graphics
}
The Paint
event is not really intended for the user control or form itself to use. Instead, it is for when you want to be notified when a control within the containing control is being repainted.
Note However, having said that, it should still have worked using the Paint
event... but Microsoft say that you should override OnPaint()
to do your painting (perhaps because of issues such as the one you had).
Upvotes: 3