Delphi:开发win95屏幕保护预览程序

鬼谷子
Delphi:开发win95屏幕保护预览程序
  ----大家都知道windows屏幕保护程序的作用,而且新的屏幕保护程序越来越漂亮.如果在win95的桌面右键
菜单选属性,就弹出显示器设置界面,有一个标签是设置屏幕保护程序的. 
  ----在该页的画面上,有一个显示器图案,如果你选择win95所带的屏幕保护程序,这个屏幕保护程序就会在这个
小'显示器'上自动运行,你可以直接看到运行效果.这功能大大方便了屏幕保护程序的选择,这就是win95对屏幕保
护程序的新增接口:预览功能.
  ----目前大多数新推出的屏幕保护程序都支持这个接口.
  ----屏幕保护程序从它的诞生那时起,在同一时刻只能运行一个,不能多个同时运行,然而预览接口的推出,使同时
预览多个屏幕保护程序成为可能,本文将向读者介绍如何用Delphi开发这样一个程序. 
  ----1.屏幕保护预览接口 
  ----屏幕保护预览接口的使用很简单,这是通过传给屏幕保护程序的命令行参数来实现的,该命令行参数格式为:
  ----screensaver.exe/p##### 
  ----其中#####为一个有效的窗口句柄的10进制表示.
  ----这个窗口我们可以称之为预览窗口.
  ----实际上,支持预览接口的屏幕保护程序将自己的窗口创建为预览窗口的子窗口来实现预览功能的.
  ----2.画面布局 
  ----我们这个程序的窗口分为3部分,为倒'品'字形,上左部分列出所有可用的屏幕保护程序,上右部分列出所有
预览的屏幕保护程序,下面当然是预览窗口了. 
  ----用Delphi实现时,首先在Form里放2个TPanel组件,Panel1对齐方式为顶部对齐,Panel2为撑满用户区,
再在Panel1中放1个TFileListBox组件和一个TListBox组件,FileListBox1左对齐,ListBox1撑满用户区. 
  ----这样,FileListBox1为屏幕保护列表,ListBox1为预览列表,Panel2为预览窗口.
  ----3.列出屏幕保护程序. 
  ----将FileListBox1的Mask属性设为'*.scr',这是屏幕保护程序的扩展名.
  ----在FormCreate方法中将FileListBox1.directory设为windows系统目录GetSystemDirectory;
  ----4.预览屏幕保护程序. 
  ----在FileListBox1DblClick方法中运行该屏幕保护程序,并将Panel2的窗口句柄传给它.
  ----WinExec(pchar(FileListBox1.FileName+'/p'+inttostr(Panel2.handle)),SW_Show);
  ----运行程序,怎么样?COOL! 
  ----5.增加一些新特性:隐藏/显示/关闭.
  ---- 增 加2 个 函 数: 用 于 更 新ListBox1. 
  function EnumProc( 
  h : HWND ;// handle of child window 
  l : integer// application-defined value 
  ): boolean;stdcall; 
  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; 
  begin 
  ListBox1.clear; 
  enumChildwindows(Panel2.handle, 
  TFNWndEnumProc(@enumproc), 0); 
  end; 
  ----增加一个弹出菜单Popupmenu1, 3个菜单项, 'Show, Hide, Close', 将ListBox1.popupmemu指向
Popupmenu1.
  ----Hide的处理函数是: 
  procedure TForm1.Hide1Click(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; 
  end; 
  Show的处理函数是: 
  procedure TForm1.Show1Click(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; 
  end; 
  Close的处理函数是: 
  procedure TForm1.Close1Click(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; 
  end; 
  ----本程序在Delphi 3.0下调试通过, 应该能用Delphi 1.0 / 2.0编译.
  ----完整程序如下:
  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; 
  PopupMenu1: TPopupMenu; 
  Hide1: TMenuItem; 
  Show1: TMenuItem; 
  Close1: TMenuItem; 
  procedure FormCreate(Sender: TObject); 
  procedure FileListBox1DblClick(Sender: TObject);
  procedure Hide1Click(Sender: TObject); 
  procedure Show1Click(Sender: TObject); 
  procedure Close1Click(Sender: TObject); 
  private 
  { Private declarations } 
  public 
  { Public declarations } 
  procedure Fresh1; 
  end; 
  var 
  Form1: TForm1; 
  implementation 
  {$R *.DFM} 
  function EnumProc( 
  h : HWND ;// handle of child window 
  l : integer// application-defined value 
  ): boolean;stdcall; 
  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; 
  begin 
  ListBox1.clear; 
  enumChildwindows(Panel2.handle, 
  TFNWndEnumProc(@enumproc), 0); 
  end; 
  procedure TForm1.FormCreate(Sender: TObject);
  var buf : array[0..256] of char; 
  begin 
  GetSystemDirectory(buf, sizeof(buf) - 1); 
  FileListBox1.directory := strpas(buf); 
  ListBox1.popupmenu := Popupmenu1; 
  end; 
  procedure TForm1.FileList 
  Box1DblClick(Sender: TObject); 
  begin 
  WinExec(pchar(FileListBox1.FileName 
  + ' /p ' + inttostr(Panel2.handle)), 
  SW_Show); 
  Fresh1; 
  end; 
  procedure TForm1.Hide1Click(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; 
  end; 
  procedure TForm1.Show1Click(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; 
  end; 
  procedure TForm1.Close1Click(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; 
  end; 
  end. 

龙丘居士亦可怜
谈空说有夜不眠
忽闻河东师子吼
拄杖落手心茫然