473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

C# Control Paint Succesion

tranc3d
11
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 :)
Oct 23 '08 #1
5 6841
Plater
7,872 Expert 4TB
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.
Oct 23 '08 #2
tranc3d
11
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?
Oct 24 '08 #3
tranc3d
11
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
Oct 24 '08 #4
Plater
7,872 Expert 4TB
Hmm. I will investigate more.

Expand|Select|Wrap|Line Numbers
  1. public partial class CustomPanel : Panel
  2. {
  3.    public CustomPanel()
  4.    {
  5.       InitializeComponent();
  6.    }
  7.    protected override void OnPaint(PaintEventArgs e)
  8.    {
  9.       Debug.WriteLine("Overriden OnPaint Called"); 
  10.       base.OnPaint(e);
  11.       Debug.WriteLine("Called base.OnPaint(e), exiting overriden OnPaint()");
  12.    }
  13. }//end of class
  14.  
Expand|Select|Wrap|Line Numbers
  1. CustomPanel cp = new CustomPanel();
  2. cp.Paint += new PaintEventHandler(cp_Paint);
  3.  
  4.  
  5. void cp_Paint(object sender, PaintEventArgs e)
  6. {
  7.    System.Diagnostics.Debug.WriteLine("Paint event handler called");
  8. }
  9.  
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
Oct 24 '08 #5
tranc3d
11
That's strange, i'll review my code. Thanks for the help.
Oct 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
11
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...
5
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);...
0
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...
3
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...
20
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...
6
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...
1
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.