I want to understand the mechanism behind the painting of a control: what are the functions (handlers) involved and when they are called.
I have a custom control based on a Panel. I think the functions involved for this examples are: OnPaint handler in Panel class, OnPaint handler in CustomPanel class and OnPaint event handler of the CustomPanel in the main form (from de designer).
1.OnPaint of Panel Class (what does this do and when is it called?)
2.OnPaint of CustomPanel Class (here i have base.OnPaint() and extra code for displaying a image in the viewable are of the Panel in an optimised way, for smooth scrolling)
3.OnPaint in main form (here is code for displaying some selection rectangles over the image)
The problem is that the rectangles are, i think, behind the image (the image is painted after the rectangles are painted?). The OnPaint handler gets called. When i scroll the image the rectangles flicker.
Before this custom panel, i used PictureBox control which had a Image property in which i put the current viewable part of the image based upon the values of two scrollbars which controlled the scrolling, and in the OnPaint event handler i did the drawing of the selection rectangles. Everything worked fine, but the scrolling speed became more and more slow proportional with the dimensions of the image (i generated the viewable part of the image with DrawImage()).
What changes should i do, in order to see the ractangles? And i would like to know what is the mechanism behind it, and in the future i won't come with this stupid questions :)
5 6841
Your rectangles are drawn in your Paint event handler in your application right?
Like you have you Form, and you add this CustomPanel control to it, say you called it myCustomPanel.
Then you attached an event handler on myCustomPanel to the Paint event?
So now you have:
Inside the CustomPanel class you have overriden the OnPaint function
In your regular program, attached an event handler to the Paint function of the instance of the CustomPanel class (mycustomPanel)
When the control is told to redraw itself, the OnPaint INSIDE the CustomPanel class is called. Since you have overridden it, it will execute *YOUR* version of the OnPaint function. When you call base.OnPaint() it will execute the built-in version of OnPaint() (which for say a Button control would be where they draw the visuals that make it 'look like a button' ) It is also what triggers the Paint() event to be fired.
So whenever you call base.OnPaint(), all of the original control-specific visuals are drawn, and the Paint() event is fired. All of which will probably happen before *YOUR* OnPaint() function goes on to the next line of code.
So I guess maybe to answer your question, call base.OnPaint() as the LAST thing inside your overridden OnPaint() function, to make sure all your custom drawings occur FIRST, then the Paint event happens after.
When the control is told to redraw itself, the OnPaint INSIDE the CustomPanel class is called. Since you have overridden it, it will execute *YOUR* version of the OnPaint function.
in the paint event handler in my form which has overriden the OnPaint event in the CustomPanel class which drew the image, i draw the rectangles. The image appears (the onpaint event handler in the CustomPanel class gets exexuted. Why? I overrided it and int this one there is no explicit call to base.OnPaint() ). The rectangles appear flickering only when i scroll the image. Could you be more specific in your explanation?
So I guess maybe to answer your question, call base.OnPaint() as the LAST thing inside your overridden OnPaint() function, to make sure all your custom drawings occur FIRST, then the Paint event happens after.
Nope, the same thing
Hmm. I will investigate more. -
public partial class CustomPanel : Panel
-
{
-
public CustomPanel()
-
{
-
InitializeComponent();
-
}
-
protected override void OnPaint(PaintEventArgs e)
-
{
-
Debug.WriteLine("Overriden OnPaint Called");
-
base.OnPaint(e);
-
Debug.WriteLine("Called base.OnPaint(e), exiting overriden OnPaint()");
-
}
-
}//end of class
-
-
CustomPanel cp = new CustomPanel();
-
cp.Paint += new PaintEventHandler(cp_Paint);
-
-
-
void cp_Paint(object sender, PaintEventArgs e)
-
{
-
System.Diagnostics.Debug.WriteLine("Paint event handler called");
-
}
-
When I use that sample code, the output looks like this:
Overriden OnPaint Called
Paint event handler called
Called base.OnPaint(e), exiting overriden OnPaint()
If I remove the base.onPaint call, "Paint event handler called" never appears
That's strange, i'll review my code. Thanks for the help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Franck |
last post by:
Hello,
Sorry if that question has already been raised...
I'm looking for the exact equivalent of Java paint(Graphics g) method in c#
in order to paint a specific component and all its children...
|
by: Sagaert Johan |
last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up.
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
|
by: vooose |
last post by:
Consider a UserControl TopCont that contains two other UserControls,
CompA and CompB.
Somewhere in the constructor of TopCont we have
CompA.Paint += new PaintEventHandler(compA_Paint);...
|
by: vooose |
last post by:
Consider a UserControl to which you do
userControl.Paint += new PaintEventHandler(paint_method)
If you don't like that way, and prefer to override onPaint( ) then the
problem stated below...
|
by: pacemkr |
last post by:
Is it possible to force a control to paint to a Graphics object (or
Device Context, or a bitmap, anywhere aside from the form) that I
provide.
I am writing a windows form class that supports...
|
by: BB |
last post by:
Hello all,
I am trying to override OnPaint in a custom textbox
control (so I can drawstring a caption, etc.). In the
code below, I get the "painting the form" message as
expected, but not the...
|
by: jcrouse |
last post by:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax. I
don't really understand the event. Where it...
|
by: Tim Marshall |
last post by:
In my not too successful attempts to get an OLE chart object (Graph
11.0) that has been manipulated on a form to be reproduced on a report,
I am considering the following procedure. First copy the...
|
by: Rotsey |
last post by:
Hi,
I have a interface that I use for a form so I can pass the form
to another object.
How do I add the Paint event to the interface and subsequently
handle the paint event in my other...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |