FillRectangle over Windows Forms Components

G

Guest

Hello NG,

I want to draw a rectangle on my panel:

Graphics g = panel1.CreateGraphics();
g.FillRectangle( .......

no problem.
But if I put Form-Elements, like a button, on this panel,
my rectangle is behind this button.
But I want to render it in the front.

What can I do to solve this problem ?


regards
Stephan
 
K

Kerem OZMAN

It seems normal since your panel and button have different client rectangles
(surfaces). I guess you have two options. One is to create another surface
(another control etc) with z-order greater than both the button and the
panel and draw your rectangle on it. Or you can get a handle of the desktop
and draw your image to desktop but by using this way you have to calculate
the position where you will draw your rectangle (relative to the desktop).
 
G

Guest

Hi Kerem,

the problem with a new control is, that you hide the object under it.
I put a new panel over the button. Now I can draw
the rectancle on it, but the Button is not visible any more.

Thanx,
Stephan
 
K

Kerem OZMAN

Hello,
Let me get it right:
You have a panel and a button and you want to draw a rectangle on top of
them both. (That means your rectangle might overlap the button and hide some
portion of button's client rectangle right?) Then if your shape is strictly
rectangular you can create a control having the exact size of yoour
rectangle and by setting its z-order you can show your rectangle on top of
the button and the panel. If I am missing something please let me know and
we might think of a working solution. (Also you may go for the desktop
handle solution though it would be a longer one.)

Regards.
 
G

Guest

I think it will work with a panel under the rectangle.
The problem is that the rectangeles move several
times a second. So it would be to slow to move a panel
object all the time.
 
S

Stoitcho Goutsev \(100\)

Yes, this is correct both controls has different device context (if we speak
in terms of windwos API), but the real reason why the form cannot draw over
its children is because the form has WS_CLIPCHILDREN set by default. If one
removes this window style then the form will be able to draw over the
controls.
To do that form's CreateParams property needs to be overriden like this:

private const int WS_CLIPCHILDREN = 0x02000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~WS_CLIPCHILDREN;
return cp;
}
}


Now the form will draw over the button, how the whole application will
behanve and if there are going to be another problems frankly I don't know.
 
G

Guest

I tried it in my form, but it doesn´t work.
The rectangle is still under the Forms components.
 
S

Stoitcho Goutsev \(100\)

Stephan,

Are you sure that you use form's graphics to draw and not the graphics of
some other component (such as panel) if you do so you need to clear that
flag on the component that you draw on.

You can use Spy++ to check all the components you have on the form as well
as their styles.
I have tried this and it works if everything is done correctly.
 
G

Guest

You are right, I really used the wrong panel.
Now it´t works.
Thank you very much.

Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top