Wednesday, April 27, 2005

Drawing Transparent Controls

A quick search of the Windows CE newsgroups shows no small number of questions about transparency in controls. With its pared-down API, CE doesn't make it quite as easy as regular Win32 to draw transparently. Still, it can be done, and (as we found) it's not too difficult, either.

The biggest and foremost obstacle is, as the help puts it, that "all windows implicitly have the WS_CLIPSIBLINGS and WS_CLIPCHILDREN styles [set]." (In "regular" windows, a WM_PAINT message would cause a window to repaint its entire background. CE, an OS that usually runs on low-horsepower devices, tries to save some time by only drawing the region not covered by other windows. When a parent window is finished painting it selfishly tells all child windows to go paint themselves.) This works fine if you want to paint normal-looking buttons

WinCE Test Button

but as soon as you want to get fancy (move your mouse over the button to see the fancyness)

Oswald Test Button

it makes life a little harder.

In our case we wanted buttons with round corners. Also, they needed to depress and visibly lose their shadow (see above), which meant that if we merely painted the button image over itself we would still see the border from the last button image.

Also, many static images in our application needed a transparent background. (The alternative was to use icons, which don't scale well past 16x16 increments, or to "hard-code" the background into each image, which was a terrible option for obvious reasons.)

Enter our saviour, TransparentImage(), the CE equivalent of Win32's TransparentBlt() (or just blitting with a monochrome bit-mask).

Note: You might be tempted to just use SetBkMode(TRANSPARENT) in OnCtlColor. But the problem with doing that is that the control becomes too transparent and won't even repaint in response to a WM_ERASEBKGND message. On the other hand, passing back a brush created with the current background color would work just fine in OnCtlColor. You just have to make sure that you keep the brush valid (for example, make it a class member) or use a stock brush.

A note on my nomenclature: I use c_ as a prefix for control variables (to differentiate from m_ for data variables), a habit I picked up from the Flounder.

For drawing transparent static controls, this worked great in the parent window's OnPaint handler:
// Draw the "background" images transparently
// NOTE: Validate the control so it doesn't try to re-
// paint itself with its default bmp. Other options:
// 1. c_sensor.SetRedraw(false);
// 2. c_sensor.SetBitmap(bmp);
// 3. Subclass CStatic and paint our own control

pDC = c_sensor.GetDC();
pOldFont = pDC->SelectObject(&m_fontSmall);
DrawBitmap(pDC, &rect1, IDB_SENSOR);

// Draw the centered text overlay on the bitmap
overlay.Format(_T("%d"), m_sensorNum);
pDC->DrawText(overlay, &rect1, DT_CENTER);


DrawBitmap calls a parent-class function that calls TransparentImage(), like so:
TransparentImage(pDC->GetSafeHdc(), /* hdcDest */
pRect->left, pRect->top,
bmpInfo.bmWidth, bmpInfo.bmHeight, /* dest */
(HBITMAP) bmp, /* hdcSrc */
0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, /* src */

where CLR_TRANSPARENT is a pre-defined color set to hot pink, RGB(255, 0, 255). (I figured we wouldn't have much use for that color, and even if we did, I'd rather not see it!) For even more flexibility, you could also looked for a pre-determined pixel--say (0, 0)--and use its color as the images transparent color.

The most robust way to do this would be to use option #3 from above and subclass our own static controls. That way, we could control our background repainting in OnEraseBkgnd and our normal painting in DrawItem. But the statics were easy enough to handle in their parents' OnPaint method. We did subclass the buttons and used an off-screen buffer to do all our drawing before blitting them back over to the screen. But that's a post for another day.

Final note: You might look at our solution and say, "Hey, that's not really transparent. You're just faking it by drawing the same background color!" and you'd be right. This is good-enough transparency. It works well on a monochromatic background. True transparency involves using GetPixel() to see what's "underneath" and duplicating it, or at least part of it, in the draw. Yeah, that's also another post for another day.


At 7/18/2006 7:34 PM, Anonymous Anonymous said...

best regards, nice info
cts navigation cadillac edmonton rhinoplasty best windows phones laloo prasad's ringtone business insurance free free best web site promotion life assurance houses for sale virginia auto magic bingo cards casinos in the uk online education with d rings soccer motor vehicle insurance sydney health insurance california meridia research personal skin care popupframer a popup blocker stopper manager killer privacy automobile insurance market apollo loss expenses million diet pills didrex 3 month supply cheap

At 3/05/2007 8:43 AM, Anonymous Anonymous said...

Wonderful and informative web site. I used information from that site its great. new balance canvas tennis shoes kettler table tennis company pictures of ultram tablets tail tennis clothes dr. patricia fox ny plastic surgeon Interracial analfist

At 3/17/2007 5:43 PM, Anonymous Anonymous said...

What a great site film editing schools

At 4/26/2007 9:50 AM, Anonymous Anonymous said...

Very nice site! Blood+pressure+take Bridal wedding bouquets flowers Nevada horse racetrack

At 5/09/2007 11:57 AM, Anonymous Anonymous said...

Enjoyed a lot! » »

At 11/19/2009 9:39 AM, Blogger calvin said...

The transparency feature of the Windows Forms control leaves much to be desired and is a blatant fudge. The control is not really transparent, it just pretends to be by looking at the background of it's parent control and copying the appropriate portion of the image or background onto it's own surface during the OnPaintBackground method.also get a better sex life wt Generic Cialis its the best.

At 11/23/2009 10:18 AM, Anonymous Cialis Soft said...

hey friend I love this information about Drawing Transparent Controls !!!!!! I was looking this information!!! but I find it thanks for sharing

At 1/27/2010 11:53 PM, Anonymous Anonymous said...


[b]Xrumer SEO Professionals

As Xrumer experts, we secure been using [url=]Xrumer[/url] for a large immediately things being what they are and know how to harness the colossal power of Xrumer and go off it into a Banknotes machine.

We also provide the cheapest prices on the market. Numberless competitors will charge 2x or even 3x and a a pile of the term 5x what we responsibility you. But we feel in providing gigantic service at a low affordable rate. The large something of purchasing Xrumer blasts is because it is a cheaper substitute to buying Xrumer. So we focusing to support that bit in rebuke and provide you with the cheapest censure possible.

Not just do we have the unexcelled prices but our turnaround heyday after your Xrumer posting is wonderful fast. We will pull someone's leg your posting done to come you know it.

We also produce you with a full log of successful posts on manifold forums. So that you can catch a glimpse of for yourself the power of Xrumer and how we hold harnessed it to emoluments your site.[/b]

[b]Search Engine Optimization

Using Xrumer you can wish to realize thousands upon thousands of backlinks over the extent of your site. Scads of the forums that your Location you intent be posted on have acute PageRank. Having your join on these sites can really help establish up some top dignity help links and really aid your Alexa Rating and Google PageRank rating owing to the roof.

This is making your site more and more popular. And with this inflate in reputation as familiarly as PageRank you can expect to see your milieu definitely rank high-pitched in those Search Locomotive Results.

The amount of conveyance that can be obtained by harnessing the power of Xrumer is enormous. You are publishing your site to tens of thousands of forums. With our higher packages you may regular be publishing your site to HUNDREDS of THOUSANDS of forums. Imagine 1 post on a all the rage forum drive by enter 1000 or so views, with signify 100 of those people visiting your site. Modern create tens of thousands of posts on celebrated forums all getting 1000 views each. Your freight ordain go sometimes non-standard due to the roof.

These are all targeted visitors that are interested or bizarre about your site. Imagine how divers sales or leads you can achieve with this titanic number of targeted visitors. You are truly stumbling upon a goldmine bright to be picked and profited from.

Retain, Above is Money.


At 2/05/2010 4:10 AM, Anonymous Anonymous said...

Validate Our Crestfallen Prices at, The Unequalled [b][url=]Online Dispensary [/url][/b] To [url=]Buy Viagra[/url] Online ! You Can also Espy Vast Deals When You [url=]Buy Cialis[/url] and When You You [url=]Buy Levitra[/url] Online. We Also Be subjected to a Mammoth Generic [url=]Phentermine[/url] In gum of Your Intake ! We Present away Marque adduce [url=]Viagra[/url] and Also [url=]Generic Viagra[/url] !

At 2/19/2010 9:58 PM, Anonymous Anonymous said...

Dear John,

For long time I use this free software: [url=]FLV to MP3 free converter[/url].

FLV to MP3 free converter is a free YouTube, MegaVideo, Dailymotion and similar video sites to MP3 Converter and allows you to convert a video to MP3 file.

This software is fast, free, and requires no signup. All you need is a FLV Video file, and this software will extract the MP3, and give you an audio file.

So you are able to listen to your favorite YouTube tracks on every MP3 player.

You can download it for free at [url=][/url].

I hope this help you.

At 2/20/2010 12:05 PM, Anonymous Anonymous said...

I'm new around here, seems like a cool place though. I'll be around a bit, more of a lurker than a poster though :)
[url=]Acai Berry[/url]
Acai Berries
Acai Berry
Acai Berry

At 3/11/2010 4:06 AM, Anonymous Anonymous said...

It isn't hard at all to start making money online in the undercover world of [URL=]blackhat video[/URL], Don’t feel silly if you don't know what blackhat is. Blackhat marketing uses little-known or not-so-known avenues to build an income online.

At 3/17/2010 10:15 AM, Anonymous Anonymous said...

It isn't hard at all to start making money online in the underground world of [URL=]blackhat video[/URL], You are far from alone if you haven’t heard of it before. Blackhat marketing uses little-known or little-understood ways to build an income online.

At 3/18/2010 3:51 PM, Anonymous Anonymous said...

At 3/19/2010 12:08 PM, Anonymous Anonymous said...


When ever I surf on web I never forget to visit this website[url=].[/url]Plenty of useful information on I am sure due to busy scedules we really do not get time to care about our health. Are you really serious about your weight?. Research points that about 70% of all United States grownups are either obese or weighty[url=].[/url] So if you're one of these citizens, you're not alone. Infact many among us need to lose 10 to 20 lbs once in a while to get sexy and perfect six pack abs. Now the question is how you are planning to have quick weight loss? You can easily lose with with little effort. If you improve some of your daily diet habbits then, its like piece of cake to quickly lose weight.

About me: I am webmaster of [url=]Quick weight loss tips[/url]. I am also health expert who can help you lose weight quickly. If you do not want to go under painful training program than you may also try [url=]Acai Berry[/url] or [url=]Colon Cleansing[/url] for fast weight loss.

At 3/26/2010 11:55 AM, Anonymous Anonymous said...

[url=]Porn video online[/url]
[url=]Porn video download[/url]
[url=]Porn video torrent[/url]
[url=]Gay porn online[/url]
[url=]Teen porn online[/url]
[url=]Free porn videos online[/url]
[url=]Best porn video[/url]

At 6/02/2010 7:04 AM, Anonymous Anonymous said...

What assholes are there around in this world. They have to shit into every corner with their spam.

At 6/16/2010 3:53 PM, Anonymous Anonymous said...

work from home

At 7/11/2010 5:36 PM, Anonymous Anonymous said...

link for [b]download software for windows[/b] is available at:

download software for windows
[url=http://www.downloadsoftwareforwindows]download software for windows[/url]

[url=http://www.downloadsoftwareforwindows/products/download-dvd-to-apple-tv-converter/productpage.php]download dvd to apple tv converter[/url]

download youtube downloader

At 7/12/2010 1:52 PM, Anonymous Anonymous said...

url for [b]buy software for windows[/b] is available at:

buy software for windows
[url=]buy software for windows[/url]

[url=]buy dvd to apple tv converter[/url]

buy youtube flv url

At 7/14/2010 5:10 PM, Anonymous Anonymous said...

link to [b]software downloads[/b] are available here:

Windows YouTube downloader
[url=]Windows YouTube downloader[/url]

betting software
[url=]betting software[/url]

buy software for windows
[url=]buy software for windows[/url]

download software for windows
[url=]download software for windows[/url]

Download Youtube Videos
[url=]Download Youtube Videos[/url]

FLV to AVI converter
[url=]FLV to AVI[/url]

DVD ripper
[url=]DVD ripper[/url]

Video converter
[url=]Video converter[/url]

Home video converter software
[url=]Home video software[/url]

Poker software
[url=]Poker video software[/url]

Shark Video Downloader
[url=]Shark Video Downloader software[/url]

Simplest YouTube Internet Video Downloader
[url=]Watermark Software[/url]

Popular screensavers
[url=]Popular Screensaver[/url]

Hyper YouTube Magic Tool XXX
[url=]Hyper YouTube Magic Tool XXX[/url]

Free FLV converter
[url=]Free FLV converter[/url]

Working YouTube downloader
[url=]Working YouTube downloader[/url]

At 8/23/2010 12:13 PM, Anonymous Anonymous said...

Choreograph the brutal with two backs casinos? digging this advanced [url=]casino[/url] advisor and in online casino games like slots, blackjack, roulette, baccarat and more at .
you can also arrest our up to the anybody sec [url=]casino[/url] provide something at and heed a on with in sincere forced conceive of !
another lone [url=]casino spiele[/url] acreage is , in lieu of of german gamblers, the lavatory tailor-made manumitted [url=]online casino[/url] bonus. so check this leading [url=]online casino[/url] for free [url=]casino bonus[/url] and 100's of online [url=]casino[/url] games.

At 8/06/2011 7:46 AM, Anonymous Anonymous said...

iphone 4 unlock
how to unlock iphone 4
unlock iphone 4
i have an anti virus that I bought, but when sometimes I hit a link or something on the internet, it will automatically detect a threat and it shows like its scanning something like in red color, and it tells me to install something, which is not even related to my anti virus, it seems like theres something totally different that its scanning, like it says anti virus threats scan, what is this? what should I do?
unlock iphone 4 how to unlock iphone 4

iphone 4 unlock [url=]iphone 4 unlock[/url] unlock iphone 4 iphone 4 unlock

At 1/07/2013 4:14 AM, Anonymous Anonymous said...

top [url=][/url] check the latest [url=]casino bonus[/url] unshackled no set aside hand-out at the foremost [url=]baywatchcasino

At 1/18/2013 4:27 AM, Anonymous Anonymous said... stibirmDiet make money online fast Sapigetip [url=]EarnMoneyOnlineLikeUs[/url]

At 2/04/2013 1:00 AM, Anonymous Anonymous said...

top [url=]casino games[/url] check the latest [url=]free casino games[/url] autonomous no consign reward at the chief [url=]casino games

At 2/22/2013 4:17 PM, Anonymous Anonymous said...

Hello. And Bye. Thank you very much.


Post a Comment

<< Home