I have two controls in C# that I override OnPaint() to draw all the visuals myself.
I'd like to add one of those controls inside the other.
What do I put in my parent control's OnPaint() to call the child control's OnPaint()?
You don't specify winforms, but that seems very likely based on your question.
You are presumably calling Invalidate() or Refresh() depending upon your heart's desire.
Refresh() on a control should make the control and its children to repaint.
If you call Invalidate(true) on a control, the child controls should also receive the invalidate message. Invalidate() without the true parameters is not recursive.
Related
I have a panel with multiple panels inside of it. I have overridden OnPaint in the master panel to the following:
protected override void OnPaint(PaintEventArgs e)
{
Graphics graph = e.Graphics;
graph.Clear(Color.Black);
InvokePaintBackground(this, e);
graph.ScaleTransform(scale, scale);
foreach (childPanel child in childPanels)
{
child.onPaint(this, e);
}
graph.ResetTransform();
}
The problem I have is that the onPaint function of the first control (control in spot 0) is being called twice so I am getting two versions of the child panel, one with scaling, and one without. The second onPaint seems to be called by the child control itself.
How do I keep it from doing this?
That's because all Control objects do their own painting and the method is called automatically by Windows. The solution is to not rely on this sort of functionality at all - get rid of the panels, or set Visible to false.
Why on earth are you calling OnPaint on the child control? Windows will manage the paint calls on its own. You should never call them directly, especially with the graphics context you received from a separate paint call!
If you need to request a child control to be painted, use the Invalidate method instead. It marks a region (or entire control) as invalid so that Windows will paint it. The upside to that is that Windows is smart enough to know that if you invalidate it multiple times in the same paint cycle, it won't re-draw multiple times.
It is the inherent behavior. You can simply use a private bool secondCall; sort of a variable and do the scaling only on the second call.
I've made a control which contains few other controls like PictureBox, Label and TextBox. But I'm having two problems:
1. I tried to paint some things on top of my control and when I'm overriding OnPaint, it results that things I try to draw are under controls that my control contains. Areas on which I would like to draw intersect with controls in ways that are not easy to predict. I mean that it includes something drawn on controls inside as well as on base of control. Is there a simple way to draw something on top of all contents of my control? Setting ForeColor to Transparent isn't a solution that would help me much.
2. I have a problem with grabbing mouse click events when I place my control on a form and add click event handling. It only works when I click on an area not occupied by controls inside. I would like the whole control to react to clicks and other actions like it was one consistent control. How can I redirect/handle these clicks to make them work the way I want?
Thanks in advance for any tips
edit:
Thanks for answer, i hoped for something less complexed.
Finally i decided to do it from scratch, painting all necessary things directly on surface of my UserControl, TextBox shows up only when it is necessary. That solved all of my problems with drawing and events.
MTH
1) Overriding a control's OnPaint event allows you to draw onto that control. Whenever that control is painted, your drawing code will be executed. Then, and only then, will the child controls be drawn on top of that control, each of them with their own OnPaint events executing. Each control is its own window (in the sense of the Windows API), and therefore each is responsible for drawing its own surface. This explains why your graphics are being covered up by the child controls, and why they are only visible in the empty spaces where no child controls have been placed.
The upshot is that drawing on top of multiple controls inside a container is not well-supported. There are a couple of hacks you could try to help get around these limitations, but you may well be disappointed.
The first possible hack is to draw your graphics on the container control as you've already done, and then also draw on top of the child controls themselves. You'll need to override each child control's OnPaint event in order for this to work, however, which immediately presents a problem. A TextBox control (along with a ComboBox, ListView, TreeView, and a handful of other controls) doesn't do its drawing in an OnPaint event; it's drawn natively by the operating system. The possible workarounds for this are so potentially painful that you might as well forget about it and change your design.
The second possible hack is to add another control that sits on top of the container control and do your drawing on its surface. Perhaps the easiest way to do this is by creating something like a transparent panel using the WS_EX_TRANSPARENT extended window style:
public class TransparentPanel : Panel
{
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x00000020;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}
2) Again, it becomes relevant that each child control is its own window and handles its own mouse input events. That's why the control whose MouseClick event you've overridden is only detecting the clicks that occur directly on its surface. The other clicks get passed to the child controls, who just ignore them. Events in WinForms are not bubbled up the control hierarchy.
As before, one possible solution is to handle the MouseClick event (and any others you want) for both the container and child controls. If you decide to go this route, I think I would recommend consolidating your logic into a single routine and simply calling that routine from each control's event handler.
If you're looking for something more comprehensive, you might check out Broadcasting Events through a Control Hierarchy, although I haven't taken the time to read it.
Of course, you could also send the relevant mouse input messages to your parent container control from each child control using the SendMessage API function. But I'll leave that as an exercise for the reader because I feel like you asked a simple question expecting a simple answer.
I have a User Control that has some WinForm Controls inside. I want to be able to paint those inner controls myself (for scrolling purposes, some of them will remain, others will move).
Even when I override the OnPaint and the OnPaintBackground method, those controls keeps showing on my usercontrol which is completely black now because there is no painting methods on it.
Is there a way I can suppress those controls from been painted and then paint them by myself with the DrawToBitmap method of each control?
Yes. Remove them from (or better yet, never add them to) the UserControl's set of child controls. Simulating interaction with the controls is going to be a PITA, though.
Do you just need the controls to "look" like they're there. Or do they need to actually be there? If its the latter, you would be better off faking the scrolling somehow by just repositioning the controls manually.
Trying to re-invent the windowing system is an exercise in pain. You will be better off if you learn and work within its paradigms.
If you don't need interaction, just set every child control .Visible = false.
Otherwise, have you tried WM_SETREDRAW?
I have a UserControl with a few buttons on it. I want to override OnPaint and paint some other stuff on the control.
So I override OnPaint.
Whenever OnPaint gets called the ClipRectangle is always {0,0,0,0} and so nothing I do gets drawn on the control.
What am I doing wrong?
Ok, Ive sussed it now. Its a bit silly. I had a TableLayoutPanel that was docked to the control. That seems to clip out all the areas that we can paint on.
I created a new control that derives from TableLayoutPanel and used this control instead. Its OnPaint gets the full clip rect.
However, that wasnt any use anyway... It seems I have misunderstood how Windows painting works. I thought I would be able to paint directly over the top of my controls, but this isnt the case. The controls get placed on top of my painting.
Gonna have to mess about with panels to get this working I reckon..
I have a Control that can overlay multiple C# user controls in my GUI. This control has a semi-transparent background in order to 'grey-out' portions of the GUI and the class looks somethink like this:
public greyOutControl: UserControl
{
// Usual stuff here
protected overide OnPaint()
{
paintBackround();
base.OnPaint();
}
}
Currently the control sometimes gets caught in a loop and constantly re-draws the background, making the semi-transparent color appear less and less transparent.
My idea to combat this is the following (in broad terms):
1) Determine what controls the greyOutControl is on top of
2) call Refresh() on those controls to update the display
3) continue drawing the greyOutControl.
My question is: How can I determine which controls the greyOutControl overlaps?, or is there a way that I can refresh only the part of the GUI that greyOutControl covers?
Why don't you keep track of your transparent controls and paint them after all the other controls are drawn?. Painting anything at the top of the Z-order shouldn't cause the other controls to be repainted.
I don't see a direct way of finding the overlapping controls. I think you might need to check the whole control tree to find out that. About refreshing, you can use Control.Invalidate(Rectangle) method to specify which part to refresh.
The solution to this problem I found was to programmatically take a screen shot of the area being overlayed and then use that image as the background for the control being overlayed. This then allows you to put the alpha overlay into the image within the OnPaint() method and the control to draw itself correctly.
This does have the disadvantage that the background isn't updated in the overlapping control, but unless there was a number of event handlers watching if something changes and then update the overlayed control I cant see any way around the issue. Sometimes I regret not trying to use WPF!