windows.forms.panel calling onPaint twice - c#

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.

Related

How to draw a manually drawn control inside a manually drawn control?

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.

can't invoke paint

I created my own control and overwritten the onpaint event,
the problem is that the paint event stopped working
Any ideas why? And how to restore it?
Let's have a telepathic guess here:
You forgot to call base.OnPaint(...) inside your override. Meaning that the base functionality is no longer invoked.
Maybe the control is obscured. I had a similar problem, and the problem was that the form in the Designer view was bigger than the actual form when the application was run. My custom control had anchors on all sides, and when the main form was reduced in size, the custom control went to zero size (actually negative I guess).
In this mode, the OnPaint override nor a delegate assigned to the Paint event is not called at all.
Maximize your form and make sure that you didn't accidentally reduce the size to zero!

Problems with overriding OnPaint and grabbing mouse events in C# UserControl containing other controls

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.

c# Winforms: Refreshing a portion of a GUI (containing 1 or more controls)

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!

User Drawn Controls are using the previous Forms Background

I have several user drawn controls on a form, unfortunately when the form is shown the user drawn controls are showing the previous forms background rather than the current forms background.
The OnPaint event is very simple, and the OnBackgroundPaint event is empty...
Like this:
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.DrawImageUnscaled(_bmpImage, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//Leave empty...
}
How do I get the current background to be the transparency that is shown, rather than the background of the previous form?
You need to set the window style - here's a nice basic article.
You seem to be overriding the OnPaintBackground method not do anything about the background. Since you're leaving it empty, you probably shouldn't override it in the first place. What happens when you just let the default OnPaintBackground handler do its job? Shouldn't that automatically draw the current control's background properly?
P.S. I've never worked with custom painting .Net controls. I'm merely speculating in an attempt to help find a solution to your problem. Forgive me if what I'm suggesting is completely off...

Categories

Resources