-
August 21st, 2006, 06:48 AM
#1
Windows SDK GDI: How do I calculate the area of a window exposed?
Q: How do I calculate the area of a window exposed?
A: How to find the area exposed (not covered by other windows) on your desktop? There is no default function for that. Here is an easy method to find out.
The method followed is to create a region corresponding to an area covered by the original window and keep on subtracting the overlapping region covered by other windows on the desktop above the specified window in Z-order. Once the exposed region is calculated, its area can be calculated by counting the points in that region.
I have written two functions 'Visible()' to calculate the percentage area exposed by the window and 'Area()' to calculate the area of a region. Both the functions are self-explanatory but commented.
Code:
long Area(HRGN hrgn)
{
RECT Rect;
GetRgnBox(hrgn, &Rect);
for (int N = 0, X = Rect.left; X < Rect.right; X++)
for (int Y = Rect.top; Y < Rect.bottom; Y++)
if (PtInRegion(hrgn, X, Y))
N++;
return N;
}
float Visible(HWND hwnd)
{
// Calculates full area of total window
RECT Rect;
GetWindowRect(hwnd, &Rect);
int FullArea = (Rect.right - Rect.left) * (Rect.bottom - Rect.top);
// Creates a region corresponding to the window
HRGN hRgn = CreateRectRgn(Rect.left, Rect.top, Rect.right, Rect.bottom);
// Gets the topmost window
HWND hWnd = GetWindow(hwnd, GW_HWNDFIRST);
while (hWnd != NULL && hWnd != hwnd) // If the window is above in Z-order
{
if (IsWindowVisible(hWnd)) // If the window is visible
{
// Gets window dimension
GetWindowRect(hWnd, &Rect);
// Creates a region corresponding to the window
HRGN hrgnWnd = CreateRectRgn(Rect.left, Rect.top, Rect.right, Rect.bottom);
// Creates a region corresponding to region not overlapped
CombineRgn(hRgn, hRgn, hrgnWnd, RGN_DIFF);
}
// Loops through all windows till the specified window
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
}
// Returnes exposed area in percentage
return (float) (100.0 * Area(hRgn) / FullArea);
}
I did the testing by creating a timer in my project and repeatedly calculating the area exposed. The area currently exposed is made as the application window title. By manipulating other windows, I could see that the area exposed is displayed as the title of the application window.
Last edited by Andreas Masur; November 4th, 2006 at 01:01 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|