views:

36

answers:

2

Hi all,

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

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

A: 

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.

I tried changing the focus but regardless of how I set it my drawing still appears behind the other controls. Those controls are hosted in the panel that I draw in so I'm not sure that I can do much with the z order.
Paul Sasik
+1  A: 

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.

Hans Passant
+1 This looks very promising. (Will take some hours to work through the concepts tomorrow.) I was afraid that it might come to an overlay idea of some sort but I haven't dealt with it since my MFC days. Was hoping for a simpler .NET solution but those links are great leads! Thanks very much nobugz! And by the way, using the overlay addresses a few other designer-related issues as well.
Paul Sasik