Components

Scroll Memo by page

Posted in Components

Use SendMessage function and WM_VSCROLL message with SB_PAGEDOWN or SB_PAGEUP parameters.

procedure TForm1.Button2Click(Sender: TObject);
begin
  SendMessage(Memo1.Handle, WM_VSCROLL, SB_PAGEUP, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMessage(Memo1.Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
end;

Save StringGrid to file

Posted in Components

Use StringGrid2File procedure to save TStringGrid to file. Use File2StringGrig to restore TStringGrid from file.

procedure TForm1.File2StringGrid(StringGrid: TStringGrid; 
                                 FileName: String);
var
  F: TextFile;
  Tmp, x, y: Integer;
  TmpStr: string;
begin
  AssignFile(F, FileName);
  Reset(F);
  Readln(F, Tmp);
  StringGrid.ColCount:=Tmp;
  Readln(F, Tmp);
  StringGrid.RowCount:=Tmp;
  for x:=0 to StringGrid.ColCount-1 do
    for y:=0 to StringGrid.RowCount-1 do
    begin
      Readln(F, TmpStr);
      StringGrid.Cells[x,y]:=TmpStr;
    end;
  CloseFile(F);
end;

procedure TForm1.StringGrid2File(StringGrid: TStringGrid; 
                                 FileName: String);
var
  F: TextFile;
  x, y: Integer;
begin
  AssignFile(F, FileName);
  Rewrite(F);
  Writeln(F, StringGrid.ColCount);
  Writeln(F, StringGrid.RowCount);
  for x:=0 to StringGrid.ColCount-1 do
    for y:=0 to StringGrid.RowCount-1 do
      Writeln(F, StringGrid.Cells[x,y]);
  CloseFile(F);
end;

Running caption of the label

Posted in Components

Use Label and Timer components. It is necessary to repeat the last part of the string for getting "running" effect.

procedure TForm1.Timer1Timer(Sender: TObject);
const
  Str: string = 'It is necessary to repeat the last part' + 
    'of this string. It is necessary to re';
  i: Integer = 1;
  Count: Integer = 20;
begin
  Label1.Caption:=Copy(Str, i, Count);
  Inc(i);
  if i>Length(Str)-Count then i:=1;
end;

Put bitmap to StringGrid

Posted in Components

Use StrechDraw property of the StringGrid.Canvas and don't forget to free the bitmap.

procedure TForm1.Button1Click(Sender: TObject);
var
  Bitmap: TBitmap;
begin
  Bitmap:=TBitmap.Create;
  Bitmap.LoadFromFile('factory.bmp');
  StringGrid1.Canvas.StretchDraw(StringGrid1.CellRect(1,1),Bitmap);
  Bitmap.Free;
end;

Play a sound on mouse enter

Posted in Components

Use CM_MOUSEENTER and CM_MOUSELEAVE messages like this:

uses MMSystem;

   TYourObject = class(TAnyControl)
  ...
  private
    procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
  ...
  end;

implementation

procedure TYourObject.CMMouseEnter(var AMsg: TMessage);
begin
  sndPlaySound('c:\win98\media\ding.wav',snd_Async or snd_NoDefault);
end;

procedure TYourObject.CMMouseLeave(var AMsg: TMessage);
begin
  sndPlaySound(nil,snd_Async or snd_NoDefault);
end;