This Should Be Easy, But It’s Not

Friday March 28th, 2008 @ 9:42 PM by Chris

Every now and then when I’m sitting in front of my IDE I run into a problem I can’t solve. Strangely enough, these problems are almost always related to the User Interface.

The problem I’m currently faced with is: How to draw a semi-transparent overlay on top of an existing Windows Form in .NET?

We have certain views in our application that see heavy re-use. They appear frequently in many different contexts. This is good for code re-use, but it confuses the users. Because all of the instances of the particular view look the same, the users aren’t immediately aware of the specific context.

The way I’d like to handle this is by overlaying a semi-transparent color on top of the view. This sounds simple enough; it seems like it should be easy. Perhaps just specifying a brush with a transparent color and draw on the client rectangle.

Oh, but it isn’t that easy…

There are a lot of obstacles in the way of doing something that sounds so simple. The first obstacle is that the view (Form/UserControl) draws itself first, and then all of its controls. There’s no option to come back after all the controls have been drawn and draw an additional thing on top of the main view. The child controls don’t get painted.

Ah, but with a little recursive magic, maybe it will work eh? Tell all the child controls to draw the transparent color as well, right? I tried that too; turns out only a few controls will obey and you end up with this:

Overlay

The DataGridView obeyed the painting (which is great) but the ListView and TextBox did not. I could possibly live with that, but the DataGridView flickers quite a bit whenever a cell is edited, and that’s a bummer.

It is times like this, when I run up against the limitations of the Windows Forms API, that drive me nuts. I don’t suppose anyone has a working suggestion?

Posted in .NET |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

This Should Be Easy, But It’s Not

Friday March 28th, 2008 @ 9:42 PM by Chris

Every now and then when I’m sitting in front of my IDE I run into a problem I can’t solve. Strangely enough, these problems are almost always related to the User Interface.

The problem I’m currently faced with is: How to draw a semi-transparent overlay on top of an existing Windows Form in .NET?

We have certain views in our application that see heavy re-use. They appear frequently in many different contexts. This is good for code re-use, but it confuses the users. Because all of the instances of the particular view look the same, the users aren’t immediately aware of the specific context.

The way I’d like to handle this is by overlaying a semi-transparent color on top of the view. This sounds simple enough; it seems like it should be easy. Perhaps just specifying a brush with a transparent color and draw on the client rectangle.

Oh, but it isn’t that easy…

There are a lot of obstacles in the way of doing something that sounds so simple. The first obstacle is that the view (Form/UserControl) draws itself first, and then all of its controls. There’s no option to come back after all the controls have been drawn and draw an additional thing on top of the main view. The child controls don’t get painted.

Ah, but with a little recursive magic, maybe it will work eh? Tell all the child controls to draw the transparent color as well, right? I tried that too; turns out only a few controls will obey and you end up with this:

Overlay

The DataGridView obeyed the painting (which is great) but the ListView and TextBox did not. I could possibly live with that, but the DataGridView flickers quite a bit whenever a cell is edited, and that’s a bummer.

It is times like this, when I run up against the limitations of the Windows Forms API, that drive me nuts. I don’t suppose anyone has a working suggestion?

Posted in .NET |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.