You are invited to Log in or Register a free Frihost Account!

Software programming with Visual Basic (VB) - window shaper

Many coders want to know how to shape a window around the image of a bitmap. That shape is a window region. But first, what is a region, really? Think of the bricks of a house. A region is really an array of rectangles that are stacked on top of each other. An individual rectangle can be as small as a 1x1 pixel. A complex region can contain hundreds or even thousands of such rectangles where a simple region contains only one rectangle.

The code behind the process is no secret and the logic is very simple.

* Determine which color will be the "transparent" color of the bitmap
* Check each pixel for that color
* Create a region portion that contains adjacent non-transparent pixels
* Add that region portion to a master region
* Delete the region portion
* When all done, apply the master region to your window

To accomplish this task, generally 5 APIs are used:

* GetPixel to determine the pixel color
* CreateRectRgn to create the region portion
* CombineRgn to append the region to the master region
* DeleteObject to destroy the region portion else memory leaks
* SetWindowRgn to apply the window shape

When the bitmap being used has most of its "transparent" colors close to each other, the above logic can produce fairly quick results. A major speed enhancement was to replace GetPixel with GetDIBits, so the pixels could be returned inside an array which can be traversed much faster than using GetPixel.

Now this is where most shapers stop when it comes to improvements. Unfortunately, we are left with the CreateRectRgn, CombineRgn & DeleteObject APIs which are applied to every portion of the region as it is being accumulated. Remember earlier we said there could be thousands of these portions. What if you don't need to call those APIs, how much faster would the routine return a region for your window? The difference can be noticed by the naked eye!

The next major speed enhancement: build the region array by hand vs API
We know how Windows likes it regions: left to right, top to bottom and no row overlaps another row. Well, if we can put those rectangles into an array in the proper order, we can bypass those other costly API calls and simply call a single API when we are all done arranging the array. That API is ExtCreateRegion. Enjoy

Edited: Attachments
1. The code in a text document that can be changed to .bas if desired
2. A really simple sample is also provided. The sample form only has 2 lines of code.
3. The "fun example" uses the shaper as a basis for creating some neat special effects using regions in different ways. It also uses a host of other region-related APIs that you may not be aware of. Maybe it'll get those synapses firing.
Related topics
which one programming lanuge result's the best speed
MS Visual Basic Studio 2005 Express Edition
Visual Basic Tutorial - File I/O in VB: Text Files
Visual Basic Tutorial - Control Arrays: What, Why and How
Visual Basic Tutorial - Looping
Visual Basic Express Addition
graphic programming in visual basic
Visual Basic 6
Visual Basic Programming
Visual Basic 6
VB Help for those who need small pointers in Visual Basic
visual basic
Visual Basic 6 vs Visual Basic 2010
Visual basic 6
Reply to topic    Frihost Forum Index -> Scripting -> Others

© 2005-2011 Frihost, forums powered by phpBB.