+62 812-1171-5379 Fast Respond

Tips dan Trik Delphi - Tool

Macam macam perintah shellexecute / Tool / Windows / Delphi 7 - XE


ShellExecute(Application.Handle, 'open', 'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL);

ShellExecute(Application.Handle, 'open', 'c:\windows\notepad.exe', 'c:\SomeText.txt', nil, SW_SHOWNORMAL);

ShellExecute(Application.Handle, 'open', 'c:\MyDocuments\Letter.doc', nil, nil, SW_SHOWNORMAL);

ShellExecute(Application.Handle, 'open', 'https://example.com', nil, nil, SW_SHOWNORMAL);

Menghapus history project XE / Tool / Windows / Delphi 7 - XE


Uses Registry;

procedure TForm1.ClearRecentlyOpenedProjects;
var
  iCount  : Integer;
  Reg     : TRegistry;
  sList   : TStrings;
begin
  sList := TStringList.Create;
  Reg   := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Borland\BDS\5.0\Closed Projects', True) then
      Reg.GetValueNames(sList);
    if sList.Count > 0 then
      begin
        for iCount := 0 to Pred(sList.Count) do
          Reg.DeleteValue(sList[icount]);
      end
    else
      MessageDlg('No registry items to be cleaned at this time.',
        mtInformation, [mbOk], 0);
  finally
    sList.Free;
    Reg.Free;
  end;
end;

Membuat roman dari angka / Tool / Windows / Delphi 7 - XE


function IntToRoman(num: Cardinal): string;
const
  Nvals = 13;
  vals: array [1..Nvals] of word =
    (1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);
  roms: array [1..Nvals] of string[2] =
    ('I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C',
    'CD', 'D', 'CM', 'M');
var
  b: 1..Nvals;
begin
  result := '';
  b := Nvals;
  while num > 0 do
  begin
    while vals[b] > num do
      dec(b);
    dec (num, vals[b]);
    result := result + roms[b]
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     Edit2.Text := IntToRoman(Strtoint(Edit1.Text));
end;

Memeriksa berisi string atau angka / Tool / Windows / Delphi 7 - XE


function TForm1.IsNumber(pcString: PChar): Boolean;
begin
    Result := False;
    while pcString^ <> #0 do
    begin
        if not (pcString^ in ['0'..'9']) then
          Exit;
        Inc(pcString);
    end;
    Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     if IsNumber(pchar(Edit1.Text)) then
     begin
          ShowMessage('Benar');
     end else
     begin
          ShowMessage('Ditemukan ada huruf atau tanda baca lainnya');
     end;
end;


Mengetahui kecepatan processor komputer / Tool / Windows / Delphi 7 - XE


Uses Registry;

function GetCPUSpeed : String;
var
  Reg : TRegistry;

begin
 Reg := TRegistry.Create(KEY_QUERY_VALUE);
 try
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  if Reg.OpenKeyReadOnly('HARDWARE\DESCRIPTION\System\CentralProcessor\0') then
   begin
    Result := Format('CPU Speed is %dMHz', [Reg.ReadInteger('~MHz')]);
    Reg.CloseKey;
   end;
 finally
  Reg.Free;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     label1.Caption := GetCPUSpeed;
end;

Mengetahui kapasitas memory komputer atau Laptop / Tool / Windows / Delphi 7 - XE


Uses PSapi;

function ProcessMemory: longint;
var
  pmc: PPROCESS_MEMORY_COUNTERS;
  cb: Integer;
begin
  // Get the used memory for the current process
  cb := SizeOf(TProcessMemoryCounters);
  GetMem(pmc, cb);
  pmc^.cb := cb;
  if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then
     Result:= Longint(pmc^.WorkingSetSize);

  FreeMem(pmc);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     label1.Caption := floattostr(ProcessMemory);
end;

Membuat form selalu muncul diatas / Tool / Windows / Delphi 7 - XE


procedure TForm1.SetTopmost(AForm: TForm; ATop: boolean);
begin
     if ATop then
        SetWindowPos(AForm.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
      else
        SetWindowPos(AForm.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end;

Mengunci program supaya tidak jalan dua kali (2X) / Tool / Windows / Delphi 7 - XE

program Project1;

uses
  Forms, Windows,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;

  CreateMutex(nil, True, 'kursusdelphi.com');
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
       ShowMessage('Program anda sudah running');
       Halt;
  end;

  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


                                        
Pembulatan angka / Tool / Windows / Delphi 7 - XE

Untuk menghilangkan belakang koma silahkan gunakan fungsi Trunc(nilai), dan sedangkan untuk mengambil angka setelah koma gunakan fungsi Frac(nilai)


Function Bulatkan(Nilai : Double) : Double;
Var Hasilfrac : Double;
Begin
     Result := Nilai;
     Hasilfrac := Frac(Nilai);
     if Hasilfrac >= 0.5 Then
     Begin
          Result := TRUNC(nilai) + 1;
     End else
     if Hasilfrac < 0.5 Then
     Begin
          Result := TRUNC(nilai);
     End;
End;

Zoom pada StringGrid / Tool / Windows / Delphi 7 - XE

Zoom pada StringGrid


procedure TForm1.gridZoom(FFact: Real);
var
  x: Integer;
begin
      for x := 0 to StringGrid1.colcount - 1 do
        StringGrid1.colwidths[x] := round(StringGrid1.colwidths[x] * FFact);

      for x := 0 to StringGrid1.RowCount - 1 do
        StringGrid1.rowheights[x] := round(StringGrid1.rowheights[x] * FFact);

      StringGrid1.Font.Size := round(StringGrid1.rowheights[0] * 0.65);
end;


Cara memanggil procedure gridZoom(1.1); // Zoom In gridZoom(0.9); // Zoom Out