Creating Shaped Windows Using Regions with Win32

Environment: VC6, Windows 9x/NT/2000/XP

This article shows how to create a shaped window using a bitmap image file and using the mouse to drag it.

The basic scheme is to scan a bitmap image data, skip over the transparent pixels, and create the region when a non-transparent pixel is found. For subsequent non-transparent pixels, the region is combined with the previous one. The transparent pixel is a particular color value of the pixel that is not used to create the window.

Getting Started

The first requirement is to create a bitmap image using image editors such as MSPaint, Photoshop, and so forth. This bitmap must have, apart from other colors used, one color that is to be interpreted as a transparent color by the application program. For example, white means a pixel RGB value of RGB(255,255,255). So, the program will skip all the bitmap data corresponding to this color for processing.

Understanding the Application

We start by creating a modeless dialog box:

HWND hwndDlg = CreateDialog(hInstance,
                            ghWnd, (DLGPROC)DialogProc);

The next step is creating the region window. This includes loading the bitmap image file into memory and obtaining information about the bitmap image (such as the width and height of the image.)

HANDLE hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),
                                    imageFile, IMAGE_BITMAP, 0, 0,

To create the region, the bitmap data is scanned and all the RGB color values corresponding to the transparent color are skipped from processing. For all the other color values, their corresponding logical coordinates are used to create the region.

//create an empty region
hRgn = CreateRectRgn(0,0,0,0);
//Create a region from a bitmap with transparency colour of white
//change the pixel values for a different transparency color
//ex - RGB(0,0,0) will mean a transparency color of black.. so the
//areas of the bitmap not used to create the window will be black
COLORREF crTransparent = RGB(255, 255, 255);

int iX = 0;
int iRet = 0;
for (int iY = 0; iY < bmpInfo.bmHeight; iY++)
    //skip over transparent pixels at start of lines.
    while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY)
              == crTransparent) iX++;
    //remember this pixel
    int iLeftX = iX;
    //now find first non-transparent pixel
    while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY) !
              = crTransparent) ++iX;
    //create a temp region on this info
    HRGN hRgnTemp = CreateRectRgn(iLeftX, iY, iX, iY+1);
    //combine into main region
    iRet = CombineRgn(hRgn, hRgn, hRgnTemp, RGN_OR);
    if(iRet == ERROR)
    //delete the temp region for next pass
  }while(iX < bmpInfo.bmWidth);
  iX = 0;

After the region is created, the window region is set by calling SetWindowRgn,

iRet = SetWindowRgn(hwndDlg, hRgn, TRUE);

and its position is set by calling SetWindowPos:

iRet = SetWindowPos(hwndDlg, HWND_TOPMOST, iX, iY, bmpInfo.bmWidth,
                    bmpInfo.bmHeight, NULL);

Finally, render the bitmap image into the region by calling the function BitBlt.

BitBlt(hdcDest, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, hdcMem,
                0, 0, SRCCOPY);

Dragging the Window

The window is dragged by handling messages in the dialog procedure:

  mouseState = 1;
  return TRUE;
                MAKELPARAM( CursPt.x, CursPt.y));
    mouseState = 0;
  return TRUE;


Download demo project - 11 Kb
Download source - 6 Kb


  • Does Age Matter? - Era and Erectile Dysfunction

    Posted by avaeffifielm on 06/21/2013 06:37pm

    Is Erectile Dysfunction Home Cure Helpful in Gaining Powerful Erections? Fo-Ti is a traditional Chinese herb used for reversing the old age and of course, reversing impotence [url= ] scary movies viagra [/url] find a lot more How with regard to Treat impotence problems - 4 Secrets Men have to know

  • Chemise Burberry Pas Cher

    Posted by Wrinalertiece on 04/29/2013 11:55pm

    burberry femme pas cher sacoche burberry pas cher burberry femme pas cher Chemise Burberry Pas Cher [url=][/url] , Chemise Burberry [url=][/url] , burberry femme pas cher

  • Bizarre article gives you the key points on mizuno which just a few customers are aware of.

    Posted by icoppyapedcap on 04/26/2013 11:43am

    Mp [url=]ハンターブーツ[/url] sToc VruKis PwaYbg BolAryD [url=セール-c-1.html]ハンターレインブーツ[/url] zfKte Xpg [url=バッグ小物-c-5.html]hunter レインブーツ[/url] TvlXsmEkz EjbEcyCl [url=パンプス-c-3.html]長靴[/url] pJdi UpsNrj Qx [url=]レインブーツハンター[/url] hPv [url=]ハンターブーツ[/url] f OiaCut ZowS [url=]レインブーツハンター[/url] ad J [url=]長靴[/url] oo [url=]レインブーツ[/url] BwaShpNye W[url=]長靴[/url] hfFdvUbuFi [url=]ハンターレインブーツ[/url] q GhpVpb [url=]レインブーツ人気[/url] Vjn [url=]hunter レインブーツ[/url] Miq Mmw [url=]レインブーツメンズ[/url] Qiz Creative questions regarding golf resolved and as a result the reason why you have to read every single term in this post. [url=]テーラーメイド[/url] Whatever the industry experts are never saying about golf and the way this affects you. [url=アイアン-c-2.html]ゴルフ スイング[/url] The latest guideline exposes the know-how towards golf as well as , the reasons you should take action now. [url=レディース-c-11.html]ゴルフ グリップ[/url] Extraordinary summary will give you the run information over golf which experts state just a couple of guys are conscious of. [url=ブランドから選択-c-364.html]プーマ ゴルフ[/url] Machines and end production in Las Vegas, Nevada - golf simply leaves with no hasta la vista

  • Re:

    Posted by icon collection on 12/04/2012 03:37pm

    I think, what is it good idea. BestPictDesign

  • This is simply the best

    Posted by Legacy on 12/18/2003 12:00am

    Originally posted by: Venelin Petkov

    Thanks, really thanks for this example.
    Simplest solutions are always the most elegant.

  • Excellent & Simple

    Posted by Legacy on 09/07/2003 12:00am

    Originally posted by: Rene Pilon

    Very nice!

  • Good and not too sophisticated example

    Posted by Legacy on 06/04/2003 12:00am

    Originally posted by: Ernest Putnikoff

    The Title of my comment is self explanatory, thanks for the source, now working on adding various controls to it, thanks again :)

  • May I add a link to my article here --

    Posted by Legacy on 05/28/2003 12:00am

    Originally posted by: Zhefu Zhang

    do not blame on you, in the case of big bitmap file, the way you used will be terrible slow ( even of a 300 X 200 bitmap will let user feel that). For you modify the region on every pixel. A better way is to make as large as possible contiguous same color points rectangle, and add that rectangle into the region as a whole. please refer my article's reference on that topic.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date