Windows Develop Bookmark and Share   
 index > Windows Forms General > Redraw control after window has hovered over it
 

Redraw control after window has hovered over it

Hi!

I'm writing a custom control and have a problem when the controls needs to redraw itself. Specifically, it's when you drag, for example, another window over my application's form containing the control. The redrawing of the control doesn't happen or the redrawing is incomplete.

I already set the ControlStyles AllPaintingInWmPaint, DoubleBuffer, ResizeRedraw and UserPaint.

I'm overriding the OnPaintBackground to draw a series of lines, like the following



protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    DrawGrid(e.ClipRectangle, e.Graphics);
}

 


The DrawGrid method calls DrawLine on the Graphics objects in a for-loop - and only draws horizontal lines as it's supposed to. But when I drag a window over it, the redrawing is bad. Shouldn't be anything complicated though. I also call Invalidate(true) in the last line of DrawGrid, to invalidate and repaint the control and its children.

Can you see what I'm doing wrong?

Regards,
Patrick
Patricius  Monday, November 14, 2005 9:16 PM
Ok, I found the solution. It seemed, that I mistakenly thought that I needed to switch on the ControlStyles mentioned in the first post. That, combined with calling invalidate all the time, was the source of my problem.

Thanks for your help guys!
Patricius  Wednesday, November 16, 2005 8:43 PM
Sorry you are right. The AllPaintingInWmPaint control styles causes the framework to skip the OnPaintBackground and just call OnPaint.
David M. Kean  Wednesday, November 16, 2005 8:55 PM
you should call your control Invalidate() method I think...
Amadrias  Monday, November 14, 2005 9:46 PM
When I call invalidate after having drawn the grid in the OnPaintBackground, the CPU usage goes to ~100%. This can't be the solutionSmile.
Patricius  Tuesday, November 15, 2005 7:20 AM
You are putting the invalidation in an infinite loop. If you are calling DrawGrid from the PaintBackground event, there is no need to call Invalidate from DrawGrid as the control is already Invalidating:

Your code path looks like the following:

1.) OnPaintBackground
2.) DrawGrid
3.) DrawGrid calls Invalidate which raises OnPaintBackground
4.) OnPaintBackground
5.) DrawGrid
6.) DrawGrid calls Invalidate which raises OnPaintBackground

and so on....

Remove the Invalidate() method from DrawGrid and see how it works. This is the best I can offer you without seeing any code.
Joe Sm  Wednesday, November 16, 2005 7:59 PM
You are drawing the grid only on the ClipRectangle. This rectangle represents the portion of the control that needs to be redrawn and does not represent the true size of the grid.

Try replace e.ClipRectangle with ClientRectangle.
David M. Kean  Wednesday, November 16, 2005 8:39 PM
Ok, I found the solution. It seemed, that I mistakenly thought that I needed to switch on the ControlStyles mentioned in the first post. That, combined with calling invalidate all the time, was the source of my problem.

Thanks for your help guys!
Patricius  Wednesday, November 16, 2005 8:43 PM

Are you sure? The ControlStyles setting you have set seem fine and according to the code you posted it looks like you are only drawing on the ClipRectangle which may not be what you want.

David M. Kean  Wednesday, November 16, 2005 8:46 PM
Well, as far as it looks now, the repainting is done appropriately. I'm not setting any control styles at all now, and it works when I drag a window over it. It updates automatically - I guess because of the invalidation that happens when dragging the window over.

What is the difference between painting in OnPaintBackground and OnPaint? Does it have anything to do with that, since I'm not painting anything (yet) in OnPaint?
Patricius  Wednesday, November 16, 2005 8:49 PM
Sorry you are right. The AllPaintingInWmPaint control styles causes the framework to skip the OnPaintBackground and just call OnPaint.
David M. Kean  Wednesday, November 16, 2005 8:55 PM
I believe calling Invalidate without any parameters causes ClipRectangle to equal ClientRectangle.
Joe Sm  Wednesday, November 16, 2005 9:06 PM
However you can't guarantee that as moving a window over a small portion of a the control will cause the ClipRectangle to equal the portion that was covered.
David M. Kean  Wednesday, November 16, 2005 9:19 PM
No, that is true, however I was referring to when he is calling Invalidate explicitly. Sorry for not being more clear in what I was saying.
Joe Sm  Wednesday, November 16, 2005 9:27 PM

You can use google to search for other answers

Custom Search

More Threads

• How to working with two SqlDataReader
• Translating a drawShape beyond zero
• Half transparent d'n'd icons - how are they made?
• "bubbling" events in a usercontrol?
• problem with disposing the parents
• Attachments in a grid view and unwanted items in outer xml
• System::Drawing::Image's filename + Native code being compiled as manage
• How to provide prompts in status bar in response to menu selections like used to come for free in MFC?
• Thread::Sleep question
• Compiling using the .NET Framework SDK