Scroll Application Title on TaskBar with Delphi

procedure TForm1.Timer1timer(Sender: TObject);
begin
  Application.Title := Copy(Application.Title,
    2,Length(Application.Title)) + Application.Title[1];
  Application.Title := Application.Title[Length(Application.Title)] +
    Copy(Application.Title, 1,Length(Application.Title) - 1);
end;

Detect a keypress during a loop with Delphi

procedure TForm1.Button1Click(Sender: TObject);
var
  LoopAborted: Boolean;
  i: Integer;
begin
  LoopAborted := false;
  i := 0;
  repeat
    Caption := IntToStr(i);
    Application.ProcessMessages;
    if GetKeyState(VK_Escape) and 128 = 128 then
    begin
      LoopAborted :=
        true;
      Break;
    end;
    Inc(i);
  until i = 100000;
  if LoopAborted then
    ShowMessage('User has aborted the loop!');
end;

Example of controling the sound volume from Delphi

uses MMSystem;

type
  TVolumeRec = record
    case Integer of
      0: (LongVolume: Longint);
      1: (LeftVolume, RightVolume: Word);
  end;
const
  DeviceIndex = 5{0:Wave  1:MIDI  2:CDAudio  3:Line-In  4:Microphone  5:Master  6:PC-loudspeaker}

procedure SetVolume(aVolume: Byte);
var
  Vol: TVolumeRec;
begin
  Vol.LeftVolume := aVolume shl 8;
  Vol.RightVolume :=
  Vol.LeftVolume;
  auxSetVolume(UINT(DeviceIndex), Vol.LongVolume);
end;

function GetVolume: Cardinal;
var
  Vol: TVolumeRec;
begin
  AuxGetVolume(UINT(DeviceIndex), @Vol.LongVolume);
  Result := (Vol.LeftVolume + Vol.RightVolume) shr 9;
end;

Extract numbers from a string with Delphi

type
  TCharSet = set of Char;

function StripNonConforming(const S: string;
  const ValidChars: TCharSet): string;
var
  DestI: Integer;
  SourceI: Integer;
begin
  SetLength(Result, Length(S));
  DestI := 0;
  for SourceI := 1 to Length(S) do
    if S[SourceI] in
      ValidChars then
    begin
      Inc(DestI);
      Result[DestI] := S[SourceI]
    end;
  SetLength(Result,
    DestI)
end;

function StripNonNumeric(const S: string): string;
begin
  Result :=
    StripNonConforming(S, ['0'..'9'])
end;

Check if a disk is in the drive with Delphi

function DiskInDrive(Drive: Char): Boolean;
var
  ErrorMode: Word;
begin
  { make it upper case }
  if Drive in ['a'..'z'] then
    Dec(Drive, $20);
  { make sure it's a letter }
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  { turn off critical errors }
  ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := false
    else
      Result := true;
  finally
    { Restore old error mode }
    SetErrorMode(ErrorMode);
  end;
end;

A simple Delphi routine that gets the serial number of a disk

function FindVolumeSerial(const Drive: PChar): string;
var
  VolumeSerialNumber: DWORD;
  MaximumComponentLength: DWORD;
  FileSystemFlags: DWORD;
  SerialNumber: string;
begin
  Result := '';
  GetVolumeInformation(Drive, nil, 0, @
    VolumeSerialNumber, MaximumComponentLength, FileSystemFlags, nil, 0);
  SerialNumber :=
    IntToHex(HiWord(VolumeSerialNumber), 4) + ' - ' +
    IntToHex(LoWord(VolumeSerialNumber), 4);
  Result := SerialNumber;
end;