Delphi:异类的屏幕保护
来源:优易学  2011-9-10 17:50:11   【优易学:中国教育考试门户网】   资料下载   IT书店

  我们知道,可以设定,当在一定的时间内没有对计算机进行任何操作时,启动屏幕保护程序。我们还可以规划计划任务,让电脑在某个设定的时间起动我们想进行的工作。这两个功能都是不错的构思,所以微软把它们一直保留了下来。不过,我们可能有更好的想法,希望能在电脑闲置一段时间后进行我们想做的工作,而不仅仅是启动屏幕保护程序。我们可以暂且称它为异类的屏幕保护。可是,我们怎么实现呢?
  不会很难吧?对于我们学会编程的人来说,稍稍动动脑筋就行了。难道不是吗?
  我一开始也是这样想的。不过,当我真的动手时,发现有一个为难的地方:我们怎样判断电脑是闲置的呢?
  不过这不要紧,我想。我们可以把启动的屏幕保护程序指向我们需要的任务嘛。不过,我想还有更好的方法。
  这个方法我找到了,而且代码也很简洁。我想把我的心得发表出来,与大家共享。
  如果我们做过钩子,不论是键盘钩子、鼠标钩子,或者是其它的系统钩子,一定还记得SetWindowsHookEx这个函数吧?我统计了一下,在《电脑爱好者》的程序谷中,这个函数也是频繁的出现。
  不多说啦,今天的程序的核心就是SetWindowsHookEx这个函数了,没有其它要说明的地方。我原来想对程序添加注释,不过你看看程序吧,真的没有必要啊,所以就不多说什么了。哈哈!
  程序在DELPHI6.0+WINDOWS ME下编程通过。
  unit Unit1;
  interface
  uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls,SHELLAPI;
  type
  TForm1 = class(TForm)
  Label1: TLabel;
  Timer1: TTimer;
  procedure FormCreate(Sender: TObject);
  procedure Timer1Timer(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
  { Private declarations }
  public
  { Public declarations }
  end;
  var
  Form1: TForm1;
  hHook: integer;
  TimeTotal: integer;
  bNotRunning:boolean;
  implementation
  {$R *.DFM}
  const
  Timescount = 10;
  function JournalRecordProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
  begin
  TimeTotal := 0;
  Result := 0;
  end;
  function StartHook: Boolean;

  begin
  Result := False;
  if hHook = 0 then
  begin
  hHook := SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordProc, HInstance, 0);
  if hHook > 0 then Result := True;
  end;
  end;
  procedure StopHook;
  begin
  if hHOok > 0 then
  begin
  UnHookWindowsHookEx(hHook);
  hHook := 0;
  end;
  end;
  procedure TForm1.FormCreate(Sender: TObject);
  begin
  hHook := 0;
  bNotRunning:=True;
  StartHook;
  end;
  procedure TForm1.Timer1Timer(Sender: TObject);
  begin
  inc(TimeTotal);
  label1.Caption := floattostr(TimeTotal);
  if (TimeTotal > Timescount) and bNotRunning then
  begin
  bNotRunning:=False;
  ShellExecute(Handle, \'open\', PChar(\'notepad\'),
  nil, nil, SW_SHOW);
  end;
  end;
  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
  stophook;
  end;

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训