protected virtual OnPaint(PaintEventArgs e)
{
if(paint != null)
{
paint(this,e);
}
}
If you rewrite onpaint directly, paint will fail; this will not fail:
protected override OnPaint(PaintEventArgs e)
{
(e); //Own code
}
(1) OnPaint() and Paint are often used when repainting. What is the difference between them?
The method is for a control; the Paint event is for a control object. The former is equivalent to a member function of the class, while the latter is equivalent to a variable of a function pointer type of the class (which will vary depending on the object).
The method raises the Paint event, so override the OnPaint method and must be called, otherwise the Paint event will not be raised. The OnPaint prototype should be similar to the following form (as can be seen from it):
protected virtual void OnPaint(PaintEventArgs e)
{
if ( != null)
{
(this,e);
}
}
3. Observe the call order of the two from the examples
private void Form1_Paint(object sender, PaintEventArgs e)
{
test t = new test();
= true;
(.Circle1FillColor, );
(, 10, 10);
}
protected override void OnPaint(PaintEventArgs e)
{
(e);//Raise Paint event processing (Call Form1_Paint method when processing this event)
..........
}
Form1_Paint() is just a method to handle Paint events. It can also write its four lines of code in the OnPaint method. At this time, you can not write (e), that is, the event processing is not triggered, and the same effect can be achieved.
(II) So under what circumstances should they be used?
1. If you want to display all controls in a fixed way, such as: when writing controls yourself, you need to modify the OnPaint method of the overloaded control; and if you only need to display a certain object differently in a certain environment, you only need to do it in its Paint event.
2. When implementing derived classes, follow C# principle 35: Choose to overwrite functions instead of using event handles.
Many classes in .net class libraries provide two different ways to handle event handles. You can either add events to it or override the event abstraction method of its base class. When implementing derived classes, the better option is to override abstract methods in the base class.
Because of this, once the event handle throws an exception, no other event handles will be called. This avoids the problem that some error codes continue to be called. By overriding the protected virtual method, our handle can be called the first one. Virtual functions in the base class are responsible for calling other related handles. This means that if those event handles need to be called (generally required), the virtual function of the base class must be called. In some special cases we need to replace the default behavior of the base class and may not need to call any original event handles. Although we cannot guarantee that all event handles are executed because they may throw exceptions, we can guarantee that the derived class behaves correctly.
Using override is much more efficient than adding event handles. In clause 22, it shows how the class stores handle time and corresponds it to each event. This event mechanism will cause more consumption due to checking the event handle. Each method in the event handle list needs to be executed. Compared to rewriting virtual methods, it takes more time to process events.
In addition, rewriting virtual methods only requires maintaining one function to achieve the purpose of inspection and modification, and the code is clearer. The event mechanism requires two maintenance points: event handle function and event binding code. Any of these can cause overall functional failure. A function is obviously simpler.