![]() |
Platforms, Frameworks & Libraries »
Mobile Development »
Dialogs and Windows
Intermediate
Fast region creation on WinCE/PocketPCBy dzoleeA fast solution to create custom-shaped windows from a bitmap mask |
VC8.0, eVC4.0, Windows, WinMobile5, MFC, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
SetWindowRgn API.Creating a region is the first step towards a custom-shaped window.
Every region creator that I've seen so far uses the same idea:
GetPixel funcion on the bitmap mask to see which areas of the window should be transparent CreateRectRgn to create new rectangular regions based on pixels or pixel blocks CombineRgn function. CombineRgn becomes unacceptably slow.ExtCreateRegion API. The code works fine on WinCE/PocketPC and also on Win32. I've tested it with Embedded Visual C++ 4 and Visual Studio 2005, too.
The CRegionBuilder class (see files RegionBuilder.h and RegionBuilder.cpp) has only one public function, BuildRegion. It uses the ExtCreateRegion API to create a region from manually built region data. The region data consists of a header (a RGNDATAHEADER structure) and an array of RECT structures that make up the region.
The BuildRegion function takes two parameters, a bitmap handle of a loaded bitmap and a pointer to store the resulting region handle:
RegionBuilderError BuildRegion(HBITMAP hBmp, HRGN *pDest);
The function includes every non-black pixel in the resulting region.
Possible return values are: rbeOK, rbeNoMem and rbeGDIError as defined in RegionBuilder.h. If memory allocation fails, it returns rbeNoMem. If any of the used GDI functions return an error, it returns rbeGDIError. If all goes fine, the function return rbeOK and the resulting region handle will be copied to the destination.
Some tips:
DeleteObject on the region handle until your window is visible, instead, free it in OnDestroy. RECTs (eg. 640x480 "random noise"), you might experience sudden device crashes or other drawing problems. The BuildRegion function first gets bitmap dimensions with the GDI GetObject function. To avoid the slow GetPixel function, it reads bitmap bits directly. But the GetObject function does not return a pointer to the bitmap bits unless the bitmap was created with CreateDIBSection. So, BuildRegion creates a new, monochrome bitmap using this function and then copies the source bitmap to it with BitBlt. Using a monochrome version of the bitmap saves a lot of precious memory.
After the monochrome copy has been created, the function loops through the bitmap bits to see how much memory will be required for the RECT array. If there are horizontal lines in the bitmap, they will be packed into one RECT.
Then the function allocates a proper memory block for the rectangles and loops through the bits again to build the RECT array.
It loops twice through the bitmap, so it allocates only the required amount of memory. Despite this it's still pretty fast I think. A lot faster than the ordinary GetPixel + CombineRgn method.
Have fun!
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Mar 2007 Editor: Chris Maunder |
Copyright 2007 by dzolee Everything else Copyright © CodeProject, 1999-2010 Web22 | Advertise on the Code Project |