请教一下用什么空间可以用来监视当前桌面活动窗口标题,谢谢。

解决方案 »

  1.   

    不用控件,调用APIunit CapWinU;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        Timer1: TTimer;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Edit1: TEdit;
        procedure Timer1Timer(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    var
      scrdc: HDC;procedure TForm1.Timer1Timer(Sender: TObject);
    var
      hwnd: THandle;
      buf: array [0..127] of Char;
      winclass: string;
      wintitle: string;
    begin
      hwnd := GetForegroundWindow();
      if GetClassName(hwnd, buf, 127) > 0 then
      begin
        winclass := StrPas(buf);
        if GetWindowText(hwnd, buf, 127) > 0 then
          wintitle := StrPas(buf)
        else
          wintitle := '';
        winclass := winclass + '(' + wintitle + ')';
      end;
      if winclass = '' then
        winclass := '######';
      if ListBox1.Items.Indexof(winclass) < 0 then
        ListBox1.Items.Add(winclass);
      if scrdc <> 0 then
      begin
        FillRect(scrdc, Rect(200, 0, 600, 32), COLOR_WINDOW+1);
        TextOut(scrdc, 220, 8, @winclass[1], Length(winclass));
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Items.Clear();
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if ListBox1.ItemIndex >= 0 then
        ListBox1.Items.Delete(ListBox1.ItemIndex);
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      ListBox1.Items.SaveToFile(Edit1.Text);  
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      scrdc := GetDC(0);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      if scrdc <> 0 then
        ReleaseDC(0, scrdc);
    end;end.