unit1.pas

来自「精彩编程百例26~50 其中有 控制任务栏 windows底层任务控制 屏保预览」· PAS 代码 · 共 113 行

PAS
113
字号
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    FileListBox1: TFileListBox;
    ListBox1: TListBox;
    Button1: TButton;
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FileListBox1DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
  procedure Fresh1;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function EnumProc(h : HWND; l:integer): boolean;stdcall; //更新ListBox1的内容
            // h:子窗口的句柄   l:运行函数的定义
  var buf : array[0..255] of char;
begin
  GetWindowText(h, buf, sizeof(buf)- 1);
    if iswindowvisible(h) then
      Form1.ListBox1.items.add('-'+strpas(buf)+':'+inttostr(h))
    else
      Form1.ListBox1.items.add('-'+strpas(buf)+':'+inttostr(h));
      Result := true;
end;

procedure TForm1.Fresh1;  //更新ListBox1的内容
begin
  ListBox1.clear;
  enumChildwindows(Panel2.handle,
  TFNWndEnumProc(@enumproc), 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FileListBox1.directory :='C:\WINDOWS\SYSTEM32';//读者可以查看自己的屏保程序的位置,重新设置路径
end;

procedure TForm1.FileListBox1DblClick(Sender: TObject); //运行屏保程序
begin
  WinExec(pchar(FileListBox1.FileName + ' /p ' + inttostr(Panel2.handle)), SW_Show); //预览屏保
  Fresh1;  //更新ListBox1
end;

procedure TForm1.Button1Click(Sender: TObject);//隐藏屏保
var
  h : integer;
  s : string;
begin
  if ListBox1.itemindex = -1 then exit;
  s := Listbox1.items[ListBox1.itemindex];
  h := strtoint(copy(s, pos(':', s) + 1, length(s)));
  ShowWindow(h, SW_HIDE); //隐藏屏保
  Fresh1;   //更新ListBox1
end;

procedure TForm1.Button2Click(Sender: TObject);//显示屏保
var
  h : integer;
  s : string;
begin
  if ListBox1.itemindex = -1 then exit;
  s := Listbox1.items[ListBox1.itemindex];
  h := strtoint(copy(s, pos(':', s) + 1, length(s)));
  ShowWindow(h, SW_SHOW);  //显示屏保
  Fresh1;  //更新ListBox1
end;

procedure TForm1.Button3Click(Sender: TObject); //关闭屏保
var
  h : integer;
  s : string;
begin
  if ListBox1.itemindex = -1 then exit;
  s := Listbox1.items[ListBox1.itemindex];
  h := strtoint(copy(s, pos(':', s) + 1, length(s)));
  PostMessage(h, WM_QUIT, 0, 0);  //关闭屏保
  Fresh1;  //更新ListBox1
end;

procedure TForm1.Button4Click(Sender: TObject); //退出程序
begin
  close;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?