Problem Source: http://www.cnblogs.com/del/archive/2008/12/13/1081644.html#1400835
Getactivewindow only obtains the currentProgram(Strictly speaking, it is in the thread) is activated in the window;
Getforegroundwindow is used to obtain the window activated in the current system.
The two functions have different levels. One is the line level and the other is the system level.
The activated window is not necessarily the top-level window (the top window ).
The following example illustrates the problem. Test method: Click the three buttons and switch the focus and observe repeatedly.
Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) timer1: ttimer; label1: tlabel; label2: tlabel; button1: tbutton; button2: tbutton; button3: tbutton; Procedure processing (Sender: tobject); Procedure processing (Sender: tobject); Procedure formcreate (Sender: tobject); Procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var I: integer; Procedure tform1.timer1timer (Sender: tobject); var Buf: array [byte] of char; begin getwindowtext (getactivewindow, Buf, length (BUF) * sizeof (BUF [0]); label1.caption: = 'getactivewindow: '+ Buf; getwindowtext (getforegroundwindow, Buf, length (BUF) * sizeof (BUF [0]); label2.caption: = 'getforegroundwindow: '+ Buf; end; Procedure tform1.button1click (Sender: tobject); var frm: tform1; begin Inc (I); frm: = tform1.create (Self ); FRM. formstyle: = fsnormal; frm. text: = format ('normal % d', [I]); frm. show; end; Procedure tform1.button2click (Sender: tobject); var frm: tform1; begin Inc (I); frm: = tform1.create (Self); frm. formstyle: = fsstayontop; frm. text: = format ('stayontop % d', [I]); frm. show; end; Procedure tform1.button3click (Sender: tobject); begin winexec('notepad.exe ', sw_normal); winexec('calc.exe', sw_normal); end; procedure merge (Sender: tobject); begin timer1.interval: = 100; button1.caption: = 'create normal Windows'; button2.caption: = 'create top-level Windows'; button3.caption: = 'start notepad and calculator'; end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 155 clientwidth = 250 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false oncreate = formcreate pixelsperinch = 96 textheight = 13 object label1: tlabel left = 32 top = 24 width = 281 Height = 17 caption = 'label1' end object label2: tlabel left = 32 top = 47 width = 281 Height = 17 caption = 'label2' end object button1: tbutton left = 24 Top = 87 width = 97 Height = 25 caption = 'button1' taborder = 0 onclick = button1click end object button2: tbutton left = 136 Top = 87 width = 97 Height = 25 caption = 'button2' taborder = 1 onclick = button2click end object button3: tbutton left = 24 Top = 118 width = 209 Height = 25 caption = 'button3' taborder = 2 onclick = button3click end object timer1: ttimer ontimer = timer1timer left = 208 Top = 56 endend