只要你不断的努力,明天的太阳更加灿烂

努力学好.net

博客园 首页 新随笔 联系 订阅 管理

unit Frm_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Menus,TLHelp32;

type
  TForm_Main = class(TForm)
    ListBox_Course: TListBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Timer1: TTimer;
    procedure ListBox_CourseDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private

    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Main: TForm_Main;
  function ForceForegroundWindow(hwnd: THandle): boolean;
 
  Function EnumWindowsProc (Wnd: HWND; lb: TListbox): BOOL; stdcall;
implementation

{$R *.dfm}

Function EnumWindowsProc (Wnd: HWND; lb: TListbox): BOOL; stdcall;
var
 caption: Array [0..128] of Char;
begin
 Result := True;
 if { skip invisible windows }
    IsWindowVisible(Wnd) and
    { only process truly top-level windows. GetWindowLong must be used, not
GetParent }
    ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
     (HWND(GetWindowLong(Wnd, GWL_HWNDPARENT)) = GetDesktopWindow)) and
    { skip WS_EX_TOOLWINDOW windows }
    ((GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW) = 0)
 then begin
   SendMessage( Wnd, WM_GETTEXT, Sizeof( caption ), integer(@caption));
   lb.Items.AddObject( caption, TObject( Wnd ));
 end;
end;

 

function ForceForegroundWindow(hwnd: THandle): boolean;
const
  SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
  SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
  ForegroundThreadID: DWORD;
  ThisThreadID      : DWORD;
  timeout           : DWORD;
begin
  if IsIconic(hwnd) then
    ShowWindow(hwnd, SW_RESTORE);

  if GetForegroundWindow = hwnd then
    Result := true
  else begin
    if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4))
    or ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and ((Win32MajorVersion > 4)
    or ((Win32MajorVersion = 4)and (Win32MinorVersion > 0)))) then
    begin
      Result := false;
      ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow,nil);
      ThisThreadID := GetWindowThreadPRocessId(hwnd,nil);
      if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
      begin
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hwnd);
        AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
        Result := (GetForegroundWindow = hwnd);
      end;
      if not Result then begin
        // Code by Daniel P. Stasinski
        SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),SPIF_SENDCHANGE);
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hWnd);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,TObject(timeout), SPIF_SENDCHANGE);
      end;
    end
    else begin
      BringWindowToTop(hwnd); // IE 5.5 related hack
      SetForegroundWindow(hwnd);
    end;
    Result := (GetForegroundWindow = hwnd);
  end;
end;

 

procedure TForm_Main.ListBox_CourseDblClick(Sender: TObject);
var
    h : HWND;
    i :integer;
begin
    for i:=0 to ListBox_Course.Items.Count-1 do
    begin
        if  ListBox_Course.Selected[i]=True then
        begin
            h := FindWindow(nil, PChar(ListBox_Course.Items.Strings[i]));
            if h > 0 then  begin
                SetWindowPos(h,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE or SWP_NOMOVE);
                ForceForegroundWindow(h);
            end;
            Exit;
        end;
    end;
end;

 

procedure TForm_Main.FormCreate(Sender: TObject);
begin
     EnumWindows( @EnumWindowsProc, integer( ListBox_Course ));
end;

procedure TForm_Main.N1Click(Sender: TObject);
begin
    ListBox_Course.Clear;
    EnumWindows( @EnumWindowsProc, integer( ListBox_Course ));
end;


procedure TForm_Main.Timer1Timer(Sender: TObject);
begin
    ListBox_CourseDblClick(Sender);
end;

end.

posted on 2008-08-10 14:39  张有明  阅读(229)  评论(0)    收藏  举报