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,
MAKEINTRESOURCE(IDD_DIALOG1),
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,
LR_LOADFROMFILE);
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++) { do { //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) { return; } //delete the temp region for next pass DeleteObject(hRgnTemp); }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:
case WM_LBUTTONDOWN:
mouseState = 1;
return TRUE;
case WM_MOUSEMOVE:
if(mouseState)
{
GetCursorPos(&CursPt);
PostMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION,
MAKELPARAM( CursPt.x, CursPt.y));
mouseState = 0;
}
return TRUE;
Downloads
Download demo project - 11 KbDownload source - 6 Kb

Comments
Chemise Burberry Pas Cher
Posted by Wrinalertiece on 04/29/2013 11:55pmburberry femme pas cher sacoche burberry pas cher burberry femme pas cher Chemise Burberry Pas Cher [url=http://chemiseburberry6.webnode.fr/]chemiseburberry6.webnode.fr[/url] , Chemise Burberry [url=http://chemiseburberry6.webnode.fr/]chemiseburberry6.webnode.fr[/url] , burberry femme pas cher
ReplyBizarre article gives you the key points on mizuno which just a few customers are aware of.
Posted by icoppyapedcap on 04/26/2013 11:43amMp [url=http://hunterrainboots.biz/]ãã³ã¿ã¼ãã¼ã[/url] sToc VruKis PwaYbg BolAryD [url=http://www.hunterrainboots.biz/ã»ã¼ã«-c-1.html]ãã³ã¿ã¼ã¬ã¤ã³ãã¼ã[/url] zfKte Xpg [url=http://www.hunterrainboots.biz/ããã°å°ç©-c-5.html]hunter ã¬ã¤ã³ãã¼ã[/url] TvlXsmEkz EjbEcyCl [url=http://www.hunterrainboots.biz/ãã³ãã¹-c-3.html]é·é´[/url] pJdi UpsNrj Qx [url=http://hunter-rain-boots.webnode.jp]ã¬ã¤ã³ãã¼ããã³ã¿ã¼[/url] hPv [url=http://hunterrainbootsjp.webnode.jp]ãã³ã¿ã¼ãã¼ã[/url] f OiaCut ZowS [url=http://hunter-boots8.webnode.jp]ã¬ã¤ã³ãã¼ããã³ã¿ã¼[/url] ad J [url=http://rain-boots-men.webnode.jp]é·é´[/url] oo [url=http://hunter-rain-boots-ja.webnode.jp]ã¬ã¤ã³ãã¼ã[/url] BwaShpNye W[url=http://rainshoesja.webnode.jp]é·é´[/url] hfFdvUbuFi [url=http://ja-hunter-rain-boots.webnode.jp]ãã³ã¿ã¼ã¬ã¤ã³ãã¼ã[/url] q GhpVpb [url=http://rain-boots-popular.webnode.jp]ã¬ã¤ã³ãã¼ã人æ°[/url] Vjn [url=http://rain-boots-men6.webnode.jp]hunter ã¬ã¤ã³ãã¼ã[/url] Miq Mmw [url=http://jahunterrainboots.webnode.jp]ã¬ã¤ã³ãã¼ãã¡ã³ãº[/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=http://www.golfjapan.co/]ãã¼ã©ã¼ã¡ã¤ã[/url] Whatever the industry experts are never saying about golf and the way this affects you. [url=http://www.golfjapan.co/ã¢ã¤ã¢ã³-c-2.html]ã´ã«ã ã¹ã¤ã³ã°[/url] The latest guideline exposes the know-how towards golf as well as , the reasons you should take action now. [url=http://www.golfjapan.co/ã¬ãã£ã¼ã¹-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=http://www.golfjapan.co/ãã©ã³ããã鏿-c-364.html]ãã¼ã ã´ã«ã[/url] Machines and end production in Las Vegas, Nevada - golf simply leaves with no hasta la vista
ReplyRe:
Posted by icon collection on 12/04/2012 03:37pmI think, what is it good idea. BestPictDesign
ReplyThis is simply the best
Posted by Legacy on 12/18/2003 12:00amOriginally posted by: Venelin Petkov
Thanks, really thanks for this example.
ReplySimplest solutions are always the most elegant.
Excellent & Simple
Posted by Legacy on 09/07/2003 12:00amOriginally posted by: Rene Pilon
Very nice!
ReplyGood and not too sophisticated example
Posted by Legacy on 06/04/2003 12:00amOriginally 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 :)
ReplyMay I add a link to my article here -- http://www.codeguru.com/dialog/RenderingRegion.html
Posted by Legacy on 05/28/2003 12:00amOriginally 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.
Reply