Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Tuesday, June 06, 2006

A function to detect if a window is visible

In a discussion with author of StarDict, we found out that it was not straightforward to check if a window was visible or not. Several APIs have been tried, and  at last I came up with the following solution. It is still not perfect, so I am happy to get feedback from others.

 

bool is_window_visible( HWND main_window) {

                   

RECT rc;

::GetWindowRect(main_window,&rc);

POINT pt;

pt.x = rc.left+1;

pt.y = rc.top+1;

HWND hwnd1 = WindowFromPoint(pt);

pt.x = rc.left+1;

pt.y = rc.bottom-1;

HWND hwnd2 = WindowFromPoint(pt);

pt.x = rc.right-1;

pt.y = rc.top+1;

HWND hwnd3 = WindowFromPoint(pt);

pt.x = rc.right-1;

pt.y = rc.bottom-1;

HWND hwnd4 = WindowFromPoint(pt); 

 

if( main_window == hwnd1

 && main_window == hwnd2

 && main_window == hwnd3

  && main_window == hwnd4 )

  return true;

  else

  return false;    

}

 

0 Comments:

Post a Comment

<< Home