2010-09-14 117 views

回答

16

致電Windows.GetForegroundWindow()然後將HWND傳遞給Controls.FindControl()函數。如果HWND屬於您的進程,它將返回非零TWinControl指針。例如:

if FindControl(GetForegroundWindow()) <> nil then 
    // has focus ... 
else 
    // does not have focus ... 
4

如果您的應用程序由一個單一的形式,然後

GetForegroundWindow = Handle 

就足夠了。上面的表達式當且僅當您的窗體是前景窗口,也就是說,如果鍵盤焦點屬於此窗體上的控件(或窗體本身),則上述表達式爲true。

如果您的應用程序包含多個表單,只需遍歷它們並檢查它們中的任何一個是否匹配GetForegroundWindow

0

上Remys響應的細微變化是:

Var 
    Win: TWinControl; 
Begin 
    Win := FindControl(GetForegroundWindow); 
    if Win <> nil then 
//  StringGrid1.Row :=5; 
//  StringGrid1.SetFocus; 

編譯OK的我,但我發現它不可靠的調試過程中,stringgrid.setfocus被稱爲即使窗口ISN」重點導致一個錯誤。

3

D2007具有這種有用的屬性

if Application.Active then 
// 
相關問題