首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 實作桌面壁紙更換器:註冊表操作與系統通知

     編輯:Delphi

Windows 95/98 使用註冊表管理系統資料,壁紙設定儲存於 HKEY_CURRENT_USER\Control Panel\Desktop 的 Wallpaper 與 TileWallpaper 鍵值。修改這兩個值並發送訊息通知 Windows 即可更換壁紙。

介面配置

  • TForm 主視窗
  • 兩個 TSpeedButton:瀏覽圖片、套用壁紙
  • TImage:預覽圖片
  • 檔案控制項群:TFileListBox、TDriveComboBox、TDirectoryListBox,設定 Mask 過濾 *.bmp

核心程式碼

procedure 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,否則可能建立新鍵導致設定失效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved