Windows 95/98 使用註冊表管理系統資料,壁紙設定儲存於 HKEY_CURRENT_USER\Control Panel\Desktop 的 Wallpaper 與 TileWallpaper 鍵值。修改這兩個值並發送訊息通知 Windows 即可更換壁紙。
TForm 主視窗TSpeedButton:瀏覽圖片、套用壁紙TImage:預覽圖片TFileListBox、TDriveComboBox、TDirectoryListBox,設定 Mask 過濾 *.bmpprocedure TForm1.SpeedButton1Click(Sender: TObject); // 瀏覽
begin
if FileListBox1.FileName = '' then
MessageDlg('請先選擇一幅位圖', mtInformation, [mbOK], 0)
else
Image1.Picture.LoadFromFile(FileListBox1.FileName);
end;
procedure TForm1.SpeedButton2Click(Sender: TObject); // 套用壁紙
var
Reg: TRegistry;
begin
if FileListBox1.FileName = '' then
MessageDlg('請先選擇一幅位圖', mtInformation, [mbOK], 0)
else
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
Reg.OpenKey('Control Panel\Desktop', False);
Reg.WriteString('TileWallpaper', '0'); // 0=置中, 1=平鋪
Reg.WriteString('Wallpaper', FileListBox1.FileName);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, nil, SPIF_SENDCHANGE); // 通知系統更新
Reg.CloseKey;
Reg.Free;
end;
end;
關鍵提醒:呼叫 OpenKey 時第二個參數必須設為 False,否則可能建立新鍵導致設定失效。