Paul Sasik
Paul Sasik

Reputation: 81459

How to draw "on top" of hosted controls in .NET?

I am working on a simple form builder which hosts "live" .NET objects as well as performing its own drawing for guidelines (grid), object selection etc. Things are going well but as you see below, the custom painting is not exactly optimal since it always paints behind the hosted controls.

Selection Sample http://img405.imageshack.us/img405/8572/selectsample01.png

How can I modify my painting logic to draw "on top" of all hosted controls?

If the image is missing in your SO view it can be viewed here: http://img405.imageshack.us/img405/8572/selectsample01.png

Upvotes: 4

Views: 973

Answers (2)

Hans Passant
Hans Passant

Reputation: 941495

It is not a Z-order issue. The problem is that you can't draw inside the client rectangle of another window. The "Text" window in your case. A window like your "form" has the WS_CLIPCHILDREN style flag turned on.

I'm not exactly sure how the Windows Forms designer manages to draw selection handles around the controls. But when I look at the designer with Spy++, I see two otherwise invisible windows listed that are the size of the design area. They are named "OverlayControl" and "AdornerWindow". My guess is that the designer actually draws the handles on one of those windows (OverlayControl probably) and that the windows background is transparent.

I used a similar trick in this thread, you might be able to leverage the code. You also really ought to take a look at this magazine article.

Upvotes: 2

user113476
user113476

Reputation:

From the screenshot it appears that the hosted control still has focus which means that it should be painted in front.

Remove the focus from the hosted control and ensure that the z-order is set correctly.

If you control the z-order then ensure that you are painting according to the z-order.

If you don't have a z-order then you need to implement one.

Upvotes: 0

Related Questions