all groups > dotnet drawing api > april 2007 >
You're in the

dotnet drawing api

group:

Cheating the Paint Event ?


Cheating the Paint Event ? Bill Woodruff
4/29/2007 10:02:00 PM
dotnet drawing api:
I really hate writing Paint Events ! (it showed ?)

One strategy I explored to avoid writing a Paint Event in the case of a
control with a partially transparent background placed over a second control
(where, as we all know too well, only the Form background, not the second
control background, will show through) ...

Was to try in the Form Load event : (C#)

1. create a new Bitmap of size of the Form (I experimented with both
PixelFormat Format32bppRgb and Format24bppRgb settings)

2. get the Form Window handle using Graphics.FromHwnd(this.handle)

3. using DrawImage on the Form Window handle : copy the Form background into
the new Bitmap

I then tried setting the Form.BackgroundImage property of the Form to the
new Bitmap to check and see if the first stage of 'copying' was working.
Nope : while it doesn't throw an error (as long as you don't call Dispose()
on the new bitmap) : the form background is now all black.

My idea was to see if I could copy the Form's current background, bitblt a
picture of a control background onto this copy in the right place and then
set that compound bitmap to be the Form's BackgroundImage property. Then, so
I hoped, I could place my second control (a pointer or indicator) over the
Form and have the transparent areas of the second control do the right
thing. And so cheat that infernal master the Paint Event !

Any thoughts ?

thanks, Bill


Re: Cheating the Paint Event ? Michael C
4/30/2007 12:00:00 AM
[quoted text, click to view]

I don't quite follow what you're trying to do but one thing I'm pretty
certain of is you're doing it the wrong way. You can't grab the form's image
when it's loading because it doesn't exist on the screen yet. When you say
trasparent do you mean some pixels turned on and some off or some partially
trasparent?

Michael

Re: Cheating the Paint Event ? Bob Powell [MVP]
4/30/2007 12:00:00 AM
It sounds to me as if you need to implement the LayeredWindow API properly
and forget about the idea of trying to cheat.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
Re: Cheating the Paint Event ? Bill Woodruff
4/30/2007 10:14:26 PM
Michael wrote :

"I don't quite follow what you're trying to do but one thing I'm pretty
certain of is you're doing it the wrong way. You can't grab the form's image
when it's loading because it doesn't exist on the screen yet. When you say
trasparent do you mean some pixels turned on and some off or some partially
trasparent?"

fyi I tried grabbing the form's image using the 'Shown event, which should
occur after loading, but still get black.

You can take a png file into .NET and use the MakeTransparent call on a
specified color to make certain pixels truly transparent, but as usual,
there's always the gotcha that what shows through is not what's underneath,
but the only the form background itself. Hence, Bob Powell's suggestion to
use the Layered Windows API for a full solution.

In the case however that you have only one presentation element between the
newly transparentized control and the form background, it seems a reasonable
strategy to explore the idea of getting the image of the presentation
element between control and form background and in essence making it
permanently part of the background of the form, which is what I am
exploring.

best, Bill


Re: Cheating the Paint Event ? Michael C
5/1/2007 12:00:00 AM
[quoted text, click to view]

If your control is there though then nothing behind will paint.

[quoted text, click to view]

AddThis Social Bookmark Button