Click here to Skip to main content
Email Password   helpLost your password?

CRegionCreator Test Application Image

Introduction

This article is on creating a custom region from a bitmap and changing the default window skin using this region. A class called CRegionCreator has been written in order to simplify this problem. Also, an example on skinning a custom window has been shown.

Note

This example is using a default skin bitmap for the free media player BSPlayer found in its installation folder, for the purpose of demonstration.

Background

As a background, a reader can take a look at different CodeProject articles on this subject.

Using the code

To use the code, add the following to the class in which you would like to create custom regions:

//

#include "RegionCreator.h"

//

Then, create an instance of the included class and use its only method called CreateRegionFromBitmap() which will return to you a handle of the new created region:

//

CRegionCreator regionCreator;
//

HRGN hRgn = regionCreator.CreateRegionFromBitmap(hBitmap, 
                                       transparentColor);
//

The two arguments passed to this method are a bitmap handle obtained somewhere else and a transparent color in the bitmap. Now, you should have a valid region handle which you should have destroyed after finishing your work with it. In my example, I used a LoadImage() function to load all kinds of bitmaps (4b, 8b, 16b, 24b, 32b) and set as transparent color the color of the top left pixel in the bitmap, but one can pass in just any color. Every pixel that has this value will not be a part of the resulting region. So, in this manner, a very nice region can be formed, either connected or disconnected.

Changing the default window skin

To apply these regions to custom windows, it is necessary to call SetWindowRgn() and pass as arguments a window handle, the newly obtained region handle, and a flag which will indicate if the window needs to be repainted.

//

SetWindowRgn(hWnd, hRgn, TRUE);
//

That is all! After this step, you will have your window in the shape you like.

Small warnings

You should keep in mind the following:

Points of Interest

Working on this subject, I found that it can be very amusing to have a window in different skins. A whole framework can be implemented in this manner, so it's a challenge.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralTips
Mauro Leggieri
13:18 21 Dec '05  
Hi, nice article. I recommend two tips to improve performance.

1) Mark the first non-transparent pixel of a row and the last one, then add the complete rectangle (startx,y,endx,y) and then continue the row scanning.

2) You can save the rectangles into an array of rectangles and create the entire region using ExtCreateRegion API. It's faster because involves less API calls while scanning the bitmap. But take into account that in Win98 ExtCreateRegion() may fail if the number of rectangles is very large so when you reach a number like 2000 create the region and combine them into one.

Best regards,
Mauro H. Leggieri


Last Updated 21 Dec 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010