SHARE
Facebook X Pinterest WhatsApp

A tool to convert bitmaps to region files

Environment: VC++ 4.2 Since the arcticle Transparent Window by Franz Polzer helped me a lot, I would like to contribute a little tool that converts bitmaps to region-files suitable to use as a window-mask (SetWindowRgn). Why does anybody need this tool? its flexible; its possible to adjust the key-color its fast (not the tool, but […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Jan 24, 1999
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC++ 4.2

Since the arcticle
Transparent Window
by Franz Polzer helped
me a lot, I would like to contribute a little tool that converts bitmaps
to region-files suitable to use as a window-mask (SetWindowRgn).

Why does anybody need this tool?

  • its flexible; its possible to adjust the key-color
  • its fast (not the tool, but your app); loading a region is better than
    creating it online
  • its free; source is included

How to use it?

    enter a source bitmap path (or press browse)
  • enter a key color (or click onto any pixel of the source bitmap)
  • press “calc !” wait and see the result
  • enter a destination path (or press browse)
  • press “write”

To use those region-files in your own project, you need to do the following:
Import the *.RGN-file into your project-resources. The type of that resource
needs to be set to “Data”.

You can also import it manually by editing your *.rc file.
Just insert something like the following:

IDR_SAMPLE_REGION    RCDATA  DISCARDABLE     “res\sample.rgn”

Below is some example code that loads and activates a region,
it should be called from something like “OnInitDialog”.

//nRegionID – resource ID of the region data
BOOL CTransparentWnd::InitRegion (unsigned short nRegionID)
{
	const char *MSGCAPTION=”Init Region”;
	HRSRC hrsrc;		//handle of the resource
	HGLOBAL hmem;		//handle to resource memory
	DWORD dwSize;		//size of our resource
	RGNDATA *rd;		//region data
	CRgn rgn;			//the region itself
	//try to locate the resource
	if ((hrsrc=FindResource(NULL,
	                        (LPCSTR)nRegionID,
	                         RT_RCDATA)) == NULL)
	{
		MessageBox (“failed to find region”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//load the resource
	if ((hmem=LoadResource (NULL,hrsrc)) == NULL)
	{
		MessageBox (“failed to load region”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//get the size of the region-data structure (= header + data)
	if ((dwSize=SizeofResource (NULL,hrsrc)) == 0)
	{
		MessageBox (“region has an invalid size”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//lock the resource to get a pointer for it
	if ((rd=(RGNDATA *)LockResource (hmem)) == NULL)
	{
		MessageBox (“failed to lock resource memory”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//create a region from our data
	if (!rgn.CreateFromData (NULL,dwSize,rd))
	{
		MessageBox (“failed to create a region from that data”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//setup
	if (!SetWindowRgn (GetSafeHwnd(),(HRGN)rgn,TRUE))
	{
		MessageBox (“failed to set the region for that window”,MSGCAPTION,MB_OK);
		return FALSE;
	}
	//NOTE: win32-based apps do not need to unlock or free loaded resources (not manually)
	return TRUE;
}

Download project files – 34 KB

Recommended for you...

Drawing 3D OpenGL Graphics on Google Maps
Mandelbrot Using C++ AMP
CodeGuru Staff
Jan 27, 2012
Simple C++ MP3 Player Class
CodeGuru Staff
Aug 22, 2011
Library for Raw Video Processing
CodeGuru Staff
Jun 14, 2011
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.