我如何知道Delphi控件当前是否可见? [英] How can I tell if a Delphi control is currently visible?
问题描述
我需要一种自定义控件的方法(来自TCustomControl)来判断当前是否可见。我不是在谈论.Visible财产;我的意思是说它是否在屏幕上实际显示。有谁知道如何做到这一点?
I need a way to for a custom control (descended from TCustomControl) to tell if it is currently visible. I'm not talking about the .Visible property; I mean whether or not it's actually being displayed on-screen at the moment. Does anyone know how to do this?
推荐答案
几年前,我遇到了同样的问题:我是寻找一种方法来确定窗体是否实际上可见(甚至部分地)给用户。
特别是当它应该是可见的,而显示是True,但是窗口实际上完全落在另一个之后。
这是代码,它可以适应WinControl ...
A few years back I had the same kind of problem for a Form: I was looking for a way to determine if a Form is actually visible (even only partially) to the user.
In particular when it was supposed to be visible and Showing was True but the window was actually entirely behind another one.
Here's the code, it could be adapted for a WinControl...
{----------------------------------------------------------}
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;
这篇关于我如何知道Delphi控件当前是否可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!