2005. március 25., péntek

How to check if a control is partially covered by another window


Problem/Question/Abstract:

Is there a way that I can know if there is a 'Stay On Top' form owned by another application partially covering my control?

Answer:

You would have to iterate over all windows above yours in Z-order and check for each window you find if it has the WS_EX_TOPMOST exstyle set and is visible. If it has, you have to get its window rectangle (GetWindowRect) and test if that overlaps your window. Example:

procedure TForm1.Button1Click(Sender: TObject);

  function IsTopmost(wnd: HWND): Boolean;
  begin
    Result := (GetWindowLong(wnd, GWL_EXSTYLE) and WS_EX_TOPMOST) <> 0;
  end;

  procedure logWindowInfo(wnd: HWND);
  const
    visString: array[Boolean] of string = ('not ', '');
  var
    buffer: array[0..256] of Char;
    r: TRect;
  begin
    if wnd = 0 then
      exit;
    GetClassname(wnd, buffer, sizeof(buffer));
    memo1.lines.add(format(' Window of class %s ', [buffer]));
    Windows.getWindowrect(wnd, r);
    memo1.lines.add(format(' at (%d,%d):(%d,%d)', [r.left, r.top, r.right,
      r.bottom]));
    memo1.lines.add(format(' Window is %svisible',
      [visString[IsWindowVisible(wnd)]]));
    memo1.lines.add(format(' Window is %stopmost', [visString[IsTopmost(wnd)]]));
  end;

var
  wnd: HWND;
begin
  memo1.clear;
  wnd := handle;
  repeat
    wnd := GetNextWindow(wnd, GW_HWNDPREV);
    LogWindowInfo(wnd);
  until
    wnd = 0;
  memo1.lines.add('End log');
end;

An easier approach would be to make your own window topmost while it is active.

Nincsenek megjegyzések:

Megjegyzés küldése