Gehe zu deutscher Webseite

ViaThinkSoft CodeLib

This article is in:
CodeLibProgramming aidsDelphi

uses
  MMSystem, Registry;

procedure PlayAppEventSound(EventName: string);
begin
  PlaySound(PChar(EventName), 0, SND_APPLICATION or SND_ALIAS or SND_ASYNC or SND_NODEFAULT);
end;

procedure RegisterAppEventSound(AppDescription, EventName, EventDescription, WaveFile: string);
var
  reg: TRegistry;
  AppName: string;
begin
  if (Copy(WaveFile, 2, 1) <> ':') and (Copy(WaveFile, 1, 2) <> '\\') then
    WaveFile := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + WaveFile;

  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CURRENT_USER;

    AppName := ChangeFileExt(ExtractFileName(ParamStr(0)),'');

    if not reg.KeyExists('AppEvents\Schemes\Apps\'+AppName)
       and reg.OpenKey  ('AppEvents\Schemes\Apps\'+AppName, true) then
    begin
      reg.WriteString('', AppDescription);
      reg.CloseKey;
    end;

    if not reg.KeyExists('AppEvents\Schemes\Apps\'+AppName+'\'+EventName+'\.Current')
       and reg.OpenKey  ('AppEvents\Schemes\Apps\'+AppName+'\'+EventName+'\.Current', true) then
    begin
      reg.WriteString('', WaveFile);
      reg.CloseKey;
    end;

    if not reg.KeyExists('AppEvents\Schemes\Apps\'+AppName+'\'+EventName+'\.Default')
       and reg.OpenKey  ('AppEvents\Schemes\Apps\'+AppName+'\'+EventName+'\.Default', true) then
    begin
      reg.WriteString('', WaveFile);
      reg.CloseKey;
    end;

    if not reg.KeyExists('AppEvents\EventLabels\'+EventName)
       and reg.OpenKey  ('AppEvents\EventLabels\'+EventName, true) then
    begin
      reg.WriteString('', EventDescription);
      reg.CloseKey;
    end;
  finally
    FreeAndNil(reg);
  end;
end;

const
  MYAPP_EVENT_NEWVOICEMAIL     = 'MYAPP_NewVoiceMail';
  MYAPP_EVENT_TICKETSIGNAL     = 'MYAPP_TicketSignal';
  MYAPP_EVENT_TICKETOTHEREVENT = 'MYAPP_TicketOtherEvent';

procedure TForm6.Button1Click(Sender: TObject);
resourcestring
  AppDescription = 'My App';
begin
  // Run this at program startup
  RegisterAppEventSound(AppDescription, MYAPP_EVENT_NEWVOICEMAIL,     'New voice mail', 'Sound1.wav');
  RegisterAppEventSound(AppDescription, MYAPP_EVENT_TICKETSIGNAL,     'Ticket signal',  'Sound2.wav');
  RegisterAppEventSound(AppDescription, MYAPP_EVENT_TICKETOTHEREVENT, 'Ticket event',   'Sound3.wav');

  // Run this when you want to play the sound
  PlayAppEventSound(MYAPP_EVENT_TICKETSIGNAL);
end;
Daniel Marschall
ViaThinkSoft Co-Founder