...simulate ALT + TAB?

Author: Evian
Homepage: http://www.Phillsoft.de

Category: System

{ Wenn man die ALT + TAB Funktion von Windows über einen Button des eigenen
Programms realisieren möchte, reicht es nicht aus, einfach nur das Drücken
der Tasten ALT + TAB zu simulieren, weil bei jedem Klick auf den eigenen
Button, das eigene Programm den Fokus erhält und an den Anfang der List
gestellt wird.
Deshalb muss man sich alle aktiven Programm "merken" und selber ihnen selber
per Liste den Fokus geben. }

{ If you want to simulate the ALT + TAB function of windows, by clicking a
button of your own program, you cant only simulate to press the ALT and TAB
keys, because in the moment, when you  click on your button, your program will
get the focus and will be set at the beginning of  the List. Because of this
you have to remember all the active programs and to give them the focus by your
own list. }


var
  
Index: INTEGER;

// Save description of all active windows to listbox
function EnumWindowsProc(Wnd: HWND; lParam: lParam): BOOL; stdcall;
var 
  
Bezeichnung: array[0..200] of Char;
begin
  if 
(IsWindowVisible(Wnd) or IsIconic(wnd)) and
    
((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
    
(GetWindowLong(Wnd, GWL_HWNDPARENT) = GetDesktopWindow)) and
    
(GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then 
  begin
    
GetWindowText(Wnd, Bezeichnung, 256);
    if Bezeichnung <> 'GDI+ Window' then
      
Form1.Listbox1.Items.Append(Bezeichnung);
  end;
end;

procedure TForm1.Refresh;
begin
  
Listbox1.Clear;
  EnumWindows(@EnumWindowsProc, 1);
end;

// Simulate ALT + TAB
procedure Forwardtab;
var
  
hWnd: DWORD;
begin
  
Refresh;
  if Index < Listbox1.Count - 1 then
    
Inc(Index) 
  else
    
Index := 0;

  hWnd := FindWindow(nil, PChar(Listbox1.Items[Index]));
  if hWnd <> 0 then
  begin
    
windows.ShowWindow(hwnd, 1);
    windows.SetForegroundWindow(hWnd);
    windows.SetFocus(hWnd);
  end;
end;

// Simulate ALT + TAB (Backwards)
procedure Backwardtab;
var
  
hWnd: DWORD;
begin
  
Refresh;
  if Index > 0 then
    
Dec(Index) 
  else
    
Index := listbox1.Count - 1;

  hWnd := FindWindow(nil, PChar(Listbox1.Items[Index]));
  if hWnd <> 0 then
  begin
    
windows.ShowWindow(hwnd, 1);
    windows.SetForegroundWindow(hWnd);
    windows.SetFocus(hWnd);
  end;
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base