Faq de Delphi

Comme mettre le focus sur un contrôle?

form1.ActiveControl := Edit1;

Comment mettre une image sur un bouton?

var
  Bitmap, Bitmap2: TBitmap;
  MyHandle: THandle;
  Rec: TRect;
begin
  Bitmap:=TBitmap.Create;
  Bitmap2:=TBitmap.Create;
  Bitmap2.LoadFromFile('c:\a.bmp');
  Rec:=Rect(2, 2, Button1.Width-2, Button1.Height-2);
  MyHandle:=GetDC(Button1.Handle);
  Bitmap.Canvas.Handle:=MyHandle;
  Bitmap.Canvas.StretchDraw(Rec, Bitmap2);
end;

Comment avoir la ligne courante dans un memo?

procedure TForm1.MemExpKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  LigneNum: LongInt;
begin
  if (Key=VK_UP)or(Key=VK_DOWN) then
  begin
    LigneNum:=MemExp.Perform(EM_LINEFROMCHAR, MemExp.SelStart, 0);
    Label1.Caption:='Line - '+IntToStr(LigneNum+1);
  end;
end;

Comment créer un composant à l'exécution

procedure TForm1.Button1Click(Sender: TObject);
var
  Button: TButton;
begin
  Button:=TButton.Create(Form1);
  try
    with Button do 
    begin
      Parent:=Self;
      Left:=Random(Form1.ClientWidth-Width);
      Top:=Random(Form1.ClientHeight-Height);
      Caption:='Button';
    end;
  except
    Button.Free;
  end;
end;

Comment fermer une application par son nom?

function KillApp(const sCapt: PChar) : boolean;
 var AppHandle:THandle;
begin
 AppHandle:=FindWindow(Nil, sCapt);
 Result:=PostMessage(AppHandle, WM_QUIT, 0, 0);
end;

Comment rendre certaine touches inactive

L'exemple ci-dessus désactive les touches ALT+TAB, CTRL+ESC, CTRL.

procedure SystemKeys(Disable: Boolean);
 var OldVal : LongInt;
begin
 SystemParametersInfo(SPI_SCREENSAVERRUNNING,
                      Word(Disable), @OldVal, 0);
end;

Comment prendre des caractère dans une string?

function RightStr
   (Const Str: String; Size: Word): String;
begin
 if Size > Length(Str) then Size := Length(Str);
 RightStr := Copy(Str, Length(Str)-Size+1, Size)
end;

function MidStr
   (Const Str: String; From, Size: Word): String;
begin
 MidStr := Copy(Str, From, Size)
end;

function LeftStr
   (Const Str: String; Size: Word): String;
begin
 LeftStr := Copy(Str, 1, Size)
end;
end;

Comment chercher et remplacer des caractères?

function SearchAndReplace
  (sSrc, sLookFor, sReplaceWith : string) : string;
var
  nPos, nLenLookFor : integer;
begin
  nPos        := Pos(sLookFor, sSrc);
  nLenLookFor := Length(sLookFor);
  while (nPos > 0) do begin
    Delete(sSrc, nPos, nLenLookFor);
    Insert(sReplaceWith, sSrc, nPos);
    nPos := Pos(sLookFor, sSrc);
  end;
  Result := sSrc;
end;

Comment vérifier si un fichier est utilisé?

function IsFileInUse(fName : string) : boolean;
var
   HFileRes : HFILE;
begin
   Result := false;
   if not FileExists(fName) then exit;
   HFileRes :=
     CreateFile(pchar(fName),
                GENERIC_READ or GENERIC_WRITE,
                0, nil, OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                0);
   Result := (HFileRes = INVALID_HANDLE_VALUE);
   if not Result then
   CloseHandle(HFileRes);
end;

Comment entrer que des chiffres ou lettre dans un TEdit ?

//que des chiffres
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if not (Key in ['0'..'9']) then
      Key := #0;
end;

//que des lettres
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if not (Key in ['a'..'z']) then
      Key := #0;
end;

Comment mettre le titre invisible d'une fenêtre?

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetWindowLong( Handle,
    GWL_STYLE,
    GetWindowLong( Handle, GWL_STYLE )
    and not WS_CAPTION );
  ClientHeight := Height;
end;


Page valide XHTML 1 Strict