Thursday, February 19, 2009

[VC++] Checking your window is visible to the user ?

Are you interested in knowing programmatically, that your application's windows are visible or not ? You might be familiar with IsWindowVisible(), but this function won't give you the right answer to identify whether your window is partially shown or some other window is over your window or so. It is possible to check the window visiblity by clipping the window with GetClipBox(); below function demonstrate the same, what you have to do is call the below function passing handle to the window and check the return value visibile, partial visible or hidden.
// Enumeration for return values
enum VISIBLE_STATS_e
{
WND_ERROR = -1, // Failed getting visiblity
WND_HIDDEN = 0, // Window is hidden
WND_PARTIAL_VISIBLE = 1, // Window is partially shown
WND_FULL_VISIBLE = 2 // Window is fully shown
};

// Utility function which will return depending the windows current status
VISIBLE_STATS_e GetWindowVisibleStatus( HWND hWnd_i )
{
VISIBLE_STATS_e eRetStatus = WND_ERROR;
if( IsWindow( hWnd_i ))
{
CRect ClientRect;
CRect ClipRect;
HDC hWndDC = GetDC( hWnd_i );
// Get the Window associated to the client rectangle
GetClientRect( hWnd_i, &ClientRect );
// Get the clipping rect of window
switch ( GetClipBox( hWndDC, &ClipRect ))
{
case NULLREGION:
eRetStatus = WND_HIDDEN;
break;
case COMPLEXREGION:
eRetStatus = WND_PARTIAL_VISIBLE;
break;
case SIMPLEREGION:
// If client area is visible then treat it as visible
if( EqualRect( &ClipRect, &ClientRect ))
{
eRetStatus = WND_FULL_VISIBLE;
}
else
{
eRetStatus = WND_PARTIAL_VISIBLE;
}
break;
default:
eRetStatus = WND_ERROR;
break;
}
ReleaseDC( hWnd_i, hWndDC );
}
return eRetStatus;
}

No comments: