我需要一种方法来使自定义控件(派生自TCustomControl)能够判断它当前是否可见。我指的不是.Visible属性,而是指它是否正在屏幕上实际显示。有人知道如何做到这一点吗?
我需要一种方法来使自定义控件(派生自TCustomControl)能够判断它当前是否可见。我指的不是.Visible属性,而是指它是否正在屏幕上实际显示。有人知道如何做到这一点吗?
{----------------------------------------------------------}
function IsMyFormCovered(const MyForm: TForm): Boolean;
var
MyRect: TRect;
MyRgn, TempRgn: HRGN;
RType: Integer;
hw: HWND;
begin
MyRect := MyForm.BoundsRect; // screen coordinates
MyRgn := CreateRectRgnIndirect(MyRect); // MyForm not overlapped region
hw := GetTopWindow(0); // currently examined topwindow
RType := SIMPLEREGION; // MyRgn type
// From topmost window downto MyForm, build the not overlapped portion of MyForm
while (hw<>0) and (hw <> MyForm.handle) and (RType <> NULLREGION) do
begin
// nothing to do if hidden window
if IsWindowVisible(hw) then
begin
GetWindowRect(hw, MyRect);
TempRgn := CreateRectRgnIndirect(MyRect);// currently examined window region
RType := CombineRgn(MyRgn, MyRgn, TempRgn, RGN_DIFF); // diff intersect
DeleteObject( TempRgn );
end; {if}
if RType <> NULLREGION then // there's a remaining portion
hw := GetNextWindow(hw, GW_HWNDNEXT);
end; {while}
DeleteObject(MyRgn);
Result := RType = NULLREGION;
end;
function IsMyFormVisible(const MyForm : TForm): Boolean;
begin
Result:= MyForm.visible and
isWindowVisible(MyForm.Handle) and
not IsMyFormCovered(MyForm);
end;
你可以将代码附加到OnPaint事件上吗?这个事件会被频繁调用,但只有在控件实际绘制(例如以您指定的方式可见)时才会被调用。
function IsMyFormCovered(const MyForm: TForm): Boolean;
var
RectForm, RectForeground: TRect;
hWndForeground: HWND;
begin
GetWindowRect(MyForm.Handle, RectForm);
hWndForeground := GetForegroundWindow;
if (hWndForeground <> MyForm.Handle) and (hWndForeground <> 0) then
begin
GetWindowRect(hWndForeground, RectForeground);
if (RectForm.Left < RectForeground.Right) and
(RectForm.Right > RectForeground.Left) and
(RectForm.Top < RectForeground.Bottom) and
(RectForm.Bottom > RectForeground.Top) then
Result := True
else
Result := False;
end else
Result := False;
end;