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 Kb
Download source - 6 Kb


Comments

  • 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=http://rxhealth.pw ] scary movies viagra [/url] find a lot more How with regard to Treat impotence problems - 4 Secrets Men have to know

    Reply
  • 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=http://chemiseburberry6.webnode.fr/]chemiseburberry6.webnode.fr[/url] , Chemise Burberry [url=http://chemiseburberry6.webnode.fr/]chemiseburberry6.webnode.fr[/url] , burberry femme pas cher

    Reply
  • 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=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

    Reply
  • Re:

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

    I think, what is it good idea. BestPictDesign

    Reply
  • 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.

    Reply
  • Excellent & Simple

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

    Originally posted by: Rene Pilon

    Very nice!

    Reply
  • 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 :)

    Reply
  • May I add a link to my article here -- http://www.codeguru.com/dialog/RenderingRegion.html

    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.

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

Top White Papers and Webcasts

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds