www.pudn.com > Desktop-Manager.rar > Unit4.pas, change:2004-05-30,size:3302b


unit Unit4; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, DBCtrls, ExtCtrls, Menus, JPEG, Registry; 
 
type 
  TForm4 = class(TForm) 
    Image1: TImage; 
    PopupMenu1: TPopupMenu; 
    Quit: TMenuItem; 
    SetToDesktop: TMenuItem; 
    procedure QuitClick(Sender: TObject); 
    procedure SetToDesktopClick(Sender: TObject); 
    procedure Image1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  Form4: TForm4; 
 
implementation 
uses unit1; 
{$R *.dfm} 
 
procedure TForm4.QuitClick(Sender: TObject); 
begin 
  close; 
end; 
 
procedure TForm4.SetToDesktopClick(Sender: TObject); 
var 
  JPEG:TJPEGImage; 
  bmp:TBitmap; 
  Reg:TRegistry;//创建注册表对象读取注册表 
  FileName:string; 
begin 
  if Form1.DataSource1.DataSet.FieldByName('picture').IsNull=false then 
  begin 
///////////////////////////////////////////////////////////////////// 
//每次用本软件换桌面都会留下无用bmp图片, 
//为了节省磁盘空间,在换桌面前删除以前的图片 
    Reg:=TRegistry.Create; 
    Reg.RootKey:=HKEY_CURRENT_USER;//设置根键为HKEY_CURRENT_USER 
    try//如果已经存在主键,则读取最近一次用本软件设置为壁纸的图片文件名 
      if Reg.KeyExists('\software\DesktopManagement') then 
      begin 
        Reg.OpenKey('\software\DesktopManagement',false); 
        FileName:=Reg.ReadString('FileName'); 
        Reg.CloseKey; 
        //的到文件名后先删除指定路径下的该文件,在删除余下的空目录 
        DeleteFile('C:\WINDOWS\Web\Wallpaper\myWallPaper\'+FileName+'.bmp'); 
        RmDir('C:\WINDOWS\Web\Wallpaper\myWallPaper') 
      end; 
      //创建主键,并把当前设置为桌面的图片的文件名写入注册表 
      Reg.OpenKey('\software\DesktopManagement',true); 
      Reg.WriteString('FileName',Form1.DBText1.Caption); 
      Reg.CloseKey; //关闭主键 
    Finally 
        Reg.Free;//释放TRegistry对象 
    end; 
 
    //将当前图片转换为bmp格式存到C:\WINDOWS\Web\Wallpaper\myWallPaper目录下 
    //如果目录不存在则创建 
    if not DirectoryExists('C:\WINDOWS\Web\Wallpaper\myWallPaper') then 
      ForceDirectories('C:\WINDOWS\Web\Wallpaper\myWallPaper'); 
 
      JPEG:=TJPEGImage.Create; 
      bmp:=Tbitmap.Create; 
 
      //以下2句实现bmp向jpeg的转换 
      JPEG.Assign(Image1.Picture); 
      bmp.Assign(JPEG); 
 
      //将得到的位图存到指定路径 
      bmp.SaveToFile('C:\WINDOWS\Web\Wallpaper\myWallPaper\'+Form1.DBText1.Caption+'.bmp'); 
     //调用aip函数设置桌面 
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,pchar('C:\WINDOWS\Web\Wallpaper\myWallPaper\' 
                         +Form1.DBText1.Caption+'.bmp'),SPIF_UPDATEINIFILE{=1}); 
/////////////////////////////////////////////////// 
//最小化桌面所有程序,用api模拟了win+D的动作 
    keybd_event(91,mapvirtualkey(91,0),0,0); 
    keybd_event(77,mapvirtualkey(77,0),0,0); 
    keybd_event(77,mapvirtualkey(77,0),keyeventf_keyup	,0); 
    keybd_event(91,mapvirtualkey(91,0),keyeventf_keyup	,0); 
//过1000毫秒重新显示程序主窗口 
    Form1.Hide; 
    Form4.Hide; 
    form4.Close; 
    sleep(1000); 
    Form1.Show; 
///////////////////////////////////////////////////////////////// 
  end 
  else 
    ShowMessage('没有图片源!'); 
end; 
 
procedure TForm4.Image1Click(Sender: TObject); 
begin 
  close; 
end; 
 
end.