JPEG是一種圖像壓縮標準,很多精美的圖片都是采用這種格式標準,其特點是文件體積較小,而且支持24位色深。但是,Windows9x只支持位圖文件(即以bmp為擴展名的文件)的墻紙。下面這個程序就是將以jpg或jpeg為擴展名的圖片文件轉(zhuǎn)化為位圖文件,并通過修改注冊表的鍵值來通知Windows更換墻紙。本程序使用了一個Tform;一個OpenPictureDialog用來讓用戶選擇圖片;三個SpeedButton;分別用來打開OpenPictureDialog對話框,確定更換墻紙,退出程序。程序全部代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, registry, ExtDlgs, StdCtrls, Buttons;
type
TForm1 = class(Tform)
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
OpenPictureDialog1: TOpenPictureDialog;
procedure SpeedButton1Click(Sender: Tobject);
procedure SpeedButton2Click(Sender: Tobject);
procedure SpeedButton3Click(Sender: Tobject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.SpeedButton1Click(Sender: Tobject);
begin
openpicturedialog1.execute;{打開OpenPictureDialog對話框}
end;
procedure TForm1.SpeedButton2Click(Sender: Tobject);
var
reg: tregistry;{Tregistry對象在Registry單元中聲明,需要在Uese 中引用Registry單元}
jpeg: tjpegimage;{Tjpegimage對象在Jpeg單元中聲明,需要在Uese 中引用Jpeg單元}
bmp: tbitmap;
begin
if (openpicturedialog1.filename=``)
then {判斷OpenPictureDialog1中有無文件被選中}
messagedlg(`請選擇一張圖片`,mtinformation,[mbOK],0)
else
begin
jpeg:=tjpegimage.Create;
jpeg.LoadFromFile(openpicturedialog1.filename);{加載被用戶選中的文件}
bmp:=tbitmap.Create;
bmp.assign(jpeg);
bmp.savetofile(`c:\windows\wall.bmp`);{將轉(zhuǎn)換成功的文件保存的路徑及文件名}
reg:=tregistry.Create;
reg.rootkey:=hkey_current_user;{設置根鍵名稱}
reg.openkey(`control panel\desktop`,false);{打開Control Panel\Desktop路徑對應的主
鍵}
reg.writestring(`tilewallpaper`,`0`);
reg.writestring(`wallpaper`,`c:\windows\wall.bmp`);
{覆蓋并寫入新值TileWall- paper和Wallpaper串}
systemparametersinfo(spi_setdeskwallpaper,0,nil,spif_sendchange);{通知win-dows更
換墻紙}
reg.closekey;{寫入更改內(nèi)容并關閉注冊表}
reg.Free;{釋放對象}
close;
end;
end;
procedure TForm1.SpeedButton3Click(Sender: Tobject);
begin
close;
end;
end.
此程序只能將以jpg或jpeg為擴展名的圖片文件轉(zhuǎn)化為Windows的墻紙,因此應在OpenPictureDialog控件的Filter屬性中篩選顯示文件的類型,最好設置為JPEG Image File(*.jpg)和JPEG Image File(*.jpeg)文件。