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.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. ////////////////////////////////////////////////// 实际上Windows的屏幕保护程序也是一个可执行文件,只不过扩展名是 .SCR而不是.EXE。但是它也应满足一定的要求,以便提供给控制面板合适的接口。使得用户可以在显示器的“属性”中选择和配置它。通常它应满足以下要求: 1、包含有关于自己的设置; 2、提供关于自己的描述; 3、能够区分是激活状态还是配置状态; 4、不允许自己的拷贝同时运行; 5、当键盘或鼠标有动作时退出程序。 其中,“设置”与普通程序一样,只是要注意,只有在控制面板中配置它时“设置”窗口才会被激活。下面我们主要讨论剩下的四个问题。 一、首先,建立屏幕保护的主窗口。 在Delphi的菜单上或工具条上选择"New Form"建立一个"Blank Form"在Object Inspector中将它的属性配置为: BorderIcon [] biSystemMenu False biMinimize False biMaximize False BorderStyle bsNone Color clBlack FormStyle fsStayOnTop name Sav_Form Visible False 从System控件板中选择一个"Timer"控件,设置它的属性为: Enabled Ture Interval 2000 Name Timer1 Tag 0 在本例中保护屏幕上显示照片,所以要在窗口上再加一个Image将它的AuotoSize设为Ture,Visible设为False,双击装入你想显示的图片,本例采用《计算机世界》1998年49期中介绍的方法,使BMP图片以垂直交错的方法显示,代码如下:(详情见《计算机世界》1998年49期“Delphi中的图形显示技巧”) private { Private declarations } procedure ShowPic(); ..... procedure TSav_Form.ShowPic(Sender: TObject); var newbmp:TBitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= TBitmap.Create; newbmp.Width:=image1.Width; newbmp.Height:=image1.Height; bmpheight:=image1.Height; bmpwidth:=image1.Width; i:=0; while i< =bmpheight do begin j:=i; while(j >0)do begin newbmp.Canvas.CopyRect(Rect(0,j-1,bmpwidth,j),image1. Canvas,Rect(0,bmpheight-i+j-1,bmpwidth,bmpheight-i+j)); newbmp.Canvas.CopyRect(Rect(0,bmpheight-j,bmpwidth, bmpheight-j+1),image1.Canvas,Rect(0,i-j,bmpwidth,i-j+1)); j:=j-2; end; Sav_Form.Canvas.Draw(20,10,newbmp); i:=i+2; end; newbmp.free; end; 当然,你也可以采用别的方法显示,或者将屏幕设计成做你想做的任何模样,只要在定时器的响应时间中调用你的函数就行了。本例,在定时器的OnTimer消息处理程序中调用ShowPic(); type ...... procedure Timer1Timer(Sender: TObject);
procedure TSav_Form.Timer1Timer(Sender: TObject); begin ShowPic; end; 剩下的工做就是让设计好的Sav_Form能覆盖整个屏幕,并隐藏鼠标。点击Sav_Form在Object Inspector中选择Events标签,双击OnActive消息为它加上消息处理函数FormActivate(Sender: TObject);添加代码如下: trype procedure FormActivate(Sender: TObject); procedure TSav_Form.FormActivate(Sender: TObject); begin WindowsState:=wsMaximized; //覆盖屏幕; ShowCursor(false); //隐藏光标; end; 二、监测键盘和鼠标 保护屏幕作好后,就要监测键盘和鼠标。方法是为Application.OnMessage事件建立一个句柄,检测中断屏幕保护的条件是否满足。在Sav_Form的Private中加入以下代码: procedure Detect(var Msg:TMsg;var Handled:boolean); 在implementation中加入以下代码: procedure TSav_Form.Detect(var Msg:TMsg; var Handled:boolean); Var Done:boolean; begin Done:=false; if (Msg.message=WM_KEYDOWN or Msg.message=WM_MOUSEMOVE) then done:=true; if done then begin close; end; end; ---- 在为窗口的OnShow事件加上处理函数。在函数中加上以下代码: procedure TSav_Form.FormShow(Sender: TObject); begin Application.OnMessage:=Detect; // ShowCursor(false); end; 三、提供关于自己的描述 你可以用Delphi中的{$D text} 定义出现在“显示”“属性”中显示的你的屏幕保护程序的名称,$D参数直接将text插入可执行文件入口的描述模块(Description Module) 为了让控制面板认识你的描述,必须加入"SCRNSAVE"。选择项目文件在{$R *.RES}后加入以下代码: {$D SCRNSAVE My Screen Saver}"My Screen Saver"将显示在控制面板中的“显示”“属性”中。 四、激活状态和设置状态,以及屏蔽多拷贝的运行 Windows在两种条件下运行屏幕保护程序,1、当屏幕保护的时间设置到时则激活;2、当屏幕保护被设置时激活。为了区分这两种状态Windows在命令后加了参数, "/s"表示是激活状态,"/c"表示是设置状态。因此,我们的屏幕保护程序也必须区分参数以便做不同的处理。 屏幕保护程序和其他程序不同的是,它不允许自己的拷贝同时运行。hPrevInst是一个Delphi定义的全局变量,它指向当前程序前一个拷贝,如果该变量为0,则表示当前程序是唯一的实例。我们可以利用这个变量屏蔽多拷贝的运行。 以下的代码实现了区分激活状态和设置状态,以及屏蔽多拷贝的运行的功能。(该代码在项目文件中) begin if hPrevInst = 0 then begin //如果当前实例是唯一实例 if (ParamCount > 0) and (UpperCase(ParamStr(1)) = '/S') then begin //如果是激活状态 Application.Initialize; Application.CreateForm(TScrnFrm, ScrnFrm); end else begin //可以在这里加入你的配置代码 end; Application.Run; end; end. 五、安装 ---- 最后,将程序编译、链接后拷贝到Windows目录下,将程序的扩展名改为.SCR即可在控制面板中选择"My Screen Saver"了。以上代码均在Delphi4.0中编译通过 ///////////////////////////////////////////////////
procedure TForm2.Image1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if trace=true then //是否在追踪鼠标? begin //是,擦除旧的矩形并画上新的矩形 with image1.canvas do begin rectangle(x1,y1,oldx,oldy); Rectangle(x1,y1,x,y); oldx:=x; oldy:=y; end; end else if flag=true then //在鼠标所在的位置上画十字 begin with image1.canvas do begin moveTo(foldx,0); //擦除旧的十字 LineTo(foldx,screen.height); moveto(0,foldy); lineto(screen.width,foldy); moveTo(x,0); //画上新的十字 LineTo(x,screen.height); moveto(0,y); lineto(screen.width,y); foldx:=x; foldy:=y; end; end; end;